WordPress控制面板的十個非常規使用技巧
來源:程序員人生 發布時間:2014-05-25 03:58:01 閱讀次數:3201次
控制面板是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', create_function( '$a', "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, 0, $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');
»來源原文
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