the-deuce.jpg

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).

Take the Next Step!

  1. Share this on Twitter:
  2. Share this on Facebook:
  3. Submit it to StumbleUpon
  4. Bookmark it on Delicious

113 comments… read them below or add one

Ted Hessing May 15, 2008

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!

Reply

wpGuy May 15, 2008

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!

Reply

Frances Palaschuk May 15, 2008

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!

Reply

Anthony May 15, 2008

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.

Reply

Simple Mom May 15, 2008

So… you going to tell us Thesis theme users what this code is, or do we have to just try it out ourselves? ;)

Reply

Bruce Keener May 15, 2008

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 &amp; 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) :)

Reply

Sumesh from Blog Creativity May 15, 2008

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 :)

Reply

Chris P. May 15, 2008

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:

If you want to use a literal ampersand in your document you must encode it as “&amp;(even inside URLs!).

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.

Reply

Doug Martin May 15, 2008

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.

Reply

Dr. Mac May 15, 2008

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

Reply

Ozh May 15, 2008

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 ;)

Reply

Chris P. May 15, 2008

Dr. Mac — You asked:

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?

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.

Reply

Ian Stewart May 15, 2008

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.

Reply

Shane Arthur May 16, 2008

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

Reply

Dave Zoekmachine May 18, 2008

I’m not a techie but even I can understand and implement it..THXS !

Dave

Reply

Tim Norton May 18, 2008

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.

Reply

Miguel Wickert (Pineiro) May 20, 2008

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.

Reply

Chris P. May 20, 2008

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.

Reply

Miguel Wickert (Pineiro) May 20, 2008

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!

Reply

Patrick Ogenstad May 20, 2008

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 :)

Reply

Chris P. May 20, 2008

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.

Reply

Shane Arthur May 20, 2008

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.

Reply

Chris P. May 20, 2008

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!

Reply

Shane Arthur May 20, 2008

Sweeeeeeet!

Reply

Shane Arthur May 20, 2008

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.

Reply

Shane Arthur May 20, 2008

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.

Reply

Patrick Ogenstad May 20, 2008

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. :)

Reply

BANAGO May 21, 2008

Very good article, thanks!

Reply

André Wegner May 21, 2008

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.

Reply

Will May 24, 2008

I just learn web design, thank for good article.

Reply

Armen May 26, 2008

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!

Reply

rob K May 28, 2008

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

Reply

Nick Normal May 29, 2008

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!

Reply

jessiegirl June 2, 2008

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

Reply

CBM June 2, 2008

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.

Reply

Bruce Keener June 2, 2008

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.

Reply

Chris P. June 4, 2008

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!

Reply

CBM June 4, 2008

Thank you Bruce for the update. Chris I just purchased your developer package. It is a beautiful theme!

Reply

Chris Pearson June 4, 2008

CBM — Saweeeeet. Welcome aboard!

Reply

Rajaie AlKorani June 10, 2008

I keep some of my code in separate files, but this is the first time someone has suggested customized functions, great idea!

Reply

Andrew June 26, 2008

Good detailed post. Thanks.

Reply

Kamil July 1, 2008

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.

Reply

Chris Pearson July 1, 2008

Kamil — Sorry, but I’m not for hire, and this is my personal template. Thanks for your interest!

Reply

Kamil July 1, 2008

But maybe I can buy this ?
I don’t must have unique template.
This is really super theme.
Pliss. I pay 100$ :)

Reply

Chris Pearson July 1, 2008

Kamil — That’s a nice offer, but no.

Reply

Sonali Sengupta July 4, 2008

Thanks for making life easier for bloggers who want to have their blog themes customized.

Reply

Jenefeldt July 4, 2008

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?

Reply

Chris Pearson July 6, 2008

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!

Reply

Jenefeldt July 6, 2008

Thought so. But I had to ask, ‘couse it’s really nice.

Reply

Javenx July 7, 2008

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

Reply

Chris Pearson July 7, 2008

