WordPress Snippets for Client Websites

WordPress is becoming a very popular CMS of choice for many web developers, designers and for myself. I use it for all of my Client websites who require either a blog, allow them to make edits on their own, membership areas and more. Below is a collection of custom WordPress Snippets, hacks, customizations (whatever you want to call them) that either removes un-needed WordPress functions or builds upon them to allow my Clients a safe and pleasant experience. All of these can go into a custom plugin file you create for them or added to the theme’s functions.php file. I personally add them to my custom BNE Support plugin.

[box style=”notice”]Each snippet below shows the <?php and ?> tags (because that’s the correct way to present it and shows that it’s for PHP…). Most likely if you’re adding this into a theme’s functions.php file, those tags are already provided so only copy and paste what is between those lovely tags. [/box]

1. Hide the WP Admin bar

Some websites you may want to completely hide the WordPress Admin menu bar that is shown on the top of every page. The following script hides it for everyone who is logged in.

<?php 

     show_admin_bar(true); /* False/True */

?>

Want to hide it for everyone except for users with an Administrator role? Use this one instead:

<?php

if (!current_user_can('administrator')) :
    show_admin_bar(false);
endif;

?>

 

2. Edit WP Admin Bar Links

Remove the WP Logo, Add New Content and Update Notifications from the WP Admin menu.

<?php

function wps_admin_bar() {
    global $wp_admin_bar;
    $wp_admin_bar->remove_menu('wp-logo'); 		// Removes WP Logo
    $wp_admin_bar->remove_menu('new-content'); 	// Removes +New Links
    $wp_admin_bar->remove_menu('updates'); 		// Updates
}
add_action( 'wp_before_admin_bar_render', 'wps_admin_bar' );

?>

 

3. Remove “Help Menu” button on WP Dashboard

Feel like hiding the “existence” that the website is built with WordPress or don’t want to confuse your client with the WordPress lingo help section? Add this snippet to hide the “help” button:

<?php

function hide_help() {
    echo '<style type="text/css">
            #contextual-help-link-wrap { display: none !important; }
          </style>';
}
add_action('admin_head', 'hide_help');

?>

 

 4. Change the default From Name and Email Address

By default, WordPress will send emails with the prefix, wordpress@yourdomain.com and the Name of the website. To change the prefix of the system email address and From Name, add the following script. Make sure you change what is there with your own email address and Name.

<?php

function new_mail_from($old) {
	return 'noreply@yourwebsitedomain.com'; // From Email Address
}
add_filter('wp_mail_from', 'new_mail_from');

function new_mail_from_name($old) {
	return 'Company Name'; // From Email Name
}
add_filter('wp_mail_from_name', 'new_mail_from_name');

?>

 

5. Change the WP Dashboard footer information

The bottom of the WordPress dashboard admin screens is some information about the website. I like to add support information here instead for my Clients.

<?php

function change_footer_admin() {
	echo '<a href="/" target="_blank">View Your Website</a>  <span style="padding-left:10px">|<span style="padding-left:10px">  For Support, please call (800) 123-4567 or email <a href="mailto:support@email_address.com">support@email_address.com</a>  <span style="padding-left:10px">|<span style="padding-left:10px">  BNE Support v3.1 - 9.20.2012';
}
add_filter('admin_footer_text', 'change_footer_admin');

?>

 

6. Remove the WordPress Version Information and Update Notifications for all users except Admins

Don’t want your users (Editors) to be nagged about update notifications from WordPress or show the word “WordPress” all over your front and backend? Use this snippet.

<?php

class HideWordPressVersion {
	static function wp_version() {
		$GLOBALS['wp_version'] = rand( 99, 999 );
	}
	static function update_nag() {
		if ( ! current_user_can('update_core') )
			remove_action( 'admin_notices', 'update_nag', 3 );
	}
	static function update_right_now_message( $translation, $text ) {
		if ( 'You are using <span class="b">WordPress %s</span>.' != $text || current_user_can( 'update_core' ) )
			return $translation;
	}
	static function admin_footer() {
		if ( !current_user_can( 'update_core' ) )
			remove_filter( 'update_footer', 'core_update_footer' );
	}
	static function scripts() {
		global $wp_scripts;
		if ( !is_a( $wp_scripts, 'WP_Scripts' ) )
			return;
		foreach ( $wp_scripts->registered as $handle => $script ) {
			if ( $script->ver === false )
				$wp_scripts->registered[$handle]->ver = null;
		}
	}
	static function styles() {
		global $wp_styles;
		if ( !is_a( $wp_styles, 'WP_Styles' ) )
			return;
		foreach ( $wp_styles->registered as $handle => $style ) {
			if ( $style->ver === false )
				$wp_styles->registered[$handle]->ver = null;
		}
	}
	static function http() {
		return 'WordPress; ' . get_bloginfo( 'url' );
	}
	static function xmlrpc( $blog_options ) {
		unset( $blog_options['software_version'] );
		return $blog_options;
	}
	static function pingback( $new_useragent, $useragent ) {
		return "{$useragent} -- WordPress";
	}
	static function bloginfo( $output, $show ) {
		if ( $show != 'version' )
			return $output;
	}
}

