11 个用于定制WordPress管理仪表盘的代码片段

摘要:11 个用于定制WordPress管理仪表盘的代码片段

Remove items from your WordPress Dashboard’s Menu

// removing appearance, users and plugins options from menu Items in WordPress Dashboard
function wp_admin_dashboard_remove_menus() {
    global $menu;
    $restricted = array(__('Appearance'), __('Users'), __('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', 'wp_admin_dashboard_remove_menus');

 

List all Dashboard Widgets Within the Dashboard Itself

function list_active_dashboard_widgets() {
    global $wp_meta_boxes;
    foreach (array_keys($wp_meta_boxes['dashboard']['normal']['core']) as $name) {
	echo '<div>' . $name . '</div>';
    }
}
add_action('wp_dashboard_setup', 'list_active_dashboard_widgets');

 

Editing the Footer Text in your Dashboard

function remove_footer_admin () {
    echo "Your own text";
}
add_filter('admin_footer_text', 'remove_footer_admin');

 

Changing the Colour of WordPress Dashboard’s Header

// Editing the WordPress Dashboard Header
function wp_admin_dashboard_header_colour() {
echo '<style type="text/css">#wphead{background:#000000;
background-image: -webkit-gradient(linear, 0% 100%, 0% 0%, from(#7684hg), to(#730fvk));
}
#wphead h1#site-heading a{color: #ggg;}
#wphead h1#site-heading a:hover{color: #fff;}
#wphead #wphead-info #user_info{color: #ggg;}
#wphead #wphead-info #user_info a{color: #ggg;}
#wphead #wphead-info #user_info a:hover{color: #fff;}
</style>';
}
add_action('admin_head', 'wp_admin_dashboard_header_colour');

 

Adding a Custom Widget in Your WordPress Dashboard

add_action('wp_dashboard_setup', 'custom_dashboard_widgets');
function custom_dashboard_widgets() {
    global $wp_meta_boxes;
    wp_add_dashboard_widget('custom_ad_widget', 'MyAds', 'custom_dashboard_ad');
}

function custom_dashboard_ad() {
    echo '<p> Here is my widget.</p><br /> And one more line';
}

 

Error Logging in WordPress Dashboard

function slt_PHPErrorsWidget() {
	$logfile = '<root>/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' );

 

Removing the Logo From the Dashboard

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/logo.jpg) !important; }</style>';
}

 

Add a Custom News Feed to the WordPress Dashboard

// A news feed widget for your WordPress dashboard
function wp_admin_dashboard_add_news_feed_widget() {
    global $wp_meta_boxes;
    // The new widget
    wp_add_dashboard_widget( 'dashboard_new_feed', 'News of Your Choice', 'dashboard_my_feed_output' );
}
add_action('wp_dashboard_setup', 'wp_admin_dashboard_add_news_feed_widget');
function dashboard_my_feed_output() {
    echo '<div>';
    wp_widget_rss_output(array(
        'url' => 'http://www.mywebsite.com/feed/',
        'title' => 'Latest news of my choice',
        'items' => 2,
        'show_summary' => 1,
        'show_author' => 0,
        'show_date' => 1
));
echo "</div>";
}

 

Removing the WordPress Logo from the Login Page

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

 

Disable Non-Default Dashboard Widgets

function disable_default_dashboard_widgets() {
    remove_meta_box('widget-name', 'dashboard', 'normal');
}
add_action('admin_menu', 'disable_default_dashboard_widgets');

 

Remove the Upgrade WordPress Banner for Non-Admin Users

// Removing the "Please Update Now" notice for non-admin users
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;" ) );
    add_filter( 'pre_site_transient_update_core', create_function( '$a', "return null;" ) ); 
}

扩展阅读

20个精心挑选新鲜好用的前端开发工具
WordPress.com 推出新版 Windows 桌面应用
15个开发人员的PHP资源库
2014年国人开发的最热门的开源软件TOP 100
推荐11个强大流行的WordPress插件

为您推荐

20个你应该知道的有用HTML5代码段
如何像Python高手(Pythonista)一样编程
11 个用于定制WordPress管理仪表盘的代码片段
OpenBSD 5.6 发布
5个实用的CSS代码开发片段

更多

WordPress
开源软件