Duuuuuuuuude! Heh.

Reply

Ravi July 9, 2008

The theme is very professional. No wonder you dont want to give it out. Everybody wants to keep something unique for themselves. Well done.
Regards

Reply

Amos Singarella July 17, 2008

Hello Chris:

Would you be able to include on your website some of the most discreet kind of publicity in exchange for a fixed monthly income? I am referring to a friendly text link on the side bar of a blog.

We are a web marketing firm; thanks for letting me know if wish to monetise your blog with us.

Sincerely,

Amos Singarella

Reply

Chris Pearson July 18, 2008

Amos — In a word, no :)

Reply

Quote Catcher July 22, 2008

I was just going through this exact dilemma of wanting to, yet again, make some changes and figured… there has got to be a better way. Thanks for blogging about it and saving me a ton of time!

Reply

Rexibit July 22, 2008

Wow, this is a great article. Managing a large blog takes a lot of work. Anything you can use to automate the process is great.

Reply

Pedro July 31, 2008

That’s a great idea!

Reply

Mauro August 4, 2008

Great article, extreme tip.

I’m always learning great things about wordpress.

This goes to my list of the “must read” best articles.

Reply

Ride it like you stole it August 12, 2008

Man I wish Blogger was more controllable & predictable.

Reply

Chris Pearson August 12, 2008

RILYSI — Switch to WordPress; now is the time :D

Reply

cerita September 23, 2008

Wow great..
Thanks for your tutorials and idea…

Reply

Nihar September 26, 2008

Chris,

Thanks for this tip. I have changed my theme lot of times and everytime i use diff tool to add my toppings in index.php, single.php and other files.

I didn’t know about this feature before. I will try to implement this.

One doubt.

My single.php shows <?php the_content(‘Read the rest of this entry »’); ?> code.

Can i add new function that i write in other snippet or should be like you mentioned?

Thanks in advance.

Reply

WebTraveler October 4, 2008

You know if you look hard enough you can learn something new everyday. What you have here is great, very easy to understand and it makes complete sense that anyone who is looking to change templates often should definitely be doing this. thanks for the great read..very COOL!

Reply

Robert Portillo October 30, 2008

Being a new user to Wordpress, I found this article very usefull. But what is a smarter blog? Will it help my website get better rankings than if my wordpress blog does not use these techniques?

Reply

Personal Development November 2, 2008

Wordpress is truley a valuable and amazing platform. When I first heard of wordpress I figured you needed to be a coder or a web expert. The language and the ideas made no sense. After I was online for a year or so I decided to give wordpress a whirl and am glad I did. When I need to learn something I can and its free. I don’t have to bug anyone or call anyone. I’ve yet to need something for my site that I couldn’t find on the Internet. Templates are free and really cool ones are cheaper than a pair of sneakers! Wordpress is a great example of community,contribution, and leadership!

Reply

edwin December 3, 2008

Nice tutorial, bookmarked for future developing purposes.

Reply

stevenal December 5, 2008

Hope you happen upon this comment, Chris. I use neoclassical 1.1 for my site and still love it. There’s a problem in River City, however.

I use bluehost and recently installed simplescripts over wordpress and then updated wordpress (I was several revs behind because I was out of pocket for several months). Everything works fine except that no one can post comments any longer. I had them turned off before I upgraded and I suspect that wordpress “remembers” this rather than my new settings in the discussion tab. Any clues about how I can fix this?

Thanks!

Reply

stevenal December 6, 2008

Please ignore this. I figured it out.

Reply

William Todd December 9, 2008

Hey Chris

Having fun and learning a lot with Thesis. Unless I’m missing something Thesis has no ‘single.php’ file, or, if it does, I can’t find it. Can you help?

William

Reply

Collin December 17, 2008

Hey Chris, Thanks for posting such great tutorials! I really learn a lot from reading your blog. You deserve a pat on the back for all the help you have been to the wordpress community. Keep up the awesome work!

Reply

Jhay December 21, 2008

