Ultimate WordPress Functions.php Examples And Tutorials
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. All of these go in your functions.php file, of course!
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 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 junk from head 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:
// no more jumping for read more link 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.
// Allow constributors to upload files 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.
// enable threaded comments 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.
function custom_admin_footer() { 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.
// MAKE CUSTOM POST TYPES SEARCHABLE 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:
//Remove plugin update for inactive plugins 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.
//remove pings to self 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!
// shortcode in widgets if ( !is_admin() ){ add_filter('widget_text', 'do_shortcode', 11); } |
If you’ve got any personal favourites, let me know in the comments!
at 7:42 pm
These are great yet so simple.
the shortcode in widgets & searchable custom post types are really great!
at 8:38 pm
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.
at 11:57 am
There are some advantages, sure, but I find it a pain. It’s the *publish article*… *ooh! An email!* *ohwait* that annoys me ;)
at 10:03 am
instead use this argument when declaring a CPT : ‘exclude_from_search’ => false,
also it’s wprecipes.com
at 11:54 am
I can’t spell. Thanks ;)
at 10:17 am
I also use this one to disbale autosaving:
function disable_autosave() {
wp_deregister_script(‘autosave’);
}
add_action( ‘wp_print_scripts’, ‘disable_autosave’ );
at 11:54 am
I happen to quite like autosaving, but I can see some won’t. Thanks for sharing :)
at 11:59 am
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
at 5:35 am
hey how we can know that response item is not an active plugin . And if response item is not an active plugin then how to remove it………please post such kind of code………
thanks