How You Can Use WordPress Functions to Run a Smarter Blog

We bloggers have it rough. We live in a world full of more WordPress themes than you can shake a stick at, yet most of us are still tied to one old, worn out template. It’s unfortunate, but let’s be honest here—changing themes is a huge ordeal.

Generally speaking, whenever you opt for a new theme, you’re going to have to do a fair amount of customization to get everything just right for your site. By the time you get to your third or fourth theme, you’ll probably begin to grow a little weary of reinventing the wheel with each new design.

Fortunately, savvy users like you can solve this problem by creating a user-defined functions file that will work with any WordPress theme. The idea here is to place all of your common, customized design elements—like an author bio or a sales widget for your sidebar, for instance—within functions that reside in a separate, non-theme file. This serves a twofold purpose:

  1. First, because your common design tweaks are isolated in a non-theme file, you won’t risk overwriting them whenever you change or upgrade your theme.
  2. Second, using a separate functions file creates a much more organized and less intimidating environment for customizing your theme. You can add and subtract HTML from your user-defined functions file without fear of “breaking” your theme, simply because you aren’t trying to edit the theme files directly.

Before jumping into an example, I’d also like to point out that you can (and should) combine your user-defined functions file with a custom CSS stylesheet. Together, these two files act like a preferences panel for your site, allowing you to easily incorporate1 your most common changes into any WordPress theme.

In the following example, you’ll create your own user-defined functions file containing one sample function. Once you’ve done that, you’ll use your new function in tandem with a custom stylesheet to output a del.icio.us bookmark link at the end of each post.

Step 1: Download Your User-defined Functions File

Download user-functions.zipYour new functions file, user-functions.php, is just a normal PHP file that can be as simple or as complex as you like. Since we’re going to be working with a common example today, I’ve gone ahead and created a sample user-functions.php file that you can download here. Please keep in mind that you can use this same file for your own user-defined functions in the future (with or without the example function).

Step 2: Write Your Own Function(s)

As I mentioned earlier, the goal of our example is to output a handy bookmarking link immediately after each post. Although our sample function is included in the user-functions.php download file, it warrants a more detailed explanation here. We’ll start by taking a detailed look at the function, userfunc_bookmark_links():

function userfunc_bookmark_links() {
   global $post;
?>
<ul class="bookmark_links">
    <li><a href="http://del.icio.us/post?url=<?php the_permalink() ?>&amp;title=<?php urlencode(the_title()) ?>" title="Bookmark this post on del.icio.us">Bookmark this article on del.icio.us</a></li>
</ul>
<?php
}

The first thing to note here is the function name. Intrinsically, it makes sense to give the function a simple name like bookmark_links(), but in order to ensure compatibility with all WordPress plugins and themes, it’s best to add a consistent prefix to your own function names. Because all of these particular functions are user-defined, it makes sense to use something like userfunc for the prefix.

From a coding standpoint, userfunc_bookmark_links() is extremely basic. It contains no interior logic, and the only thing the function actually does is output some HTML. Also, because this function pulls in the $post variable, it will only work when called from within the WordPress loop.

Most of your user-defined functions should end up looking and behaving like our sample function here. For practical purposes, you’ll probably want to output custom HTML at various points throughout your theme, and you’ll find that user-functions.php is a fantastic way to accomplish this.

Oh, and in case you were wondering, there is no limit to the number of functions you can define in your file, so be sure to go nuts with this.

Step 3: Activating Your New Functions File

Once you’ve created your functions file, the next step is to activate it within your theme. Begin by uploading user-functions.php to your active theme folder. Next, open your theme’s functions.php file for editing2, and then add the following line of code (you can place it anywhere):

include_once (TEMPLATEPATH . '/user-functions.php');

After editing your theme’s functions.php file, simply save it and upload it back to your server. At this point, your user-defined functions will be available for use within your theme.

Step 4: How to Use Your New Functions

Although you’ve activated your new functions file, you won’t notice any difference on your site until you actually call one of your functions from within a standard theme file.

In our example, the goal is to output a bookmarking link at the end of each post3. In order to do that, you’ll need to open up your theme’s single.php file, and locate the call to the following WordPress function:

<?php the_content(); ?>

As you might have guessed, the_content() outputs a fully-formatted blog post. Because our goal is to include bookmarking links after the post, it only makes sense to place the call to your new, user-defined function immediately after the call to the_content(). Here’s how the code in single.php should look once you’ve inserted the call to userfunc_bookmark_links():

<?php
    the_content();
    userfunc_bookmark_links();
?>

If you’ve done everything correctly, when you visit a post’s permalink page, you’ll see that the post content is now followed by a clever little del.icio.us bookmark link.

Your next challenge is to style your new bookmarking links, and hopefully, you’ll find this to be a simple and straightforward task. By default, the list has been given a class name of bookmark_links, and you can use that class in your custom stylesheet to target this set of links directly.

Bonus Styles for Thesis Users

Are you a Thesis Theme user? If so, then you’ll want to try out this snippet of CSS on your site. Simply add the following declaration to your custom.css file, and boom—you’ll get instant, em-based goodness:

.custom ul.bookmark_links { 
    list-style: none;
    margin: 3.14286em 0 1.57143em 0;
    padding: 0.57143em 0.78571em;
    background: #e7f8fb;
    border: 0.07143em solid #9ad5df;
}

The Bottom Line about WordPress Functions

I hinted at it earlier, but I definitely meant it—there really is no limit to what you can accomplish with abstracted WordPress functions like those you’ll define in your user-functions.php file. By taking advantage of this rock-solid coding practice, you’ll be able to inject customized, actionable items into any theme with ease.

One idea that immediately comes to mind is the creation of your own widgets (think sales boxes, special links, product descriptions, etc.). If you define functions for your most commonly-used widgets, you’ll be able to call them at any point in your theme’s code. This makes it much easier to test how certain elements will look on different parts of the page, which is useful for designers and amateur code-wranglers alike.

As a theme architect, I’m always trying to come up with solutions that make life a little easier and a little more bulletproof for users. Ultimately, though, nothing is more bulletproof than a savvy user, and that’s precisely why you’ll benefit from implementing your own user-defined functions!

1 I’m the type who likes to split an infinitive every now and then to say things a little more clearly. Teacher hates it, but I don’t care.

2 Your theme doesn’t have a functions.php file? Burn it, and then check out Thesis. You’ll love it, and you’ll receive added benefit from my posts in the form of Thesis-targeted advice and code.

3 For the sake of clarity, I have chosen to isolate the single.php file in this tutorial. You should know, however, that the information here applies perfectly to other theme files as well, such as index.php, archive.php, and search.php (assuming your theme has all of those files).