Ultimate WordPress Functions.php Examples And Tutorials

Posted on 07. Jun, 2011 by 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.

// REMOVE THE WORDPRESS UPDATE NOTIFICATION FOR ALL USERS EXCEPT ADMIN
  1.    global $user_login;
  2.    get_currentuserinfo();
  3.    if ($user_login !== "admin") { // change admin to the username that gets the updates
  4.     add_action( 'init', create_function( '$a', "remove_action( 'init', 'wp_version_check' );" ), 2 );
  5.     add_filter( 'pre_option_update_core', create_function( '$a', "return null;" ) );
  6.    }

Remove un-needed content from the wp_head

The wp_head template tag adds a lot of “junk”. Remove it with the following!

// remove junk from head
  1. remove_action('wp_head', 'rsd_link');
  2. remove_action('wp_head', 'wp_generator');
  3. remove_action('wp_head', 'feed_links', 2);
  4. remove_action('wp_head', 'index_rel_link');
  5. remove_action('wp_head', 'wlwmanifest_link');
  6. remove_action('wp_head', 'feed_links_extra', 3);
  7. remove_action('wp_head', 'start_post_rel_link', 10, 0);
  8. remove_action('wp_head', 'parent_post_rel_link', 10, 0);
  9. 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:

// no more jumping for read more link
  1. function no_more_jumping($post) {
  2.  return '<a class="read-more" href="'.get_permalink($post->ID).'">'.'Continue Reading'.'</a>';
  3. }
  4. 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.

// Allow constributors to upload files
  1. if ( current_user_can('contributor') &amp;&amp; !current_user_can('upload_files') )
  2.     add_action('admin_init', 'allow_contributor_uploads');
  3.  
  4. function allow_contributor_uploads() {
  5.     $contributor = get_role('contributor');
  6.     $contributor-&gt;add_cap('upload_files');

Automatically enable threaded comments

Threaded comments aren’t on by default. This can be fixed with the following.

// enable threaded comments
  1. function enable_threaded_comments(){
  2.  if (!is_admin()) {
  3.   if (is_singular() AND comments_open() AND (get_option('thread_comments') == 1))
  4.    wp_enqueue_script('comment-reply');
  5.   }
  6. }
  7. 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.

function custom_admin_footer() {
  1.  echo 'Gotta love <a href="http://wpshout.com">WPShout</a>';
  2. }
  3. 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.

// MAKE CUSTOM POST TYPES SEARCHABLE
  1. function searchAll( $query ) {
  2.  if ( $query-&gt;is_search ) { $query-&gt;set( 'post_type', array( 'site','plugin', 'theme','person' )); }
  3.  return $query;
  4. }
  5. 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:

//Remove plugin update for inactive plugins
  1.  
  2. function update_active_plugins($value = '') {
  3.     /*
  4.     The $value array passed in contains the list of plugins with time
  5.     marks when the last time the groups was checked for version match
  6.     The $value-&gt;reponse node contains an array of the items that are
  7.     out of date. This response node is use by the 'Plugins' menu
  8.     for example to indicate there are updates. Also on the actual
  9.     plugins listing to provide the yellow box below a given plugin
  10.     to indicate action is needed by the user.
  11.     */
  12.     if ((isset($value-&gt;response)) &amp;&amp; (count($value-&gt;response))) {
  13.  
  14.         // Get the list cut current active plugins
  15.         $active_plugins = get_option('active_plugins');
  16.         if ($active_plugins) {
  17.  
  18.             //  Here we start to compare the $value-&gt;response
  19.             //  items checking each against the active plugins list.
  20.             foreach($value-&gt;response as $plugin_idx =&gt; $plugin_item) {
  21.  
  22.                 // If the response item is not an active plugin then remove it.
  23.                 // This will prevent WordPress from indicating the plugin needs update actions.
  24.                 if (!in_array($plugin_idx, $active_plugins))
  25.                     unset($value-&gt;response[$plugin_idx]);
  26.             }
  27.         }
  28.         else {
  29.              // If no active plugins then ignore the inactive out of date ones.
  30.             foreach($value-&gt;response as $plugin_idx =&gt; $plugin_item) {
  31.                 unset($value-&gt;response);
  32.             }
  33.         }
  34.     }
  35.     return $value;
  36. }
  37. add_filter('transient_update_plugins', 'update_active_plugins');    // Hook for 2.8.+
  38. //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.

//remove pings to self
  1. function no_self_ping( &amp;$links ) {
  2.     $home = get_option( 'home' );
  3.     foreach ( $links as $l =&gt; $link )
  4.         if ( 0 === strpos( $link, $home ) )
  5.             unset($links[$l]);
  6. }
  7. add_action( 'pre_ping', 'no_self_ping' );

Use shortcodes in widgets

Widgets can’t use shortcodes… unless you’ve got a handy snippet!

// shortcode in widgets
  1. if ( !is_admin() ){
  2.     add_filter('widget_text', 'do_shortcode', 11);
  3. }

If you’ve got any personal favourites, let me know in the comments!

Follow on Twitter! Subscribe!

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.

Visit WPWebHost

Alex's Gravatar

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”

  1. Leon

    07. Jun, 2011

    These are great yet so simple.
    the shortcode in widgets & searchable custom post types are really great!

    Reply to this comment
  2. 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.

    Reply to this comment
    • 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 ;)

      Reply to this comment
  3. Paul

    08. Jun, 2011

    instead use this argument when declaring a CPT : ‘exclude_from_search’ => false,

    also it’s wprecipes.com

    Reply to this comment
  4. 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’ );

    Reply to this comment
    • Alex Denning

      09. Jun, 2011

      I happen to quite like autosaving, but I can see some won’t. Thanks for sharing :)

      Reply to this comment
  5. 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

    Reply to this comment

Trackbacks/Pingbacks

  1. [...] 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 [...]

  2. [...] to Alex Denning for the cool tip! If you enjoyed this article, please consider sharing it! tweetmeme_style = [...]

Leave a Reply

Please use your real name when commenting. Wrap code in <code> tags and make sure HTML is encoded. You can use regular HTML like <a href="... etc.

Get yours questions answered quicker

If you're using this post for paid work and have a question of any complexity then please ask at WPQuestions where you'll get a prompt response.