Great post!

Reply

Jauhari December 30, 2008

Wow, thanks for sharing.. this is really useful functions, we can add a lot of featured with this basic

Reply

Chris Pearson January 17, 2009

William — Thesis no longer includes a single.php file. All of the different page templates and everything are handled through logic now, making the template system a little faster and a heck of a lot more efficient from a coding standpoint.

Reply

gary January 26, 2009

I am trying to use your section on “Using new Functions” where you stated:

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 post. 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(); ?>

I cannot find any file called single.php. I have WP and Thesis installed.

Reply

Keno January 31, 2009

Wow, I am a user, and as you say, it`s good to be a savvy user, able to change the templates to benefit me. So I`ve trying to be able to incorporate an Author Box into my template for about 2 weeks, and finally I found your user function solution. Thanks, great post.
Keno Toriello

Reply

Vatsala February 18, 2009

Hi Chris, This is a very informative post :) , i just want another small info.. can the function current_user_can() function be used in custom functions… or is it that it can only be used in a plugin? i have been trying to google for an answer for this, but am unable to find any concrete answer.

Reply

Chris Pearson February 18, 2009

Vatsala — The function current_user_can() is a native WP function, and fortunately, you can use any WP function in your custom functions file!

Reply

Chris Lockwood February 23, 2009

I just started using Thesis and love how it uses the (what should be obvious but doesn’t seem to be used elsewhere) technique of custom files for the user’s changes. Nice not to have to edit sidebar.php, header,php, etc., then lose changes after an update.

Reply

Indian DTH April 4, 2009

Hi,

Thanks for the functions.

why u’r using ” global $post; ” in functions ?

Reply

consacepo August 23, 2009

I really like using Wordpress, I’ve found that it delivers the most polished experience with the least amount of work. I only have a few misdemeanors: It’s a little bloated if you want to use solely as a CMS, and integrating a design can be a pretty big hassle if you just are solely a designer. They have made the initial installation a fair bit easier, but again, this still is pretty involved for a design

Reply

miriam October 25, 2009

Hi Chris Pearson,

I saw your name at What About Our Daughters’ blog. Would you be able to help me with my blog? Actually I would like to create a free Ezine at wordpress. My problem is how to save it as a volume. How to save all the pages into one collection. I guess I could paste it onto word and save it as a pdf?

Reply

Ashley Farrand January 11, 2010

This is just WONDERFUL! Great help, thanks a lot for this post!

Reply

Scotty January 22, 2010

Hi Great post thanks for sharing.

Reply

Phil February 6, 2010

Brilliant! Just what I was looking for, thanks! :)

Reply

Tanya February 11, 2010

Hi there, Chris. I am a 2nd grader as it relates to blogs/websites/coding/HTML/ccs (or is it CSS??)… In other words, I am a speaker, not a techie. Anyway, I had uploaded the PressRow through WordPress. Then I started self-hosting through hostgator.com and I’m no longer able to use PressRow. I loved it because I had customized the header (I think its called—you know, the banner across the top of the page). Anyway, I’ve been search for other wordpress themes and stumbled across Thesis. Can a 10 year old figure it out? I’m really a beginner and don’t want to purchase if it is really not for beginners. I see that you have tutorials…but I’m REALLY a beginner. Any suggestions?

Thanks… love your “Big Ass Save Button”!!!!

Tanya

Reply

Chris Pearson February 11, 2010

Tanya, Thesis is definitely the way to go if you’re a beginner, primarily because our support forums are so helpful and full of information! One thing you have to consider here is that no matter what you end up using, you’re at least going to have to learn how to use what you’ve got. With Thesis, you’ll have a huge community (over 19,500 people!) at your disposal, and if you do have questions, they’ll be able to offer a helping hand.

The most consistent feedback I receive about Thesis is that at the end of the day, the forums and support are where the true value is. I know we’ll be able to help you, too!

Reply

kwikvu March 7, 2010

