Ultimate WordPress Functions.php Examples And Tutorials
Posted on 07. Jun, 2011 by Alex Denning in Coding
The functions.php file is one of the most useful files in your theme. If you’re developing lots of themes, the chances are you’ve got some sort of framework either that you use yourself or is published by a third party. This is a collection of snippets you can put in your functions.php file and they’ll perform a number of handy functions which makes your life running WordPress just that bit easier.
Not for a second am I going to claim I wrote these myself. There are a couple of sources I must site before we go any further! A number of these are taken from: StackExchange’s excellent collection, a post on the same topic Jeff Starr put together last year on Digging into WordPress and Jean-Baptiste Jean’s WPRecipes
Remove update notification for non-admins
When a new version of WordPress is available, users who aren’t an admin get a nice notice telling them they should notify the site administrator that a new version of WordPress is available. If you’re reading this, the chances are you’ll know anyway when a new version of WordPress is out and thus you don’t need all your users telling you.
The following will remove the notification for all users apart from administrators.
- global $user_login;
- get_currentuserinfo();
- if ($user_login !== "admin") { // change admin to the username that gets the updates
- add_action( 'init', create_function( '$a', "remove_action( 'init', 'wp_version_check' );" ), 2 );
- add_filter( 'pre_option_update_core', create_function( '$a', "return null;" ) );
- }
Remove un-needed content from the wp_head
The wp_head template tag adds a lot of “junk”. Remove it with the following!
- remove_action('wp_head', 'rsd_link');
- remove_action('wp_head', 'wp_generator');
- remove_action('wp_head', 'feed_links', 2);
- remove_action('wp_head', 'index_rel_link');
- remove_action('wp_head', 'wlwmanifest_link');
- remove_action('wp_head', 'feed_links_extra', 3);
- remove_action('wp_head', 'start_post_rel_link', 10, 0);
- remove_action('wp_head', 'parent_post_rel_link', 10, 0);
- remove_action('wp_head', 'adjacent_posts_rel_link', 10, 0);
Stop “read more” jumps
When you click on “read more”, it automatically drops to the point in the article you theoretically just got to the end of.
It’s a bit of a pain, though. Remove it with:
- function no_more_jumping($post) {
- return '<a class="read-more" href="'.get_permalink($post->ID).'">'.'Continue Reading'.'</a>';
- }
- add_filter('excerpt_more', 'no_more_jumping');
Allow contributors to upload files
There’s an understandable security risk allowing people you don’t trust to publish content to upload media, but more often than not it’s a pain that contributors can’t upload files.
Fix it with the following.
- if ( current_user_can('contributor') && !current_user_can('upload_files') )
- add_action('admin_init', 'allow_contributor_uploads');
- function allow_contributor_uploads() {
- $contributor = get_role('contributor');
- $contributor->add_cap('upload_files');
Automatically enable threaded comments
Threaded comments aren’t on by default. This can be fixed with the following.
- function enable_threaded_comments(){
- if (!is_admin()) {
- if (is_singular() AND comments_open() AND (get_option('thread_comments') == 1))
- wp_enqueue_script('comment-reply');
- }
- }
- add_action('get_header', 'enable_threaded_comments');
Customise the admin section footer
Powered by WordPress? Powered by you! Change the footer text to whatever you want; especially useful on client sites.
- echo 'Gotta love <a href="http://wpshout.com">WPShout</a>';
- }
- add_filter('admin_footer_text', 'custom_admin_footer');
Make custom post types searchable
WordPress’ search isn’t great at the best of times and doesn’t include custom post types by default.
You can add your custom post types to WordPress’ search with the following.
- function searchAll( $query ) {
- if ( $query->is_search ) { $query->set( 'post_type', array( 'site','plugin', 'theme','person' )); }
- return $query;
- }
- add_filter( 'the_search_query', 'searchAll' );
Remove plugin update nag for inactive plugins
This is a pet hate of mine: the number that displays next to updates includes inactive plugins. You can change it so that you only get the number for active plugins with the following:
- function update_active_plugins($value = '') {
- /*
- The $value array passed in contains the list of plugins with time
- marks when the last time the groups was checked for version match
- The $value->reponse node contains an array of the items that are
- out of date. This response node is use by the 'Plugins' menu
- for example to indicate there are updates. Also on the actual
- plugins listing to provide the yellow box below a given plugin
- to indicate action is needed by the user.
- */
- if ((isset($value->response)) && (count($value->response))) {
- // Get the list cut current active plugins
- $active_plugins = get_option('active_plugins');
- if ($active_plugins) {
- // Here we start to compare the $value->response
- // items checking each against the active plugins list.
- foreach($value->response as $plugin_idx => $plugin_item) {
- // If the response item is not an active plugin then remove it.
- // This will prevent WordPress from indicating the plugin needs update actions.
- if (!in_array($plugin_idx, $active_plugins))
- unset($value->response[$plugin_idx]);
- }
- }
- else {
- // If no active plugins then ignore the inactive out of date ones.
- foreach($value->response as $plugin_idx => $plugin_item) {
- unset($value->response);
- }
- }
- }
- return $value;
- }
- add_filter('transient_update_plugins', 'update_active_plugins'); // Hook for 2.8.+
- //add_filter( 'option_update_plugins', 'update_active_plugins'); // Hook for 2.7.x
Stop your blog trackbacking itself
Another pet hate is trackbacks appearing when you’ve linked to something on your own site. Stop this happening with the following.
- function no_self_ping( &$links ) {
- $home = get_option( 'home' );
- foreach ( $links as $l => $link )
- if ( 0 === strpos( $link, $home ) )
- unset($links[$l]);
- }
- add_action( 'pre_ping', 'no_self_ping' );
Use shortcodes in widgets
Widgets can’t use shortcodes… unless you’ve got a handy snippet!
- if ( !is_admin() ){
- add_filter('widget_text', 'do_shortcode', 11);
- }
If you’ve got any personal favourites, let me know in the comments!

