19 個代碼片段讓 WordPress 更易于管理

openkk 12年前發布 | 11K 次閱讀 WordPress

19 個代碼片段讓 WordPress 更易于管理

1. 根據用戶名來限制管理菜單項目的訪問

如果你希望你的管理菜單的某些項對某些用戶可見,那么這個代碼就會幫到你。只需替換 functions.php 中的 clients-username 為如下代碼即可:

function remove_menus()
{
    global $menu;
    global $current_user;
    get_currentuserinfo();

if($current_user->user_login == 'clients-username')
{
    $restricted = array(__('Posts'),
                        __('Media'),
                        __('Links'),
                        __('Pages'),
                        __('Comments'),
                        __('Appearance'),
                        __('Plugins'),
                        __('Users'),
                        __('Tools'),
                        __('Settings')
    );
    end ($menu);
    while (prev($menu)){
        $value = explode(' ',$menu[key($menu)][0]);
        if(in_array($value[0] != NULL?$value[0]:"" , $restricted)){unset($menu[key($menu)]);}
    }// end while

}// end if

} add_action('admin_menu', 'remove_menus');</pre>

Source

 

2. 從面板中刪除默認的Widget部件

// Create the function to use in the action hook
function example_remove_dashboard_widgets() {
    // Globalize the metaboxes array, this holds all the widgets for wp-admin

global $wp_meta_boxes;

// Remove the incomming links widget
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_incoming_links']);   

// Remove right now
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_right_now']);
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']);
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_secondary']);

}

// Hoook into the 'wp_dashboard_setup' action to register our function add_action('wp_dashboard_setup', 'example_remove_dashboard_widgets' );</pre>

Source

 

3. 在 WP Admin 中顯示緊急信息

該代碼將任何登錄的用戶顯示定制的消息

