定制WordPress后台的几个技巧

通过一些代码可以定制一个自己喜欢的wordpress后台,本文所述代码都应写在主题的functions.php中.

移除后台某些菜单

如果你先让后台用不到的菜单隐藏掉可以将你想移除的菜单添加到$restricted数组中即可:

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');

更换登陆/注册页面的Logo

function my_custom_login_logo() {
    echo '
<style type="text/css">
        .login h1 a { background-image:url('.get_bloginfo('template_directory').'/images/custom-login-logo.gif) !important; }
    </style>
  
';
}
  
add_action('login_head', 'my_custom_login_logo');

更换Dashboard(仪表盘)的Logo

add_action('admin_head', 'my_custom_logo');
  
function my_custom_logo() {
   echo '
   <style type="text/css">
   #wp-admin-bar-wp-logo > .ab-item .ab-icon 
    { background-image: url('.get_bloginfo('template_directory').'/images/custom-logo.gif) !important; }
   </style>';
}

删除“Please Upgrade Now”升级提示

# 2.3 to 2.7:
add_action( 'init', create_function( '$a', "remove_action( 'init', 'wp_version_check' );" ), 2 );
add_filter( 'pre_option_update_core', create_function( '$a', "return null;" ) );
 
# 2.8 to 3.0:
remove_action( 'wp_version_check', 'wp_version_check' );
remove_action( 'admin_init', '_maybe_update_core' );
add_filter( 'pre_transient_update_core', create_function( '$a', "return null;" ) );
 
# 3.0:
add_filter( 'pre_site_transient_update_core', create_function( '$a', "return null;" ) );

删除Dashboard的Widget

如果你不想整天读WordPress官方的新闻,不想看见Dashboard到处是盒子,用下面的代码移除。虽然WordPress的屏幕选项也能实现差不多的效果,但从屏幕选项里去掉选择只不过是隐藏了这些widget,东西都在,只是你看不见罢了。用代码则可以禁止这些widget加载。

function remove_dashboard_widgets() {
    global $wp_meta_boxes;
  
    unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_quick_press']);
    unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_incoming_links']);
    unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_right_now']);
    unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_plugins']);
    unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_recent_drafts']);
    unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_recent_comments']);
    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' );

创建自己的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' );

结束语

wordpress提供高可定制性的服务,让我们满足各种需求.这里介绍的这是简单的几例,更多的定制性等待您的开发.

发表评论