20 PHP Snippets for Making the WordPress Admin Easier for Clients


By

Out-of-the-box, WordPress is an easy-to-use CMS. But for your non-tech-savvy clients, it could be a nightmare, and a potential disaster for the site you have just built.

By simplifying the system and adding some quick hacks to disable or hide certain areas, you can not only help clients, you will also give yourself peace of mind by knowing that the site is safe by removing any potential disaster.

The PHP code snippets below will allow you to control almost all aspects of the WordPress Dashboard, and customize it to benefit your client’s level of tech comfort.

There are, of course, many plugins that will achieve the same function as these snippets, but with code, you get added control and security and, in most cases, will not need to be updated.

You might also like these useful .htaccess snippets, or these WordPress SQL Snippets.

Disabling WordPress Plugin Deactivation

This snippet is particularly useful if you have given a client plugin activation/deactivation privileges (allowing them to add new plugins themselves), but the site you have built requires some core plugins to function and should never be deactivated.

The code below will remove the ‘Deactivate’ links from whichever plugins you deem fundamental, as well as removing the ‘Edit’ links from all plugins.

Paste this into your functions.php:

add_filter( 'plugin_action_links', 'slt_lock_plugins', 10, 4 );
function slt_lock_plugins( $actions, $plugin_file, $plugin_data, $context ) {
  // Remove the edit link for all
  if ( array_key_exists( 'edit', $actions ) )
    unset( $actions['edit'] );
  // Remove deactivate link for crucial plugins
  if ( array_key_exists( 'deactivate', $actions ) && in_array( $plugin_file, array(
    'slt-custom-fields/slt-custom-fields.php',
    'slt-file-select/slt-file-select.php',
    'slt-simple-events/slt-simple-events.php',
    'slt-widgets/slt-widgets.php'
  )))
    unset( $actions['deactivate'] );
  return $actions;
}

Disabling WordPress Theme Changing

Just like the Plugin Disabling code above, you really don’t want your clients tinkering or experimenting with any theme changes. The code below will remove the ‘Appearance’ menu option from the Dashboard.

Paste this snippet into your functions.php:

add_action( 'admin_init', 'slt_lock_theme' );
function slt_lock_theme() {
  global $submenu, $userdata;
  get_currentuserinfo();
  if ( $userdata->ID != 1 ) {
    unset( $submenu['themes.php'][5] );
    unset( $submenu['themes.php'][15] );
  }
}

Disable Top-Level Menus from the WordPress Admin Panel

Depending on your client, you may need to take disabling/hiding WordPress menu options even further. With this snippet, you can hide whichever Top-Level Menu (Posts, Media, Links, Tools…) you need to.

Paste this into your functions.php:

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

Disable Submenus from the WordPress Admin Panel

Maybe your client doesn’t need the drastic action taken by the above snippets, and only needs some key sub-menu items disabled/hidden. This code will help.

Paste this into your functions.php:

function remove_submenus() {
  global $submenu;
    unset($submenu['index.php'][10]); // Removes 'Updates'.
    unset($submenu['themes.php'][5]); // Removes 'Themes'.  
    unset($submenu['options-general.php'][15]); // Removes 'Writing'.
    unset($submenu['options-general.php'][25]); // Removes 'Discussion'.       
}
add_action('admin_menu', 'remove_submenus');

The menu-ID:s are found in wp-admin/menu.php.

Restrict WordPress Admin Menu Items Based on Username

Say you want to restrict client access to certain Top-Level menu items, but you still want to maintain the full menu for the main administrator. This snippet will help.

Replace ‘clients-username’ and paste this code into your functions.php:

function remove_menus()
{
    global $menu;
    global $current_user;
    get_currentuserinfo();

    if($current_user->user_login == 'clients-username')
    {
        $restricted = array(__('Posts'),
                            __('Media'),
                            __('Links'),
                            __('Pages'),
                            __('Comments'),
                            __('Appearance'),
                            __('Plugins'),
                            __('Users'),
                            __('Tools'),
                            __('Settings')
        );
        end ($menu);
        while (prev($menu)){
            $value = explode(' ',$menu[key($menu)][0]);
            if(in_array($value[0] != NULL?$value[0]:"" , $restricted)){unset($menu[key($menu)]);}
        }// end while

    }// end if
}
add_action('admin_menu', 'remove_menus');

Remove WordPress Meta-Boxes from Posts & Pages Editor Screens

Publishing posts and pages are more than likely key to your client’s business and probably the main reason you chose WordPress for them in the first place. To help avoid any confusion from within the posts/pages editor screens, it could be helpful to remove unused meta-boxes (custom fields, recent comments, post tags…).

Paste this into your functions.php and edit as required:

