我們使用 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 裡面的。