Enjoyed the post? We'll see you on Twitter or in your RSS reader!

WPShout is hosted by the fine folks at WPWebHost.
You can get exactly the same hosting as WPShout has for $7.95/month with WPWebHost's Freedom Plan.
Plus get 30% off the Freedom Plan with the code WPSHOUT.
Alex Denning is the founder of WPShout. A WordPress developer from London, Alex is a keen musician and freelance writer and developer.
You can find Alex on Twitter.
11 Responses to “Ultimate WordPress Functions.php Examples And Tutorials”
Trackbacks/Pingbacks
[...] to receive a response.Need to know what to put in that Functionality Plugin we created? Check out WPShouts Ultimate Functions Resource.The bbPress WordPress forum plugin is in the final stages of beta. Sweet. Can’t wait to try [...]
[...] to Alex Denning for the cool tip! If you enjoyed this article, please consider sharing it! tweetmeme_style = [...]
[...] → Source: http://wpshout.com/wordpress-functions-php/ [...]

Leon
07. Jun, 2011
These are great yet so simple.
the shortcode in widgets & searchable custom post types are really great!
Xps
07. Jun, 2011
I actually prefer my own trackbacks listed. If I think Article A is useful enough to include in Article B, maybe readers of Article A will also be interested in reading Article B. Increased user navigation between relevant articles is a good thing.
I will be playing with the non-admin update notification because I don’t like announcing the system isn’t updated.
Alex Denning
09. Jun, 2011
There are some advantages, sure, but I find it a pain. It’s the *publish article*… *ooh! An email!* *ohwait* that annoys me
Paul
08. Jun, 2011
instead use this argument when declaring a CPT : ‘exclude_from_search’ => false,
also it’s wprecipes.com
Alex Denning
09. Jun, 2011
I can’t spell. Thanks
Leon
08. Jun, 2011
I also use this one to disbale autosaving:
function disable_autosave() {
wp_deregister_script(‘autosave’);
}
add_action( ‘wp_print_scripts’, ‘disable_autosave’ );
Alex Denning
09. Jun, 2011
I happen to quite like autosaving, but I can see some won’t. Thanks for sharing
shawn
28. Dec, 2011
I receive a fatal error when using the plugin update snippet
mod_fcgid: stderr: PHP Parse error: syntax error, unexpected ‘-’, expecting ‘,’ or ‘)’ in /home/
Using wp 3.3 if that matters