HomeWordpress19 Code Snippets to make WordPress More Manageable

19 Code Snippets to make WordPress More Manageable

Our aim is to provide you information by which you can use your WordPress site more efficiently and get the maximum from this. Few days earlier we provided you some useful WordPress tweaks to change look n feel of your post and page layout plus performance improvements. In this edition, today, we are featuring 19 more code snippets to make your WordPress site more manageable and efficient.

- Advertisement -

[highlight-red]Wordpress is regarded as the simplest CMS for your blogs and business websites, but when it comes to the customization then it would be the toughest task for the non-tech savvy persons.[/highlight-red] And if you belong to that category, then it would be a nightmare for your WordPress site if you dare to change without any knowledge. Then you must go through this article and get some potential help to disabling or changing some part of your CMS for particular clients and take a piece of relaxed breath. Find some of the useful WordPress Hacks to manage your WordPress site.

 

1. Restrict 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
[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’);
[/php]
[button-red url=”http://wordpress.stackexchange.com/questions/1567/best-collection-of-code-for-your-functions-php-file” target=”_blank” position=”right”] Source [/button-red]

 

2. Removing Default Widgets from the Dashboard

This snippet will remove whichever widget you define from the Dashboard.
[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 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’ );
[/php]
[button-red url=”” target=”_blank” position=”right”] Source [/button-red]

 

3. Show an Urgent Message in the WP Admin

This snippet will allow you to shows a custom message to any logged in user
[php]
/**
* Generic function to show a message to the user using WP’s
* standard CSS classes to make use of the already-defined
* message colour 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>";
}
[/php]

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

[php]
/**
* Just show our message (with possible checking if we only want
* to show 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’);
[/php]
[button-red url=”http://www.wprecipes.com/how-to-show-an-urgent-message-in-the-wordpress-admin-area” target=”_blank” position=”right”] Source [/button-red]

 

4. Hide the WordPress Upgrade Message

Using this snippet, you can hide the WordPress Upgrade Message.
[php]
add_action(‘admin_menu’,’wphidenag’);
function wphidenag() {
remove_action( ‘admin_notices’, ‘update_nag’, 3 );
}
[/php]
[button-red url=”http://www.wpbeginner.com/wp-tutorials/how-to-hide-the-wordpress-upgrade-message-in-the-dashboard/” target=”_blank” position=”right”] Source [/button-red]

 

5. Remove Meta-Boxes from Posts & Pages Editor Screens

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 etc.)
[php]
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’ );
[/php]
[button-red url=”http://webdesignfan.com/customizing-the-wordpress-admin-area/” target=”_blank” position=”right”] Source [/button-red]

 

6. Create Personalized Dashboard Widgets

This snippet will create a simple ‘Hello World’ widget, and can be easily edited to your own specifications.
[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’ );
[/php]
[button-red url=”http://hankis.me/modifying-the-wordpress-dashboard” target=”_blank” position=”right”] Source [/button-red]

 

7. Disabling Plugin Deactivation

The code below will remove the ‘Deactivate’ links from whichever plugins you deem fundamental as well as removing the ‘Edit’ links from all plugins. Add the code to the functions.php file and save it.
[php]
add_filter( ‘plugin_action_links’, ‘slt_lock_plugins’, 10, 4 );
function slt_lock_plugins( $actions, $plugin_file, $plugin_data, $context ) {
// Remove 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;
}
[/php]
[button-red url=”http://sltaylor.co.uk/blog/disabling-wordpress-plugin-deactivation-theme-changing/” target=”_blank” position=”right”] Source [/button-red]

 

8. Add, Remove & Reorder Dashboard Widgets By Role

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.
[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’);
[/php]

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

[php]
//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’]);
[/php]
[button-red url=”http://erisds.co.uk/wordpress/snippet-wordpress-admin-tidy-dashboard-widgets-by-role” target=”_blank” position=”right”] Source [/button-red]

 

9. Simpler Login URL

To make things easier, or at least more memorable, for a client you will need a cleaner URL like: http://xyz.com/login/ Paste this code in your .htaccess file before the default WordPress rewrite stuff.
[php]
RewriteRule ^login$ http://yoursite.com/wp-login.php [NC,L]
[/php]
[button-red url=”http://digwp.com/2011/01/simpler-login-url/” target=”_blank” position=”right”] Source [/button-red]

 

10. Remove Pages Columns

This code allows you to remove whichever column from the ‘Pages’ page you feel are not necessary to your client.
[php]
function remove_pages_columns($defaults) {
unset($defaults[‘comments’]);
return $defaults;
}
add_filter(‘manage_pages_columns’, ‘remove_pages_columns’);
[/php]
[button-red url=”http://wp-snippets.com/remove-pages-columns/” target=”_blank” position=”right”] Source [/button-red]

 

11. Disabling Theme Changing

The code below will remove the ‘Appearance’ menu option from the Dashboard.
[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] );
}
}
[/php]
[button-red url=”http://sltaylor.co.uk/blog/disabling-wordpress-plugin-deactivation-theme-changing/” target=”_blank” position=”right”] Source [/button-red]

 

12. Change the Dashboard Footer Text

It can be useful to be able to customize the dashboard footer text. This little snippet will do the job.
[php]
function remove_footer_admin () {
echo "Your own text";
}

add_filter(‘admin_footer_text’, ‘remove_footer_admin’);
[/php]
[button-red url=”http://www.wprecipes.com/wordpress-tip-how-to-change-the-dashboard-footer-text” target=”_blank” position=”right”] Source [/button-red]

 

13. Remove Author Metabox/Options & Move to Publish MetaBox

This code will remove the Author MetaBox and Screen Options and then add those option into the publish metabox.
[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>’;
}
[/php]
[button-red url=”http://wordpress.stackexchange.com/questions/1567/best-collection-of-code-for-your-functions-php-file?page=2&tab=votes#tab-top” target=”_blank” position=”right”] Source [/button-red]

 

14. Change the WP Login 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
[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’);
[/php]
[button-red url=”http://blogs.sitepoint.com/make-wordpress-easier-for-clients-branding/” target=”_blank” position=”right”] Source [/button-red]

 

15. Remove Posts Columns

This code will remove columns from the posts page.
[php]
function remove_post_columns($defaults) {
unset($defaults[‘comments’]);
return $defaults;
}
add_filter(‘manage_posts_columns’, ‘remove_post_columns’);
[/php]
[button-red url=”” target=”_blank” position=”right”] Source [/button-red]

 

16. Disable Top-Level Menus from the Admin Panel

With this snippet you can hide whichever Top-Level Menu (Posts, Media, Links, Tools…) you need to.
[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’);
[/php]
[button-red url=”http://www.wprecipes.com/how-to-remove-menus-in-wordpress-dashboard” target=”_blank” position=”right”] Source [/button-red]

 

17. Disable Submenus from the Admin Panel

Your client doesn’t need the drastic action taken by the above snippets, and only needs some key sub-menu items disabled/hidden.
[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’);
[/php]
[button-red url=”http://wp-snippets.com/disable-submenus-from-admin-panel/” target=”_blank” position=”right”] Source [/button-red]

 

18. Adding a Custom Dashboard 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.
[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>
‘;
}
[/php]
[button-red url=”http://www.wpbeginner.com/wp-themes/adding-a-custom-dashboard-logo-in-wordpress-for-branding/” target=”_blank” position=”right”] Source [/button-red]

 

19. Add or Remove Links to the New WP Admin Bar

The snippets below will allow you to add or remove any links.
[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’);
[/php]

[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’ );
[/php]
[button-red url=”http://www.doitwithwordpress.com/customize-wordpress-admin-bar/” target=”_blank” position=”right”] Source [/button-red]

- Advertisement -
Bono
Bono
A Computer Applications Geek, hooked on all things pertaining to, intent on delivering you the best in sports.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -

Most Popular