function remove_extra_meta_boxes() {
remove_meta_box( 'postcustom' , 'post' , 'normal' ); // custom fields for posts
remove_meta_box( 'postcustom' , 'page' , 'normal' ); // custom fields for pages
remove_meta_box( 'postexcerpt' , 'post' , 'normal' ); // post excerpts
remove_meta_box( 'postexcerpt' , 'page' , 'normal' ); // page excerpts
remove_meta_box( 'commentsdiv' , 'post' , 'normal' ); // recent comments for posts
remove_meta_box( 'commentsdiv' , 'page' , 'normal' ); // recent comments for pages
remove_meta_box( 'tagsdiv-post_tag' , 'post' , 'side' ); // post tags
remove_meta_box( 'tagsdiv-post_tag' , 'page' , 'side' ); // page tags
remove_meta_box( 'trackbacksdiv' , 'post' , 'normal' ); // post trackbacks
remove_meta_box( 'trackbacksdiv' , 'page' , 'normal' ); // page trackbacks
remove_meta_box( 'commentstatusdiv' , 'post' , 'normal' ); // allow comments for posts
remove_meta_box( 'commentstatusdiv' , 'page' , 'normal' ); // allow comments for pages
remove_meta_box('slugdiv','post','normal'); // post slug
remove_meta_box('slugdiv','page','normal'); // page slug
remove_meta_box('pageparentdiv','page','side'); // Page Parent
}
add_action( 'admin_menu' , 'remove_extra_meta_boxes' );

Remove WordPress Pages Columns

Adding this code allows you to remove whichever column from the ‘Pages’ page you feel is not necessary to your client.

Edit as required and paste this snippet into your functions.php:

function remove_pages_columns($defaults) {
  unset($defaults['comments']); 
  return $defaults;    
} 
add_filter('manage_pages_columns', 'remove_pages_columns');

Remove WordPress Posts Columns

Similar to the snippet above, this code will remove columns from the posts page.

Edit as required and paste this code into your functions.php:

function remove_post_columns($defaults) {
  unset($defaults['comments']);
  return $defaults;    
} 
add_filter('manage_posts_columns', 'remove_post_columns');

Removing Default Widgets from the WordPress Dashboard

The majority of the default Dashboard widgets may not be necessary to your non-tech client and could potentially be a disastrous distraction. This snippet will remove whichever widget you define from the Dashboard.

Paste this into your functions.php:

// Create the function to use in the action hook
function example_remove_dashboard_widgets() {
  // Globalize the metaboxes array. This holds all the widgets for wp-admin
 
  global $wp_meta_boxes;
 
  // Remove the incoming 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' );

To view each widget’s call, have a look at the Dashboard Widgets API page on the Codex.

Create Personalized WordPress Dashboard Widgets

When building themes for clients, it could be useful and appreciated to create a personalized ‘Welcome’ widget on the Dashboard. This snippet will create a simple ‘Hello World’ widget and can be easily edited to your own specifications.

Paste this code into your functions.php:

// Create the function to output the contents of our Dashboard Widget
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' );

Add, Remove & Reorder Dashboard Widgets By Role

You may have a client that needs the WordPress Dashboard customized depending on user roles by restricting editors or authors to all of the admins details. This code will get rid of the ‘Incoming Links’ widget for authors and editors and then clean up some of the other boxes for everyone.

Paste this into your functions.php:

function tidy_dashboard()
{
  global $wp_meta_boxes, $current_user;
 
  // remove incoming links info for authors or editors
  if(in_array('author', $current_user->roles) || in_array('editor', $current_user->roles))
  {
    unset($wp_meta_boxes['dashboard']['normal ']['core']['dashboard_incoming_links']);
  }
   
  // remove the plugins info and news feeds for everyone
  unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_plugins']);
  unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']);
  unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_secondary']);
 
}
//add our function to the dashboard setup hook
add_action('wp_dashboard_setup', 'tidy_dashboard');

Here’s a list of how to unset each of the current default dashboard widgets:

//Right Now - Comments, Posts, Pages at a glance
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_right_now']);
//Recent Comments
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_recent_comments']);
//Incoming Links
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_incoming_links']);
//Plugins - Popular, New and Recently updated WordPress Plugins
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_plugins']);

//WordPress Development Blog Feed
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']);
//Other WordPress News Feed
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_secondary']);
//Quick Press Form
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_quick_press']);
//Recent Drafts List
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_recent_drafts']);

Remove Author Metabox/Options & Move to Publish MetaBox

This code will remove the Author MetaBox and Screen Options and then add those options into the publish metabox.

Paste this into your functions.php:

// MOVE THE AUTHOR METABOX INTO THE PUBLISH METABOX
add_action( 'admin_menu', 'remove_author_metabox' );
add_action( 'post_submitbox_misc_actions', 'move_author_to_publish_metabox' );
function remove_author_metabox() {
    remove_meta_box( 'authordiv', 'post', 'normal' );
}
function move_author_to_publish_metabox() {
    global $post_ID;
    $post = get_post( $post_ID );
    echo '<div id="author" class="misc-pub-section" style="border-top-style:solid; border-top-width:1px; border-top-color:#EEEEEE; border-bottom-width:0px;">Author: ';
    post_author_meta_box( $post );
    echo '</div>';
}

Add or Remove Links From the WordPress Admin Bar

The WordPress Admin Bar gives you quick access to all of the main areas of your site, like creating a new post or page, moderating comments, or modifying widgets. The snippets below will allow you to add or remove any links.

This snippet will add links to the admin bar. Paste into your functions.php:

function my_admin_bar_link() {
  global $wp_admin_bar;
  if ( !is_super_admin() || !is_admin_bar_showing() )
    return;
  $wp_admin_bar->add_menu( array(
  'id' => 'diww',
  'parent' => 'my-blogs',
  'title' => __( 'Title of the link you want to add'),
  'href' => admin_url( 'http://mysitesurl.com/wp-admin.php' )
  ) );
}
add_action('admin_bar_menu', 'my_admin_bar_link');

This snippet will remove links from the admin bar. Paste into your functions.php:

function remove_admin_bar_links() {
  global $wp_admin_bar;
  $wp_admin_bar->remove_menu('my-blogs');
  $wp_admin_bar->remove_menu('my-account-with-avatar');
}
add_action( 'wp_before_admin_bar_render', 'remove_admin_bar_links' );

Show an Urgent Message in the WordPress Admin

This snippet will allow you to show a custom message to any logged-in user. Particularly useful if you need to inform a client/user that they have done something wrong.
Firstly, paste this into your functions.php:

/**
 * Generic function to show a message to the user using WP's
 * standard CSS classes to make use of the already-defined
 * message color scheme.
 *
 * @param $message The message you want to tell the user.
 * @param $errormsg If true, the message is an error, so use
 * The red message style. If false, the message is a status
  * message, so use the yellow information message style.
 */
function showMessage($message, $errormsg = false)
{
  if ($errormsg) {
    echo '<div id="message" class="error">';
  }
  else {
    echo '<div id="message" class="updated fade">';
  }

  echo "<p><strong>$message</strong></p></div>";
}    

Next, add a hook to the admin notices function to show your custom message:

/**
 * Just show our message (with possible checking if we only want
 * to show the message to certain users.
 */
function showAdminMessages()
{
    // Shows as an error message. You could add a link to the right page if you wanted.
    showMessage("You need to upgrade your database as soon as possible...", true);

    // Only show to admins
    if (user_can('manage_options') {
       showMessage("Hello admins!");
    }
}

/**
  * Call showAdminMessages() when showing other admin
  * messages. The message only gets shown in the admin
  * area, but not on the frontend of your WordPress site.
  */
add_action('admin_notices', 'showAdminMessages');

Hide the WordPress Upgrade Message

You may have clients who do not want to deal with any maintenance updates. For those clients using this snippet, you can hide the WordPress Upgrade Message.

Paste this into your functions.php:

add_action('admin_menu','wphidenag');
function wphidenag() {
remove_action( 'admin_notices', 'update_nag', 3 );
}

Simpler WordPress Login URL

The default URL for logging into your WordPress-powered site is: http://yoursite.com/wp-login.php. To make things easier, or at least more memorable, for a client, you will need a cleaner URL like: http://yoursite.com/login

Paste this code in your .htaccess file before the default WordPress rewrite stuff:

RewriteRule ^login$ http://yoursite.com/wp-login.php [NC,L]

Change the Dashboard Footer Text

When building a site for a client, it can be useful to be able to customize the dashboard footer text. This little snippet will do the job.

Edit “Your own text” and paste this into your functions.php:

function remove_footer_admin () {
    echo "Your own text";
} 

add_filter('admin_footer_text', 'remove_footer_admin');

When building themes for clients, one of their expectations may be to have some sort of company branding within WPs admin. The first page a client will ever view is the login screen, and it’s very easy to customize by simply replacing the default WordPress logo with your client’s company logo.

The new logo should be 326×82 pixels, and copy it to your themes ‘images’ folder.

Edit ‘companylogo.png’ and paste this code into your functions.php:

// login page logo
function custom_login_logo() {
  echo '<style type="text/css">h1 a { background: url('.get_bloginfo('template_directory').'/companylogo.png) 50% 50% no-repeat !important; }</style>';
}
add_action('login_head', 'custom_login_logo');

The next step in customizing WPs backend is to replace the Dashboards logo. You will need to create a transparent (.gif or .png) image of 30x31px. Then, save that image in your theme’s image folder (/wp-content/themes/theme-name/images) and name it whatever you like.

Edit ‘custom-logo.gif’ and paste this snippet into your functions.php:

//hook the administrative header output
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>
';
}

.

Related Topics


Top
This page may contain affiliate links. At no extra cost to you, we may earn a commission from any purchase via the links on our site. You can read our Disclosure Policy at any time.