11 Code Snippets to Customize Your WordPress Admin Dashboard

0
11009

WordPress has very strong admin control panel to manage your WordPress website. There are so many WP plugins to customize your admin dashboard, some for to change the look n feel of the dashboard and some for the functionality. But major drawback of these plugins are their bulky nature, these make your WordPress site heavier in load time. So it would be better option not to use so many WordPress plugins. Then what should be the better alternative to these plugins? If you are a developer then you must be aware of ‘function.php’ file, it contains definition of all working WordPress functionality.

 

wordpress code snippets and hacks

 

Today, we are going to provide you 11 very useful code snippets to modify ‘function.php’ file to customize admin dashboard of your WordPress site. You just need to add these codes to the ‘function.php’ file; no more knowledge of core development is needed.

 

Remove items from your WordPress Dashboard’s Menu

[sourcecode language=”php”]
// 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’);
[/sourcecode]

 

List all Dashboard Widgets Within the Dashboard Itself

[sourcecode language=”php”]
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’);
[/sourcecode]

 

Editing the Footer Text in your Dashboard

[sourcecode language=”php”]
function remove_footer_admin () {
echo "Your own text";
}
add_filter(‘admin_footer_text’, ‘remove_footer_admin’);
[/sourcecode]

 

Changing the Colour of WordPress Dashboard’s Header

[sourcecode language=”php”]
// 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’);
[/sourcecode]

 

Adding a Custom Widget in Your WordPress Dashboard

[sourcecode language=”php”]
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’;
}
[/sourcecode]

 

Error Logging in WordPress Dashboard

[sourcecode language=”php”]
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’ );
[/sourcecode]

 

Removing the Logo From the Dashboard

[sourcecode language=”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/logo.jpg) !important; }</style>’;
}
[/sourcecode]

 

Add a Custom News Feed to the WordPress Dashboard

[sourcecode language=”php”]
// 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>";
}
[/sourcecode]

 

Removing the WordPress Logo from the Login Page

[sourcecode language=”php”]
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’);
[/sourcecode]

 

Disable Non-Default Dashboard Widgets

[sourcecode language=”php”]
function disable_default_dashboard_widgets() {
remove_meta_box(‘widget-name’, ‘dashboard’, ‘normal’);
}
add_action(‘admin_menu’, ‘disable_default_dashboard_widgets’);
[/sourcecode]

 

Remove the Upgrade WordPress Banner for Non-Admin Users

[sourcecode language=”php”]
// 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;" ) );
}
[/sourcecode]

 

***Bonus WordPress Plugins***


 
 

White Label CMS

This plugin gives the necessary freedom to WordPress administrators who can surprise their clients with a totally new WordPress dashboard.
Download

 

Adminimize

Adminimize takes WordPress administration to next level by letting the administrator control literally every feature of WordPress from the dashboard itself.
Download