Select Page

The functions.php file is part of the WordPress theme structure.  It allows developers to extend WordPress using  PHP functions. Geek alert!

Most of the time it sits there in the directory.  All alone.  Forgotten… *sob*

But no more!  We lift the lid and peek inside this powerful little file and you don’t even need to be a hardcore PHP developer to reap the benefits of tinkering with using the file.

You will find the functions.php file in your current WordPress theme folder.  This is usually in /wp-content/themes/<your theme folder name>.

Those Sneaky Developers

Developers usually have a suite of tools that help them carefully craft those elegant functions.

Tosh to that – we’ll use the WordPress editor ????

In the admin dashboard click on the Appearance menu then Editor.

Your current theme will be selected by default and you will see a list of all the theme’s files down the RHS.  Find and click on the Theme Functions (functions.php) link.

Voila – your functions.php file is loaded in and suddenly you’re a developer.

We should note a word of warning first.  Before going any further forward you should make a copy of your functions.php file contents and save them somewhere in the unlikely event that you screw up the entire theme with your wonderful changes.

Quickest way to back it up is to click in the edit window and do a <CTRL>+A selecting everything, then <CTRL>+C copying it to the clipboard.  Open notepad and <CTRL>+V pasting the copy there and then save on the desktop.

Yay – 5 second backup done.  Reverse the process to restore.

At the top of the file you’ll see the code <?php and at the very end of the file you’ll see the code ?>.  These snippets tell the web server that the code in between is PHP and that’s where we’ll be placing our functions.

It’s important not to leave any spaces at all after the end ?> as this can cause a problem/blank screen on some versions of WordPress.

Hash Percentage Squiggle – WTFrak?

If you scroll through the file you’ll see lots of function blahblah(){ $squiggle; humdeedum-dum=humdee; } type blocks of code and these are the functions which extend the WordPress system.

We won’t get into how they work here, let’s just list a few code snippets, explaining what they do and let you paste them into the functions.php file.

Remember to paste them in between the <?php and ?> snippets. Development was never soooo easy.

10 Essential Code Snippets for your WordPress function.php File

Remove Rubbish from the Header

WordPress spits out a tonne of junk to the <head> section of your web page.  Let’s get rid of some of it.

[code lang=”php”]// 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);[/code]

Add a Favicon to your Website

This function will allow you to add your very own favicon to your site.  Simply upload any favicon.ico file to the root of your website.

