Making use of WordPress functions.php

As you know, WordPress comes with an option of putting in some code snippets in functions.php file and it will bring its magic into action. Lets explore this option and how you can use it for good.

Every theme comes with a functions.php which is executed on each page load. If its not there, you can just add one. Now what file does is, that you can put on any code in it and WordPress will execute it. Remember several tutorials telling you to add this snippet of code in your functions.php file? Yeah! That’s what we are talking about.

You might ask what else we can use it for? The answer is you can put into code snippets in this file to alter the functionality of WordPress and even for things you have been using so many tiny plugins and as a bonus improve performance. Many people would prefer to use a separate plugin to give them a certain feature instead of dealing with the copy-paste of some code, but the performance gain comes as an advantage to it. If you are shy from code, it might be a good time to try out some of it but if you have a phobia for code, then better keep going with the “add another plugin” approach as its better to use more plugins than to break your site.

Now that you might be using it or tempted to use it, I will give you some tips to manage it better.

Always leave comments about what this code snippet do and how you can make small edits into it.

For example:

/**
 * Function - Duplicate Content Cure
 */
function ashfame_dcc()
{
	if( ( is_single() || is_page() || is_home() || is_category() ) && ( !is_paged() ) )
		echo '<meta name="robots" content="index,follow" />';
	else
		echo '<meta name="robots" content="noindex,follow" />';
}
add_action('wp_head','ashfame_dcc');

So it will make you easy for you to manage things around, when you have tens of them in your functions.php

Using WordPress 3.0 goodies such as menu, theme background, adding support for post thumbnails etc all require you to add lines in functions.php

You can copy paste this functions.php to any other theme or any other site where you want to imply all the hacks you have in place.

Small plugins can be moved to this file. This largely depends on how much you can play with code but this is good in terms of performance as WordPress will now have to look for less number of files on disk to generate a page, thus reducing access to the slowest component of a computer i.e. disk.

Basically the idea is to reduce the number of files so that it can find code at one place instead of looking into several places.

I have been using functions.php for managing some custom functions that I call in my theme, a function for duplicate content cure (instead of a plugin), Yoast’s breadcrumb function & several other ad related functions so that I can just switch over to another ad by just changing the call to a different function. this way I don’t have to deal with maintaining the ad codes.

For Reference:

/**
 * Function - Yoast's Breadcrumb
 */
function the_breadcrumb()
{
	echo '<ul id="crumbs">';
	if (!is_home()) {
		echo '<li><a href="';
		echo get_option('home');
		echo '">';
		echo 'Home';
		echo "</a></li>";
		if (is_category() || is_single()) {
			echo '<li>';
			the_category(' </li><li> ');
			if (is_single()) {
				echo "</li><li>";
				the_title();
				echo '</li>';
			}
		} elseif (is_page()) {
			echo '<li>';
			echo the_title();
			echo '</li>';
		}
	}
	elseif (is_tag()) {single_tag_title();}
	elseif (is_day()) {echo"<li>Archive for "; the_time('F jS, Y'); echo'</li>';}
	elseif (is_month()) {echo"<li>Archive for "; the_time('F, Y'); echo'</li>';}
	elseif (is_year()) {echo"<li>Archive for "; the_time('Y'); echo'</li>';}
	elseif (is_author()) {echo"<li>Author Archive"; echo'</li>';}
	elseif (isset($_GET['paged']) && !empty($_GET['paged'])) {echo "<li>Blog Archives"; echo'</li>';}
	elseif (is_search()) {echo"<li>Search Results"; echo'</li>';}

	echo '</ul>';
}

So tighten up yourself for some work and relax in the coming time. 🙂

Use functions.php at its full potential and if you have any questions, comments section is all yours.


Comments

4 responses to “Making use of WordPress functions.php”

  1. […] am zipping it up as a plugin but you can copy paste the code inside your functions.php file and you should be […]

  2. […] In case, you don’t want to download yet another plugin, you can add the following code in your functions.php file […]

  3. I’m not using wordpress or anything I’m just doing normal coding so how do I use these functions ?

    1. This post is for WordPress only! Outside WordPress you can create libraries according to set of functions and include them when needed or include a common one on every page.