I just started using Thesis and have found it very user-friendly. The custom function and css files are great. With them, I don’t have to edit a large theme file and run the risk of breaking the theme.

Reply

Tim Nicholson April 19, 2010

Chris P, this is a great article and the way you have specified doing things (with modifying the theme template files), you have full access to all the template tags, even the ones that require being inside “the loop”.

I’m working on taking this even further in terms of minimizing impact of the custom functions on the theme through the use two additional techniques: (1) child themes and (2) add_filter() calls. In using those, NO changes to the core theme are needed, not even a single line to include the custom functions and NO changes are needed to the theme’s template files either.

However, I’m running into one major snag that is making the custom functions more complex than they should otherwise have to be. With the add_filter() calls and the way PHP handles variable scope in functions, there are some template tags that can’t be used, such as the_content() and the_excerpt(). I’m getting around this by simply declaring $post as global and then using $post->post_content and $post->post_excerpt. The only issue with this is that I’d still like to be able to use template tags like the_excerpt() because it will auto-trim the content to build an excerpt if a manual excerpt wasn’t filled out on the post. For now, I have to duplicate the wp_trim_excerpt() function in my custom functions.php file.

I was wondering if you had any idea what WP variables I could define as global that would let us use the_content(), the_excerpt(), and other functions that require the loop, such as get_the_post_thumbnail(). I know I could build a new wp_query but I don’t want extra reads to the database just to simplify the php code.

Reply

Osu June 13, 2010

Chris,

Nice article even many years after posting it. I have a question about your comment on widgets. You said:

“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.”

Does this mean I could insert several widget areas on the homepage for my clients to add their widgets to? Or does it refer to how one could control the output of those widgets?

I’m intrigued by what’s possible.

Thanks,

osu

Reply

Reem June 29, 2010

Hi Chris,

I couldn’t find any other way of contacting you. I’m interested in the Thesis theme but have a few questions. Will it work for a business site without any customizations? What kind of support do you provide?

Reply

Chris Pearson June 29, 2010

Reem, Thesis will work wonderfully for your business site without any customizations. However, if you decide you need to add some things (like an email signup form, perhaps), you can always turn to our professional support forums. When you purchase Thesis, you receive access to these forums where you can interact with the best and brightest of the Thesis community.

You may also be interested in checking out our newly-revamped Thesis User’s Guide, which is chock-full of helpful information that you can use to get the most out of Thesis.

Reply

Ajith August 28, 2010

Excellent, sweet article… ;-)

Reply

manish September 2, 2010

Great post thanks for sharing.

Reply

Alex Toll September 13, 2010

Thanks a bunch! I’m a rookie with the WordPress – but this shed some light on the topic=)

Reply

Ryan December 12, 2010

Thanks! I find this very clever way of maximizing functions on WordPress

Reply

Brixter February 26, 2011

Seems you and most of the people here is missing some point, the function in your download is not entirely a user-defined function. A user-defined function is like this:

function userfunc_bookmark_links( $value1, $value2, $value3, $value4 )
{
/** code here. */
}

userfunc_bookmark_links( ‘value1′, ‘value2′, ” )

Reply

Arnout Hieminga April 17, 2011

I precisely wanted to say thanks yet again. I am not sure the things that I would’ve implemented in the absence of the entire thoughts contributed by you over my theme. This has been a real hard difficulty in my circumstances, however , being able to view a specialised approach you dealt with that took me to jump with delight. Now i am thankful for the advice and as well , believe you recognize what a powerful job you have been doing educating many others through your web site. Most likely you haven’t come across all of us.

Reply

pranav May 7, 2011

Really thanks of great post, was very little knowledge about wordpress functions hope to use it now.

Reply

Thanh Tung June 19, 2011

Hi @Pearson,
i want to add a new widget called “slidebar” on the right of single post, like a sidebar.
firstly, i created a php file call “slidebar.php”, and located it in the theme folder. Then i activated it as i have read this article by ” include_once (TEMPLATEPATH . ‘/slidebar.php’);”.
finally, i placed the call function: “” –> Fatal error: Call to undefined function slidebar().
Can you help me to fix these mess ?