/**

  • Generic function to show a message to the user using WP's
  • standard CSS classes to make use of the already-defined
  • message colour scheme. *
  • @param $message The message you want to tell the user.
  • @param $errormsg If true, the message is an error, so use
  • the red message style. If false, the message is a status

    • message, so use the yellow information message style. */ function showMessage($message, $errormsg = false) { if ($errormsg) { echo '

      '; } else { echo '
      '; }

      echo "

      $message

      </div> "; } </div> </pre>

      然后,為管理提醒函數增加鉤子用來顯示定制消息:

      /**

  • Just show our message (with possible checking if we only want
  • to show message to certain users. */ function showAdminMessages() { // Shows as an error message. You could add a link to the right page if you wanted. showMessage("You need to upgrade your database as soon as possible...", true);

    // Only show to admins if (user_can('manage_options') {

    showMessage("Hello admins!");
    

    } }

/**

  • Call showAdminMessages() when showing other admin
  • messages. The message only gets shown in the admin
  • area, but not on the frontend of your WordPress site. */ add_action('admin_notices', 'showAdminMessages');</pre>

    Source

     

    4. 隱藏 WordPress 更新提醒

    add_action('admin_menu','wphidenag');
    function wphidenag() {
    remove_action( 'admin_notices', 'update_nag', 3 );
    }

    Source

     

    5. 從文章、頁面編輯器中移除 Meta-Boxes

    function remove_extra_meta_boxes() {
    remove_meta_box( 'postcustom' , 'post' , 'normal' ); // custom fields for posts
    remove_meta_box( 'postcustom' , 'page' , 'normal' ); // custom fields for pages
    remove_meta_box( 'postexcerpt' , 'post' , 'normal' ); // post excerpts
    remove_meta_box( 'postexcerpt' , 'page' , 'normal' ); // page excerpts
    remove_meta_box( 'commentsdiv' , 'post' , 'normal' ); // recent comments for posts
    remove_meta_box( 'commentsdiv' , 'page' , 'normal' ); // recent comments for pages
    remove_meta_box( 'tagsdiv-post_tag' , 'post' , 'side' ); // post tags
    remove_meta_box( 'tagsdiv-post_tag' , 'page' , 'side' ); // page tags
    remove_meta_box( 'trackbacksdiv' , 'post' , 'normal' ); // post trackbacks
    remove_meta_box( 'trackbacksdiv' , 'page' , 'normal' ); // page trackbacks
    remove_meta_box( 'commentstatusdiv' , 'post' , 'normal' ); // allow comments for posts
    remove_meta_box( 'commentstatusdiv' , 'page' , 'normal' ); // allow comments for pages
    remove_meta_box('slugdiv','post','normal'); // post slug
    remove_meta_box('slugdiv','page','normal'); // page slug
    remove_meta_box('pageparentdiv','page','side'); // Page Parent
    }
    add_action( 'admin_menu' , 'remove_extra_meta_boxes' );

    Source

     

    6. 創建個性化的面板

    可輕松編輯自己的應用

    // Create the function to output the contents of our Dashboard Widget
    function example_dashboard_widget_function() {
    // Display whatever it is you want to show
    echo "Hello World, I'm a great Dashboard Widget";
    } 

// Create the function use in the action hook function example_add_dashboard_widgets() { wp_add_dashboard_widget('example_dashboard_widget', 'Example Dashboard Widget', 'example_dashboard_widget_function'); } // Hoook into the 'wp_dashboard_setup' action to register our other functions add_action('wp_dashboard_setup', 'example_add_dashboard_widgets' );</pre>

Source

 

7. 禁用插件停用的功能

該代碼將移除插件中的 Deactivate 鏈接,將下面代碼保存到 functions.php 并保存。

add_filter( 'plugin_action_links', 'slt_lock_plugins', 10, 4 );
function slt_lock_plugins( $actions, $plugin_file, $plugin_data, $context ) {
    // Remove edit link for all
    if ( array_key_exists( 'edit', $actions ) )
        unset( $actions['edit'] );
    // Remove deactivate link for crucial plugins
    if ( array_key_exists( 'deactivate', $actions ) && in_array( $plugin_file, array(
        'slt-custom-fields/slt-custom-fields.php',
        'slt-file-select/slt-file-select.php',
        'slt-simple-events/slt-simple-events.php',
        'slt-widgets/slt-widgets.php'
    )))
        unset( $actions['deactivate'] );
    return $actions;
}

Source

 

8. 根據角色添加、刪除和記錄面板部件

function tidy_dashboard()
{
  global $wp_meta_boxes, $current_user;

// remove incoming links info for authors or editors if(in_array('author', $current_user->roles) || in_array('editor', $current_user->roles)) { unset($wp_meta_boxes['dashboard']['normal ']['core']['dashboard_incoming_links']); }

// remove the plugins info and news feeds for everyone unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_plugins']); unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']); unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_secondary']);

} //add our function to the dashboard setup hook add_action('wp_dashboard_setup', 'tidy_dashboard');</pre>

下面是用來取消默認面板部件的代碼列表:

//Right Now - Comments, Posts, Pages at a glance
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_right_now']);
//Recent Comments
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_recent_comments']);
//Incoming Links
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_incoming_links']);
//Plugins - Popular, New and Recently updated WordPress Plugins
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_plugins']);

//Wordpress Development Blog Feed unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']); //Other WordPress News Feed unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_secondary']); //Quick Press Form unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_quick_press']); //Recent Drafts List unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_recent_drafts']);</pre>

Source

 

9. 更簡單的登錄 URL

將下面代碼粘貼到 .htaccess 文件,放在 WordPress rewrite 規則之前

RewriteRule ^login$ http://yoursite.com/wp-login.php [NC,L]

Source

 

10. Remove Pages Columns

下面代碼允許你移除 ‘Pages’ 頁中的你不再需要的列

function remove_pages_columns($defaults) {
  unset($defaults['comments']);
  return $defaults;
}
add_filter('manage_pages_columns', 'remove_pages_columns');

Source

 

11. 禁止修改主題

該代碼將面板中的 ‘Appearance’ 菜單項移除

add_action( 'admin_init', 'slt_lock_theme' );
function slt_lock_theme() {
    global $submenu, $userdata;
    get_currentuserinfo();
    if ( $userdata->ID != 1 ) {
        unset( $submenu['themes.php'][5] );
        unset( $submenu['themes.php'][15] );
    }
}

Source

 

12. 修改面板底部的文本

function remove_footer_admin () {
    echo "Your own text";
}

add_filter('admin_footer_text', 'remove_footer_admin');</pre>

Source

 

13. Remove Author Metabox/Options & Move to Publish MetaBox

// MOVE THE AUTHOR METABOX INTO THE PUBLISH METABOX
add_action( 'admin_menu', 'remove_author_metabox' );
add_action( 'post_submitbox_misc_actions', 'move_author_to_publish_metabox' );
function remove_author_metabox() {
    remove_meta_box( 'authordiv', 'post', 'normal' );
}
function move_author_to_publish_metabox() {
    global $post_ID;
    $post = get_post( $post_ID );
    echo '
   
Author: '; post_author_meta_box( $post ); echo '
'; }

Source

 

14. 修改登錄的 Logo

新的 logo 尺寸是 326 x 82 ,把圖片放到主題的 images 目錄,然后修改下面代碼的 ‘companylogo.png’ 并將代碼粘貼到 functions.php

// login page logo
function custom_login_logo() {
    echo '
   
';
}
add_action('login_head', 'custom_login_logo');

Source

 

15. 移除文章 Columns

該代碼將移除 posts 頁面的列

function remove_post_columns($defaults) {
  unset($defaults['comments']);
  return $defaults;
}
add_filter('manage_posts_columns', 'remove_post_columns');

Source

 

16. 禁用管理面板的頂級菜單

function remove_menus () {
global $menu;
    $restricted = array(__('Dashboard'), __('Posts'), __('Media'), __('Links'), __('Pages'), __('Appearance'), __('Tools'), __('Users'), __('Settings'), __('Comments'), __('Plugins'));
    end ($menu);
    while (prev($menu)){
        $value = explode(' ',$menu[key($menu)][0]);
        if(in_array($value[0] != NULL?$value[0]:"" , $restricted)){unset($menu[key($menu)]);}
    }
}
add_action('admin_menu', 'remove_menus');

Source

 

17. 禁用管理面板的子菜單

function remove_submenus() {
  global $submenu;
    unset($submenu['index.php'][10]); // Removes 'Updates'.
    unset($submenu['themes.php'][5]); // Removes 'Themes'.
    unset($submenu['options-general.php'][15]); // Removes 'Writing'.
    unset($submenu['options-general.php'][25]); // Removes 'Discussion'.
}
add_action('admin_menu', 'remove_submenus');

Source

 

18. 增加自定義的面板 Logo

首先需要創建透明的圖片,尺寸為 30x31px (.gif or .png) 然后保存到主題的 images 目錄 (/wp-content/themes/theme-name/images) ,名字任意

//hook the administrative header output
add_action('admin_head', 'my_custom_logo');

function my_custom_logo() { echo '