Keep logged in on WordPress for a longer period
A very handy code snippet here, which allows you to stay logged in for a longer period of time. Paste the code to functions.php
and update, if needed, the amount of seconds to stay logged in on line 3.
add_filter( 'auth_cookie_expiration', 'stay_logged_in_for_1_year' ); function stay_logged_in_for_1_year( $expire ) { return 31556926; // 1 year in seconds }
Source: Labnol
Remove dashboard menus
When building a WordPress blog for a client, it can be a good idea to remove access to some dashboard menus in order to avoid future problems such as the client “accidentally” deleting the custom theme they paid for.
Paste the following code in the functions.php
file from your theme directory. The following example will remove all menus named in the $restricted
array.
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');
Source: Hungred
Require a featured image before you can publish posts
If your blog layout is set to display a featured image, it can be useful to prevent post publishing if the post doesn’t have a featured image set.
This code has to be pasted into your functions.php
file.
add_action('save_post', 'wpds_check_thumbnail'); add_action('admin_notices', 'wpds_thumbnail_error'); function wpds_check_thumbnail( $post_id ) { // change to any custom post type if( get_post_type($post_id) != 'post' ) return; if ( ! has_post_thumbnail( $post_id ) ) { // set a transient to show the users an admin message set_transient( "has_post_thumbnail", "no" ); // unhook this function so it doesn't loop infinitely remove_action('save_post', 'wpds_check_thumbnail'); // update the post set it to draft wp_update_post(array('ID' => $post_id, 'post_status' => 'draft')); add_action('save_post', 'wpds_check_thumbnail'); } else { delete_transient( "has_post_thumbnail" ); } } function wpds_thumbnail_error() { // check if the transient is set, and display the error message if ( get_transient( "has_post_thumbnail" ) == "no" ) { echo "<div id='message' class='error'><p><strong>You must add a Featured Image before publishing this. Don't panic, your post is saved.</strong></p></div>"; delete_transient( "has_post_thumbnail" ); } }
Source: WP Snipp
Replace dashboard logo with yours
Just as a client will love to see their own logo on WordPress login page, there’s no doubt that they’ll enjoy viewing it on the dashboard too.
Simply copy the code below and paste it to your functions.php
file.
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>'; }
Source: Smashing Magazine
Remove dashboard widgets
Introduced in WordPress 2.7, dashboard widgets can be pretty useful. For example, some can display your Google Analytics stats. Though, sometimes you don’t need it, or at least don’t need some of them.
The code below will allow you to remove WordPress’ dashboard widgets once you paste it in your functions.php file.
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']); } // Hook into the 'wp_dashboard_setup' action to register our function add_action('wp_dashboard_setup', 'example_remove_dashboard_widgets' );
Source: Hankis
Define your own login logo
Although it doesn’t have any importance for the blog performance or usability, most clients will be very happy to see their own logo on the dashboard login page, instead of the classic WordPress logo.
The Custom admin branding plugin can do that for you, as well as the following hack that you just have to paste in your functions.php file.
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');
Source: WPRecipes
Add custom widgets to WordPress dashboard
With the previous example, I showed you how easy it is to remove unwanted dashboard widgets. The good news is that creating your own widgets isn’t hard either.
The well-commented code below should be self-explanatory. Just insert it in your functions.php
, as usual.
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'); } // Hook into the 'wp_dashboard_setup' action to register our other functions add_action('wp_dashboard_setup', 'example_add_dashboard_widgets' );
Source: Source
Change WordPress dashboard colors
If you ever wanted to be able to change WordPress dashboard colors (as well as font or even display) without having to edit WordPress core files, you’ll like this hack for sure.
The following example features a basic style change (grey header is replaced by a blue one) but you can easily add as many styles as you wish within the <style>
and </style>
tags.
function custom_colors() { echo '<style type="text/css">#wphead{background:#069}</style>'; } add_action('admin_head', 'custom_colors');
Provide custom help messages
If you’re building a site for a client and they have problems with some parts of the dashboard, a good idea is to provide contextual help to the client.
The following hack will allow you to add a custom help messages for the blog admin. As usual, you only have to paste the code into your functions.php
file.
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' );
Source: Studio Grasshopper
Remove dashboard widgets according to user role
If you’re owning a multi-user blog, it may be useful to know how to hide some dashboard widgets to keep confidential information in a safe place.
The following code will remove the postcustom meta box for “author” (role 2). To apply the hack on your own blog, just copy the code below and paste it in your functions.php file.
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');
Source: Smashing Magazine
Reduce amount of post revisions
Post revisions are very useful, but they also clutter your database a lot. In order to save some space, you can consider limiting the amount of post revisions automatically saved by WordPress.
This code has to be pasted in your wp-config.php
file, located at the root of your WordPress install.
define( 'WP_POST_REVISIONS', 3 );
Source: WordPress Codex
Disable WordPress Login Hints
When you (or someone else) fails to login on your WordPress dashboard, an error message is displayed. While this can be useful to legit users, it can also give precious information to a potential hijacker.
Paste the code below into your functions.php
file to prevent login error messages to be displayed.
function no_wordpress_errors(){ return 'GET OFF MY LAWN !! RIGHT NOW !!'; } add_filter( 'login_errors', 'no_wordpress_errors' );
Source: Labnol