Reply

Chris Pearson June 19, 2011

Thanh, as it turns out, I don’t offer support for WordPress themes besides Thesis anymore. But I do offer support for issues like this if you’re a Thesis user (in the DIYthemes forums).

Reply

Thanh Tung June 19, 2011

Thanks for your soon reply !

Reply

Sumit July 4, 2011

hello, i wanted to show three latest posts on my home page is that any one can help me how i can achieve this.

Reply

Marius Nielsen July 7, 2011

Hi, I’d like to try and get this to work, but for some reason it doesn’t. I started to use wordpress just this week, so everything is new to me.

I’ve got the user-functions.php file up and running with the example function. I’ve set the include_once in functions.php. But when I go to one of the blog posts perma link all I get is a blank page (except for the menu and header. Everything from where the blogpost should have been is gone.

I suspect the include line is incorrect for some reason, maybe it can’t get the TEMPLATEPATH? If so, how should I definde this myself?

Reply

Bhaskar October 22, 2011

Another way to achieve the same end result in wordpress is to create a separate folder named “mu-plugins” in the wp-content folder. (mu stands for Must use plugins and they need not even be activated like other plugins)

Include all functions in a “my-custom-functions.php” file inside this folder

This way you can do away with the need for including include_once (TEMPLATEPATH . ‘/user-functions.php’);
code in the function.php

Wordpress core functionailities draw up all functions listed in the mu-plugins folder

Reply

Joel December 7, 2011

Chris, thanks for your guidance! I’m somewhat a Wordpress newbie, but I’ve included my function in my plugin and it works when called from pages.php for example, but I’d really like to be able to call it from INSIDE the post/page, i.e. from the text area that the author types in. Any help you could provide would be very helpful!

Reply

Chris Pearson December 8, 2011

Joel, to make your function run in the text (aka. the content), you’ll need to use the content filter.

When you use a filter, your function must return output rather than echoing it.

Here’s a reference page on the content filter in the WordPress codex.

Reply

Joel December 8, 2011

Thanks Chris! It looks like both Content Filter and Shortcodes would do the trick.
Cheers.

Reply

Calvin April 20, 2012

Great post! I’ve only just switched from Blogger to WordPress due to it’s better ability to handle mathematics symbols and equations. Tho not quite techie myself I think you’ve explained it quite well. Thanks a lot!

Reply

Shah May 21, 2012

Hi Chris, just wanna ask you … what if i have too many functions to add , my functions.php will be swarming with ‘crazy’ texts . How can i break it to multiple files like ‘functions.php’ , ‘functions-primary.php’ . i did try but got a bunch of php errors . Some hints would be appreciated . Thanks

Reply

Frost August 5, 2012

I wonder why did you even write this article. User-defined function is one of the most important when creating CMS or writing a Wordpress theme framework or simple a theme.

You can’t go anywhere without using user-defined function.

Reply

Johann January 1, 2013

Thank you. I was able to follow the instructions and make the day OK.

Reply

Robert Knapp February 13, 2014

Very helpful Thank you.

Reply

Deepa Govind April 10, 2014

Very simple, clean, and more focus on the logic of custom-functions (creating, including, and accessing them). Thanks a bunch

I have one doubt.
You mention :include_once (TEMPLATEPATH . ‘/user-functions.php’);in the core function.php

I get it, both theme-function.php and theme-userfunction.php are placed in the same folder.

Now that i have a child-theme-folder (with style.css/header.php).. and i upload my userfunction.php to my child theme folder, how do i make the same call to call the custom-user functions?

include_once (TEMPLATEPATH . ‘/user-functions.php’); ??
Coz, Templatepath, would then call the path of the parent theme

Looking forward to your response
PS: I am using NewsGrandTheme as parent theme

Reply

Hoot and/or Holler

Previous post:

Next post: