我們使用 WordPress 程序做網站之後,WordPress 後台有很多我們不需要的內容,例如:WordPress 開發博客"、"其他 WordPress 新聞","日曆"(WP_Widget_Calendar) 、"搜索"(WP_Widget_Search) 、"最近評論"(WP_Widget_Recent_Comments),版本升級等一些信息,都是我們自己做網站用不到的內容,我們可以通過修改修改 WordPress 後台內容屏蔽掉。

去除 WordPress 後台 Widget

WordPress 後台就是我們登陸後看到的那個界面,包括了概況、近期評論、引入鏈接等 Widget 的界面,去除所有 Widget 的代碼如下:

if ( ! function_exists( 'remove_dashboard_widgets' ) ) :
/**
 * Remove dashboard widgets
 */
function remove_dashboard_widgets(){
 global $wp_meta_boxes;
 unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_right_now']);
 unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_recent_comments']);
 unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_incoming_links']);
 unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_plugins']);
 unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_quick_press']);
 unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_recent_drafts']);
 unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']);
 unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_secondary']);
}
add_action('wp_dashboard_setup', 'remove_dashboard_widgets');

endif;

如果希望保留其中的某些 Widget,只要註釋掉或刪除到代碼中的某些 unset 就可以了。

增加 WordPress 後台 Widget

去除了原有的,那麼自然也就想到加入自己的 Widget 來顯得更加個性化,比如增加一個歡迎提示或是常用任務等。代碼如下;

if ( ! function_exists( 'add_dashboard_widgets' ) ) :
/**
 * Add dashboard widgets
 */
function welcome_dashboard_widget_function() {
 // Display whatever it is you want to show
 echo "<ul><li><a href='post-new.php'> 添加新文章</a></li><li><a href='edit.php'> 修改文章</a></li></ul>";
}

// Create the function use in the action hook

function add_dashboard_widgets() {
 wp_add_dashboard_widget('welcome_dashboard_widget', '常用任務', 'welcome_dashboard_widget_function');
}

// Hook into the 'wp_dashboard_setup' action to register our other functions

add_action('wp_dashboard_setup', 'add_dashboard_widgets' );

endif;

修改 WordPress 後台常用任務下拉菜單

在 WordPress 後台管理界面的右上角有一個常用任務下拉菜單,但其中不是所有項都是我們常用到的,甚至是根本不會用到的,那麼就來把它們去掉吧,當然另外可以加上自己常用的菜單。

代碼如下:

if ( ! function_exists( 'custom_favorite_actions' ) ) :
/**
 * modify favorite actions
 */
function custom_favorite_actions($actions) {
// remove menus
 unset($actions['edit-comments.php']);
 unset($actions['media-new.php']);
// add a menu link to profile.php
$actions['profile.php'] = array('My Profile', "edit_posts");
 return $actions;
}

add_filter('favorite_actions', 'custom_favorite_actions');

endif;

至於刪除 $actions 變量中的哪一項,查看一下這個變量值就知道了

Array
(
    [edit.php?post_type=post] => Array
        (
            [0] => Posts
            [1] => edit_posts
        )    [post-new.php] => Array
        (
            [0] => New Post
            [1] => edit_posts
        )

    [edit.php?post_status=draft] => Array
        (
            [0] => Drafts
            [1] => edit_posts
        )

    [post-new.php?post_type=page] => Array
        (
            [0] => New Page
            [1] => edit_pages
        )

    [media-new.php] => Array
        (
            [0] => Upload
            [1] => upload_files
        )

    [edit-comments.php] => Array
        (
            [0] => Comments
            [1] => moderate_comments
        )

)

修改 WordPress 後台左側的導航功能菜單

WordPress 後台某些功能根本用不到的話,不如直接在導航菜單中隱藏掉,代碼如下:

if ( ! function_exists( 'remove_menus' ) ) :
/**
 * Remove dashboard menus
 */
function remove_menus () {
global $menu;
    $restricted = array(__('Dashboard'),  __('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');

endif;

以上代碼隱藏了所有的功能菜單,各位可以根據自己的實際需要將需要顯示出來的在上面的代碼中刪除即可。

修改 WordPress 後台頁腳提示信息

代碼如下:

if ( ! function_exists( 'modify_footer_admin' ) ) :
/**
 * modify dashboard footer
 */
function modify_footer_admin () {
echo 'Modified by Maple Nan';
}

add_filter('admin_footer_text', 'modify_footer_admin');

endif;

隱藏 WordPress 後台自動升級提示

代碼如下:

/**
 * hide WordPress update
 */
add_filter( 'pre_site_transient_update_core', create_function( '$a', "return null;" ) );

通過以上幾步的操作,我們就可以打造出一個符合建站要求的 WordPress 後台界面了。注意這些代碼都是放置在 WordPress 的模板函數 functions.php 裏面的。