[code lang=”php”]// add a favicon
function my_favicon() {
echo ‘<link rel=”Shortcut Icon” type=”image/x-icon” href=”’.get_bloginfo(‘wpurl’).’/favicon.ico” />’;
add_action(‘wp_head’, ‘my_favicon’);[/code]

Remove WordPress Update Nag

When a new version of WordPress is released you’ll see the message “Please update ..” on every screen within the admin dashboard.  This handy function will disable the box (officially called a “nag”).  Warning though – you’ll have to keep an eye out for new WordPress versions and apply them when appropriate.

[code lang=”php”]// remove admin update admin nag
if (!current_user_can(‘edit_users’)) {
add_action(‘init’, create_function(‘$a’, “remove_action(‘init’, ‘wp_version_check’);”), 2);
add_filter(‘pre_option_update_core’, create_function(‘$a’, “return null;”));

Detect and Target Specific Web Browsers

Have you ever needed to display, hide or format HTML content for a specific browser?  This handy code snippet will detect the type of browser and add it as a class to the body tag of the page.

[code lang=”php”]function browser_body_class($classes) {
global $is_lynx, $is_gecko, $is_IE, $is_opera, $is_NS4, $is_safari, $is_chrome, $is_iphone;

if($is_lynx) $classes[] = ‘lynx’;
elseif($is_gecko) $classes[] = ‘gecko’;
elseif($is_opera) $classes[] = ‘opera’;
elseif($is_NS4) $classes[] = ‘ns4’;
elseif($is_safari) $classes[] = ‘safari’;
elseif($is_chrome) $classes[] = ‘chrome’;
elseif($is_IE) $classes[] = ‘ie’;
else $classes[] = ‘unknown’;

if($is_iphone) $classes[] = ‘iphone’;
return $classes;

Now your body tag will have an additional CSS class to it.

[code lang=”php”]<body class=”iphone”>[/code]

Automatically Insert Content After Each Post

Sometimes you may need to add something to the end of each post.  Perhaps promoting a particular product or trying to increase more social  followers.

[code lang=”php”]function insertAfterContent($content) {
if(!is_feed() && !is_home()) {
$myTwitterUsername = “gravitationalfx”;
$content.= “<div class=’follow-me’>”;
$content.= “<h4>Did You Like This?</h4>”;
$content.= “<p>Then follow <a href=””.$myTwitterUsername.””>@”.$myTwitterUsername.” on Twitter</p>”;
$content.= “</div>”;
return $content;
add_filter (‘the_content’, ‘insertAfterContent’);[/code]

The above code outputs a new div with a link to follow our Twitter feed.

Disable Auto-Saves

WordPress will automatically save each page and post while you are editing them but it can lead to unnecessary database bloat.  Sometimes it’s useful to turn the auto-save feature off.

[code lang=”php”]function disableAutoSave(){
add_action( ‘wp_print_scripts’, ‘disableAutoSave’ );[/code]

Using WordPress to Schedule an Email

This code piggybacks on the WordPress cron service which schedules tasks within the system.

We can use it to call our own function and in this example, we’re sending an hourly email to a specific address.  You can of course define your own more useful function.

[code lang=”php”]if (!wp_next_scheduled(‘my_scheduled_task_hook’)) {
wp_schedule_event( time(), ‘hourly’, ‘my_scheduled_task_hook’ );

add_action( ‘my_scheduled_task_hook’, ‘my_scheduled_task’ );

function my_scheduled_task() {
wp_mail(‘[email protected]’, ‘Groovy Subject Line’, ‘Hello, This is a scheduled email from my WordPress website. How awesome!’);

Add a Google+ Button to Your Posts and Pages

This handy function will add the Google+ button to the bottom of all your posts and pages.

[code lang=”php”]function wpr_google_plusone($content) {
$content = $content.'<div><g:plusone size=”tall” href=”’.get_permalink().’”></g:plusone></div>’;
return $content;
add_action (‘wp_enqueue_scripts’,’wpr_google_plusone_script’);
function wpr_google_plusone_script() {
wp_enqueue_script(‘google-plusone’, ‘’, array(), null);
add_filter(‘the_content’, ‘wpr_google_plusone’);[/code]

Show the Contents of a File

Sometimes it’s handy to be able to include the contents of another file on your page or post.

This function adds a shortcut which reads the content of a URL and places it on your website.

[code lang=”php”]function display_file_func( $atts ) {
extract( shortcode_atts( array(
‘file’ => ”
), $atts ) );

if ($file!=”)
return @file_get_contents($file);

add_shortcode( ‘display_file’, ‘display_file_func’ );[/code]

To use the function, add the [display_file] shortcode to your post or page.

[code lang=”text”][display_file file=””][/code]

Show a Website Snapshot

Easily display a snapshot of another website with this handy shortcode function.

[code lang=”php”]function screen_mshot ($attributes, $content = ”, $code = ”) {
‘url’ => ”,
‘width’ => 250,
), $attributes));

$imageUrl = grab_mshot ($url, $width);

if ($imageUrl == ”) {
return ”;
} else {
$image = ‘<img src=”’ . $imageUrl . ‘” alt=”’ . $url . ‘” width=”’ . $width . ‘”/>’;
return ‘<div><a href=”’ . $url . ‘”>’ . $image . ‘</a></div>’;


function grab_mshot ($url = ”, $width = 250) {
if ($url != ”) {
return ‘’ . urlencode(clean_url($url)) . ‘?w=’ . $width;
} else {
return ”;


add_shortcode(‘browsershot’, ‘screen_mshot’);[/code]

Use the shortcode [browsershot] anywhere in your content, e.g.

[code lang=”php”][browsershot url=”” width=”300″][/code]

In Summary

Don’t be afraid to experiment with your website.

Even if you aren’t a developer there are heaps of resources and handy code snippets like those above which you can use to extend and customise your website.