if ( is_admin() ) {
	if ( !is_multisite() ) {
		add_action( 'admin_notices', array('HideWordPressVersion','update_nag'), 2 );
		add_action( 'update_footer', array('HideWordPressVersion','admin_footer'), 1 );
		if ( 'index.php' == $GLOBALS['pagenow'] )
			add_action( 'gettext', array('HideWordPressVersion','update_right_now_message'), 1, 2 );
	}

	add_action( 'admin_print_styles', array('HideWordPressVersion','styles'), 100 );
} else {
	add_action( 'init', array('HideWordPressVersion','wp_version'), 1 );

	add_action( 'wp_print_scripts', array('HideWordPressVersion','scripts'), 100 );
	add_action( 'wp_print_footer_scripts', array('HideWordPressVersion','scripts'), 100 );
	add_action( 'wp_print_styles', array('HideWordPressVersion','styles'), 100 );

	remove_action( 'wp_head', 'wp_generator' );
	foreach ( array( 'rss2_head', 'commentsrss2_head', 'rss_head', 'rdf_header', 'atom_head', 'comments_atom_head', 'opml_head', 'app_head' ) as $hwv_action )
		remove_action( $hwv_action, 'the_generator' );
	unset($hwv_action);

	add_filter( 'bloginfo', array('HideWordPressVersion','bloginfo'), 100, 2 );
}

add_filter( 'http_headers_useragent', array('HideWordPressVersion','http'), 100 );
add_filter( 'xmlrpc_blog_options', array('HideWordPressVersion','xmlrpc'), 100 );
add_filter( 'pingback_useragent', array('HideWordPressVersion','pingback'), 100, 2 );

?>

 

7. Remove unwanted WP Dashboard Widgets

The WP dashboard by default comes with alot of extra widgets on the main dashboard. Client websites really don’t need these so lets deactivate them.

<?php

function remove_dashboard_widgets() {
  global $wp_meta_boxes;
  unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_plugins']); 		// Plugins
  unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_incoming_links']); // Incoming Links
  unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']); 			// WordPress Blog 1
  unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_secondary']); 		// WordPress Blog 2
}
add_action('wp_dashboard_setup', 'remove_dashboard_widgets' );

?>

 

8. Allow Theme/Plugin shortcodes in TextWidgets

By default, WordPress does not provide support for shortcodes to be used inside text widgets. However, alot of times you may want to add a link button, tabs, image slideshows, and many others.

<?php

    add_filter('widget_text', 'do_shortcode');

?>

 

9. Login using an Email Address or Username

Most websites either require a username or email address to login. So lets make it easy for our Clients to remember their username by also allowing an email address instead.

<?php

function login_with_email_address($username) {
        $user = get_user_by('email',$username);
        if(!empty($user->user_login))
                $username = $user->user_login;
        return $username;
}
add_action('wp_authenticate','login_with_email_address');
function change_username_wps_text($text){
       if(in_array($GLOBALS['pagenow'], array('wp-login.php'))){
         if ($text == 'Username'){$text = 'Username / Email';}
            }
                return $text;
         }
add_filter( 'gettext', 'change_username_wps_text' );

?>

 

10. Remove unwanted default WP widgets

Most themes now provide their own widgets that do more than the default WP core widgets such as a Recent Post Widget. So why keep those in place as well. It’ll just confuse your Client of which one to use. Use the below snippet and comment out (place ” // ” on the line you wish to not activate) the ones you wish to keep activated. I usually only keep Categories, Search, Tag and Menu active as those are useful.

<?php

add_action( 'widgets_init', 'my_unregister_widgets' );
function my_unregister_widgets() {
        unregister_widget( 'WP_Widget_Pages' );
        unregister_widget( 'WP_Widget_Calendar' );
        unregister_widget( 'WP_Widget_Archives' );
        unregister_widget( 'WP_Widget_Links' );
        unregister_widget( 'WP_Widget_Categories' );
        unregister_widget( 'WP_Widget_Recent_Posts' );
        unregister_widget( 'WP_Widget_Search' );
        unregister_widget( 'WP_Widget_Tag_Cloud' );
        unregister_widget( 'WP_Widget_RSS' );
        unregister_widget( 'WP_Widget_Meta' );
        unregister_widget( 'WP_Widget_Recent_Comments' );
        unregister_widget( 'WP_Nav_Menu_Widget' );
}

?>