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:
- 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.
- 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
Your 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; ?> <?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 smoothly. 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). ↩
72 Comments ↓
Thanks for providing the definitive guide to keeping form and function separate! This will make maintaining website designs on multiple sites much easier!
Now… if you could only recommend a good version labeling tool that would work on Windows!
Hey that’s an awesome idea… until now, I’ve been moving functions and pieces of code from one functions.php to another… this is way better. Thanks for the tip!
I so love you right now!!! This is the best article I have read in a long time… Certainly got my wheels turning. Thank you so much!
This is great! Thanks a lot. I’ve been using your custom.css technique since I first discovered your site for a set of blogs I manage that need common elements. This goes hand in hand with that. I’m a php guy, so I don’t know why I haven’t made this leap already. I just tend to think of WP (especially since we are using MU) as a beast I shouldn’t poke too much.
So… you going to tell us Thesis theme users what this code is, or do we have to just try it out ourselves?
Chris, another incredibly useful post! Thank you.
I definitely need to do this to clean up some of my “hacks” in Thesis (namely the bookmarking and social coding you used as an example).
As an aside, any place that my social bookmarking code has an
&, I replace it with&so that it validates in W3c. So far as I can tell, this does not bork the social coding and does result in perfect validation. Have you found instances where this is a bad practice, rather than a good one? I have heard, for example, that doing this sort of thing with Amazon Affiliate coding breaks the affiliate link code, so that one does not get a commission on a sale. Not sure if this is true or not. If the practice is not advisable in your view, I would not hesitate to stop doing it.Thanks again. Truly a superb article (which I submitted to del.ic.ious BTW)
Good idea about separating hacks from functions.php - things should get easier.
Maybe I’ll write a follow-up tutorial on how to write more hacks for user-functions.php
Simple Mom — Just try out the code for now. The next release of Thesis is going to have this functionality built-in, so you’ll become more and more familiar with this style of customization in the weeks ahead.
Bruce — Those ampersands should definitely be encoded, and it’s pretty sad that I didn’t do that before releasing this post. Here’s what the W3C has to say on the topic:
I’ve fixed this in both the post and the download file, so I guess you might say that I’m no longer an invalid.
Sumesh — It makes sense that there will be many user functions that will be common to a lot of blogs. Because of this, I’m thinking of setting up a user function repository where people can submit, download, and explore functions that can help them run a better blog.
[...] How You Can Use WordPress Functions to Run a Smarter Blog [...]
Why not slap in a plugin comment header and make it a simple plugin? That way you don’t have to keep copying it around into different theme folders or including it in templates after you activate it in the plugin manager.
[...] is back with a great post on How You Can Use WordPress Functions to Run a Smarter Blog. Note to self - read and [...]
Chris,
Excellent post! I’m brand new to blogging and WordPress. I’ve been using your Cutline 1.2 theme (I love it!) and been hacking away with sidebar widgets,etc…
From my understanding of this post, I can simply add all of the hacks I’ve included into the sidebar.php into the user-functions. php and it will allow seamless theme changes without losing functionality. Is this correct?
In other words, I can change from Cutline 1.2 to your new Thesis theme and keep the same functionality of these hacks by simply placing the saved user-functions.php into the Thesis theme folder? If so, you’re a God-send!
I haven’t upgraded to Cutline 1.3 because I’m afraid I’ll lose everything I’ve created upon uploading the newer version.
Thanks for your reply and help!
Dr. Mac
On a related subject, do you know about the WordPress Theme Toolkit? It can be used the same way to preserve both custom functions and an admin menu integrated into your blog management area. You should like it
Dr. Mac — You asked:
Yes, that’s exactly right, but do keep in mind that you’ll probably want to style your hacks a little differently in each theme. Personally, I recommend using a custom stylesheet in this situation, simply because it’s the safest and easiest way to make styling changes.
The bottom line here is that if you isolate your most common code changes in an abstracted functions file, you’ll be able to use those functions like a toolkit with any theme.
[...] How You Can Use WordPress Functions to Run a Smarter Blog — Pearsonified (tags: business code) [...]
[...] How You Can Use WordPress Functions to Run a Smarter Blog — Pearsonified (tags: wordpress tips blogging php howto themes customize design) [...]
Like Doug Martin said, wouldn’t it be simpler to just make a plugin out of all your theme hacks?
Either way, still a great idea.
[...] How You Can Use WordPress Functions to Run a Smarter Blog — Pearsonified Nice concept, considering I’m undergoing a theme change right now. (tags: wordpress tips tricks php customize) [...]
Rock on as you always say.
When you gonna’ put out one of your “Tubetorial-esque” videos to compliment these posts? Seeing this in action would get some serious video hit juice (revver.com / youtube / ThemeTorial.com?).
Regards
Shane
[...] How You Can Use WordPress Functions to Run a Smarter Blog — Pearsonified - Use your own functions to make Wordpress better. [...]
[...] How You Can Use WordPress Functions to Run a Smarter Blog — Pearsonified (tags: webdesign php wordpress) [...]
[...] WordPress themes a lot? If you do, Chris Pearson has taken the time to write a great post about using WordPress functions to ease the transition between themes. The idea is to put all your design elements in one file [...]
[...] How You Can Use WordPress Functions to Run a Smarter Blog — Pearsonified - [...]
[...] - How You Can Use WordPress Functions to Run a Smarter Blog “The idea here is to place all of your common, customized design elements—like an author [...]
I’m not a techie but even I can understand and implement it..THXS !
Dave
This is really good, when I design templates they are usually not with a lot of custom PHP, so this is a great thing to start doing. This is my first comment here and I love your themes by the way. Keep it up.
[...] Funktionen und Sicherheitsaktualisierungen lieber selbst verwalten möchte, erfährt bei Chris Pearson, wie man eigene (Standard-) WordPress-Funktionen auslagern, gestalten und nach Theme-Wechsel wieder [...]
[...] Read full article Tags: tutorial [...]
[...] How You Can Use WordPress Functions to Run a Smarter Blog [...]
[...] So are user-defined WordPress functions. [...]
Hey Chris, I’m a Thesis user and will add this to my new theme. Thanks for making life a little easier for me.
Oh, I have some questions for you concerning the Thesis. Well, I need help making some adjustments to the rotating images and what not.
How would I go about sending you a more detailed question? I’m fairly new to the game and the CSS coding world. Thanks again! I love my new thesis theme!
By the way, the site with the new theme has not been launched yet.
Miguel — Welcome to the Thesis community! The forums are really becoming a great resource for common theme solutions, and better yet, they’re full of savvy users who are quick to offer a helping hand.
Hey Chris, yeah… your right! Soon after I posted that comment I stumbled upon the forums. This sites design is great. I’m wanting to learn how to make these types of changes to my site. Thanks for the support!
Hi Chris,
I’ve been doing this previously as a custom plugin. Do you see any arguments as to why you should have this as a custom function instead of as a plugin or would it matter at all?
Thanks for the great themes by the way, I’m getting closer to a move
Miguel — No problem at all… Good luck with your site!
Patrick — Plugins and functions (at least as they’re described here) are identical in terms of their functionality and how they interact with the WordPress core code, so it really makes no difference which route you take. I think a lot of folks would argue that a plugin is actually better, simply because people are accustomed to the whole upload/activation process.
Chris, when I buy your thesis theme, I plan on customizing, tweaking, (occasionally breaking), and playing around with it until I’m happy with it. Do you suggest that people that are like me install to a subdirectory, test the hell out of it until we are happy, whipe out the subdirectory, and reinstall to the root when we’re ready to post to the world (which is what I ultimately want)? And what are the ramifications of doing this if you go with a host like MidPhase that has one click wp setup? They ask where you want it and I’m assuming changing this down the road involves more that a one click alteration.
Your opinion on this would help. I’ve delayed setting up my blog due to questions like these.
Shane — Actually, it doesn’t matter where you install WordPress, because the only things you should have to move when you want to go live are the theme files themselves along with any plugins that you would like to use.
The process you’re talking about is often referred to as sandbox development, and it’s probably the most popular way to test new designs before rolling them out.
Also, you ought to be able to install multiple versions of WordPress on your server with the one-click install, so I wouldn’t worry too much about “changing” a WordPress installation on your server. Simply use one installation for testing, and then use another for the live version of your site.
Good luck!
Sweeeeeeet!
So, what if I have an existing site on a host other than Midphase and I plan on switching to a wp blog format and switching to midphase hosting.
I would purchase space on Midphase, install the blog, test it (by entering the isp number in my browser, not the existing live web address to my current non wp site), THEN tell Midphase to link the existing webaddress I have to this new Midphase server when Im ready? Hope I’m making sense here.
Thanks Chris.
Scratch that! Never mind. That was a stupid question. I would simple link my live site to Midphase, work in the background in a sub, then put everything in root when I’m ready.
Sorry.
Chris - Thanks for your quick reply! On the other hand I would guess a lot of people are more comfortable with working in theme files than plugins. Anyway, I think I’ll stick with my plugin.
Very good article, thanks!
Hi Chris,
thanks for this article. Eventhough I’m not planing to switch my theme often it did help me to clean up my theme and put a lot of code from additional includes into the functions.php.
I just learn web design, thank for good article.
Two things to say:
1. Great article man. I love you’re writing, as I’ve told you before.
2. You have to hate the Digg algo!
I’ve been struggling with WordPress now for months, thanks for the help - you’re a genius! I will now be employing this train of thought into my own WP design.
Rob
hi, and thanks.
all is good, simple explanation and advice. however if running James Clark’s Private URL plugin (http://jamesclarke.info/projects/private-url/) I get errors. if I deactivate his plugin, no problem. activated, errors above the header. I’ve isolated the problem to lines 133 & 134 of his plugin, where it says:
header(’Cache-Control: private’);
header(’Pragma: no-cache’);
if I comment-out “//” those lines, my errors disappear, but I’m not entirely sure what those lines handle, or if they’ll cause conflicts in the future using his plugin.
his plugin is the problem, yes? or does that “global $post;” call cause the conflict? the error i get is something about how the header has already been passed bleh blah blah! thanks!
hey i can’t find your email so i thought i’d post here. All weekend i have been trying to load you Thesis theme page so i can look at it, think about it and possibly use it on my own site. A friend of mine uses cutline and loves it but i’m looking for something a little different. Maybe Thesis fits that bill - but I can’t tell because neither your demo page or DIY Themes loads at all. I don’t know if diythemes is your domain or even if you have anything to do with it not loading but i thought i’d let you know.
-jessiegirl
If anybody sees Chris, has his phone number or can shoot him a message on Twitter or whatever, please let him know that people are trying to give him business and buy his Thesis theme but we can’t do it if we can’t access the site. Thanks.
Chris began a move to LA on Friday, driving across country. He is probably unaware of the site down times. I left a message with him a couple of days ago, but doubt that he has received it.
My hunch is that he will settle into LA sometime today, and then will figure out that he has a problem to deal with regarding site availability. Could well be tied to the server that exploded this weekend. (Several blogs have noted that this has impacted about 6.000 servers.) I am confident that Chris will deal with it as soon as he becomes aware of it. He’s going to be miffed, as I would be, that something like this happened at the worst time for him (while he was doing a long drive). Anyway, that’s all I know at this point. Hopefully we’ll see him getting it straightened out soon.
jessiegirl, CBM — I’m really sorry about the problems you’ve been having accessing DIYthemes! Unfortunately, I was traveling across the country in a U-Haul (kudos to Bruce for calling that out), so I was unable to fix my server problems in a timely manner. There were two separate server crashes over the weekend, and as a result, nearly everyone who tried to access my sites on Friday and Sunday ended up empty-handed.
I can assure you that nothing is more frustrating than dealing with server problems, especially when you’re on the road and completely preoccupied with other things. You have my sincerest apologies for the downtime, and I hope you’ll give Thesis another shot now that things are back up and running smoothly!
Thank you Bruce for the update. Chris I just purchased your developer package. It is a beautiful theme!
CBM — Saweeeeet. Welcome aboard!
[...] themes a lot? If you do, Chris Pearson has taken the time to write a great post about using WordPress functions to ease the transition between themes. The idea is to put all your design elements in [...]
I keep some of my code in separate files, but this is the first time someone has suggested customized functions, great idea!
[...] Pearson has put together a nice tutorial all about creating a user-functions.php file in your WordPress [...]
[...] Pearson wrote an interesting article about creating a user defined functions file that will work with any WordPress theme, theoretically greatly reducing time wasted customizing a [...]
[...] How You Can Use WordPress Functions to Run a Smarter Blog (tags: wordpress) [...]
[...] Pearson’s excellent tutorial on using WordPress functions. Generally speaking, whenever you opt for a new theme, you’re going to have to do a fair amount [...]
[...] http://www.pearsonified.com/2008/05/how-to-use-wordpress-functions.php [...]
Good detailed post. Thanks.
[...] I use Subversion for change control, I took Chris’ advice and use user-defined functions to produce any extra HTML, such as the link list in the footer. This is a tidier approach than [...]
To: Chris Pearson
Hi. Sorry that i write in comments, but cant find your email.
I really like template “pearsonified.com”. Can you do some similar or that same for me ? Of course i pay for this. This will be blog in polish language.
Kamil — Sorry, but I’m not for hire, and this is my personal template. Thanks for your interest!
But maybe I can buy this ?
I don’t must have unique template.
This is really super theme.
Pliss. I pay 100$
Kamil — That’s a nice offer, but no.
Thanks for making life easier for bloggers who want to have their blog themes customized.
Hi Chris. Can’t find your email anywere so I’m posting it here. The theme you use on this site is absolutely gorgeous. Is it possible for one to use it?
Jenefeldt — Thanks for asking, but unfortunately, this theme is not available for public use… I’ve got to have at least one custom design for myself!
Thought so. But I had to ask, ‘couse it’s really nice.
This template ROCKS!
I really love the template dude do you still have lots of this… for public use… hehehe plese share some… thanks dude
Duuuuuuuuude! Heh.
Hoot and/or Holler ↓