WordPress控制面板的十个非常规使用技巧

WordPress控制面板的十个非常规使用技巧,第1张

概述控制面板是WordPress博客的重要组成部分。 登录WordPress后你首先看到的就是博客后台的控制板,你可以在这里管理你博客上的所有文章、版块布局以及其它各种条目。这里我们要介绍的是十个WordPress控制面板的非常规使用技巧。删除控制板上的某个菜单有各种各样的原因可能会让用户想要删除博客后台的…

控制面板是wordpress博客的重要组成部分。 登录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

尽管登录logo对改善博客性能和可用性都没有多大帮助,但当你登录时看到的是一个不同于wordpress传统登录logo的logo时,心里总会有些异样的满足感吧。

Custom admin branding插件可以帮你实现这种效果,此外你也可以通过在functions.PHP文件里粘贴下面的代码来达到需要的效果。


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

add_action('login_head','my_custom_login_logo');
»来源

替换后台logo

既然已经重设了登录页面的logo,那么想不想也换换后台控制板上的logo呢?

同样只需要把下面的代码复制到functions.PHP文件。


add_action('admin_head','my_custom_logo');

function my_custom_logo() {
   echo '<style type="text/CSS">
         #header-logo { background-image: url('.get_bloginfo('template_directory').'
/images/custom-logo.gif) !important; }</style>';
}
»来源

关闭“请升级”提醒

wordpress新版本发布得相当频繁。 如果你不想隔三差五地看到这样的提示,或者你是在为客户开发某个WordPress网站并认为客户不必看到升级提醒,那么请把下面的代码复制到functions.PHP文件。更新文件后wordpress的升级提醒就不会继续在你面前晃荡了。


if ( !current_user_can( 'edit_users' ) ) {
  add_action( 'init',create_function( '$a',"remove_action( 'init','wp_version_check' );" ),2 );
  add_filter( 'pre_option_update_core',"return null;" ) );
}
»来源

删除控制面板Widget

Widget是使wordpress功能得以扩展的一大功臣。尽管如此,有时候我们并不需要Widget,至少并不需要其中一些Widget。

在functions.PHP里加入下面的代码可以帮你删除控制面板的Widget:


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

»来源

在控制面板里添加自定义Widget

上面的方法说的是怎样删除控制板中不需要的Widget, 而下面我们就来看看怎么创建新的Widget。

下面的代码应该不难明白,同样只需要复制到functions.PHP文件里。 同样只需要复制到functions.PHP文件里。


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控制面板换色

在不修改wordpress核心文件的情况下,也可以改变wordpress控制面板的颜色甚至字体或者版式。

下面的代码实现了一次基本的样式更改(将原本的灰色header变成蓝色),只要你愿意,其它的更改同样不在话下(在<style>和</style>标签之间进行编辑)。


function custom_colors() {
   echo '<style type="text/CSS">#wphead{background:#069}</style>';
}

add_action('admin_head','custom_colors');

提供帮助性信息

如果你是在帮助其他人开发WordPress网站,在控制面板的某些部分加上“帮助”说明必然是相当贴心的举动。

在functions.PHP文件里加上下面的代码, 让你的博客后台出现一些帮助说明类的温馨提示。


function my_admin_help($text,$screen) {
// Check we're only on my Settings page
if (strcmp($screen,MY_PAGEHOOK) == 0 ) {

$text = 'Here is some very useful information to help you use this plugin...';
return $text;
}
// Let the default WP Dashboard help stuff through on other admin pages
return $text;
}

add_action( 'contextual_help','my_admin_help' );
»来源

在控制面板中监视服务器状况

wordpress的控制面板API大大加强了控制板Widget的实用性。有这样一个强大的Widget可以实现在wordpress控制面板中直接监视服务器运行状态的功能。

也只需要在functions.PHP文件里加入下面的代码:


function slt_PHPErrorsWidget() {
    $logfile = '/home/path/logs/PHP-errors.log'; // Enter the server path to your logs file here
    $displayErrorslimit = 100; // The maximum number of errors to display in the Widget
    $errorLengthlimit = 300; // The maximum number of characters to display for each error
    $fileCleared = false;
    $userCanClearLog = current_user_can( 'manage_options' );
    // Clear file?
    if ( $userCanClearLog && isset( $_GET["slt-PHP-errors"] ) && $_GET["slt-PHP-errors"]=="clear" ) {
        $handle = fopen( $logfile,"w" );
        fclose( $handle );
        $fileCleared = true;
    }
    // Read file
    if ( file_exists( $logfile ) ) {
        $errors = file( $logfile );
        $errors = array_reverse( $errors );
        if ( $fileCleared ) echo '<p><em>file cleared.</em></p>';
        if ( $errors ) {
            echo '<p>'.count( $errors ).' error';
            if ( $errors != 1 ) echo 's';
            echo '.';
            if ( $userCanClearLog ) echo ' [ <b><a href="'.get_bloginfo("url").'/wp-admin/?slt-PHP-errors=clear"
onclick="return confirm('Are you sure?');">CLEAR LOG file</a></b> ]';
            echo '</p>';
            echo '<div ID="slt-PHP-errors" style="height:250px;overflow:scroll;padding:2px;background-color:
#faf9f7;border:1px solID #ccc;">';
            echo '<ol style="padding:0;margin:0;">';
            $i = 0;
            foreach ( $errors as $error ) {
                echo '<li style="padding:2px 4px 6px;border-bottom:1px solID #ececec;">';
                $errorOutput = preg_replace( '/[([^]]+)]/','<b>[$1]</b>',$error,1 );
                if ( strlen( $errorOutput ) > $errorLengthlimit ) {
                    echo substr( $errorOutput,$errorLengthlimit ).' [...]';
                } else {
                    echo $errorOutput;
                }
                echo '</li>';
                $i++;
                if ( $i > $displayErrorslimit ) {
                    echo '<li style="padding:2px;border-bottom:2px solID #ccc;"><em>More than '.$displayErrorslimit.'
errors in log...</em></li>';
                    break;
                }
            }
            echo '</ol></div>';
        } else {
            echo '<p>No errors currently logged.</p>';
        }
    } else {
        echo '<p><em>There was a problem reading the error log file.</em></p>';
    }
}

// Add Widgets
function slt_dashboardWidgets() {
    wp_add_dashboard_Widget( 'slt-PHP-errors','PHP errors','slt_PHPErrorsWidget' );
}
add_action( 'wp_dashboard_setup','slt_dashboardWidgets' );


»来源

根据用户角色移除相应的Widget

针对多用户博客。

下面的代码会将Meta框postcustom从“作者”角色的控制板上删除。 同样你只需要在functions.PHP文件里加上下面的代码:


function customize_Meta_Boxes() {
//retrIEve current user info
global $current_user;
get_currentuserinfo();

//if current user level is less than 3,remove the postcustom Meta Box
if ($current_user->user_level < 3)
remove_Meta_Box('postcustom','post','normal');
}

add_action('admin_init','customize_Meta_Boxes');
»来源

原文

总结

以上是内存溢出为你收集整理的WordPress控制面板的十个非常规使用技巧全部内容,希望文章能够帮你解决WordPress控制面板的十个非常规使用技巧所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/zz/1003155.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存