January 14th, 2014 by

5 Super Useful Functions for WordPress Developers

Screen Shot 2014-01-14 at 12.05.03 AM

There are scores upon scores of custom functions available across the web for WordPress developers looking to add functionality to their custom Themes or Child Themes. This is one of the great things about developing on WordPress, the fact that there is such an extensive support community and much of it is OpenSource. As a Web Designer, I find myself scouring the interwebs nearly every day in search of nice tidbits of code that will enhance my repertoire of cool web gags which my more Programming oriented brethren have already invented. Some you may only use once. But others open up whole new worlds of development that I find myself using over and over again. I will share 5 of my favorite custom functions that I have stumbled across recently, and then share a couple of resources that are great for finding more which I use all the time:

1. Turn comments OFF by default

More often than you would think I have encountered clients that want a blog set up on their site, but only want it to have one way communication. That is, they want to be able to post blogs or news but have commenting disabled. Typically I do not want to destroy the commenting code right out of the theme however, because there is always the chance that in a year they will want comments enabled. That is where using this function could be handy:

So now instead of having to create a post or page and make sure you have them all turned off for comments manually, they will simply be turned off by default. Very handy.

I have made a slight modification to this code from the original so that it will turn off comments on pages AND posts, but if you just want one or the other simply modify this line:

Source(s):

Tested with:


2. Use custom excerpts

There are times when you may need to use multiple excerpts in your templates with differing lengths. This simple function will allow you to do just that:

In order to call the function you will need to place the following in the template where you wish the excerpt to be called.

Replace LENGTH with the number of words that you want to be in your excerpt. This can be called multiple times across different templates.

Source(s):

Tested with:

  • WordPress 3.8, Child Theme of Twenty Twelve

3. Add Sonar pagination

I found this function on the site wp-ultra.com, but it is attributed originally to Kriesi. WordPress pagination by default is sort of lame, with those older and newer posts buttons. This can especially be lacking in robustness if you blog has many posts. Or if you want the user to be able to flip through a bunch on the homepage and don’t want it to scroll for 20 minutes.

That is where this useful function comes in and allows you to create pagination buttons – like the real kind – on any template that you need. I’ve used this function to replace pagination buttons mostly on category.php, archive.php and index.php. Please note that it is intended to replace the default WordPress template pagination in themes like Twenty Eleven, Twenty Twelve and Twenty Thirteen. I don’t see why it wouldn’t also work for other themes if you put the call in the right place, but the code below will explain what to replace in the default WordPress themes. If you try it with a different theme please let me know how it works and what you had to do. It is a little more complicated than the last two but not overly difficult to set up:

Next you want to find the pagination calls in your WordPress templates. These are usually in index.php, category.php, archive.php, search.php, and single.php. The call will look something like this:

Replace that line with this:

You will probably want to tweak this to fit your theme, but the following CSS is provided to get you started:

Source(s):

Tested with:


4. Multiple custom thumbnail sizes

The following function can be used to define multiple custom thumbnail sizes (from the image that is put as a featured image in a post) and then display them in your posts loop:

For “SIZE NAME”, give your custom thumbnail size a name. Obviously for WIDTH and HEIGHT enter the numerical values that you want for the width and height of the custom thumbnail. CROP receives a boolean value so enter either true, or false. This determines whether the thumbnail image will have a hard crop or not. You can use this filter as many times as you like to create a plethora of custom sizes.

To use your custom sizes, place the following in the loop you want the thumbnail to occur. In the default WordPress themes I usually put this in the content.php template.

SIZE NAME should be the same as it is in your functions.php filter. Use a separate call for each instance on your templates. If you have trouble getting your thumbnails to show up correctly, If find that using the Force Regenerate Thumbnails plugin works really well to solve any issues.

Source(s):

Tested with:

  • WordPress 3.8, Child Theme of Twenty Thirteen

5. Tree and Subcategory conditional statements

This last custom function to aid you in your WordPress child theme  development comes as a pair of two. They are very similar in how they work, but useful at different times. Part of the beauty of using php based templates in WordPress is the ability to use conditional statements within your templates. If you haven’t used them before, I advise you to look them up because you can use very basic logical statements to save you a lot of work, and make one loop do a lot of work for you. I’m sure for programmers this is obvious, but as more of a front end developer this is exciting. One conditional statement that seems to be an obvious need is one that will return whether the current page is a child of another given page ID. Here you go:

This will allow you to use the conditional is_tree as follows:

Use this conditional to check whether the current page is the parent or child of the given PAGE ID. (Replace PAGE ID with the ID of the parent page…ex. 126)

The final function I will show you will allow you to do basically the same thing as is_tree(), but with categories.

This gives you the power to use the is_subcategory conditional. This is REALLY helpful for tweaking the loop for the category.php template. You can call with the following code:

It will check to see if the current page is a subcategory of the given ID. (Again, replace ID with the parent category ID you want to check against. ) I will use this with the is_category() conditional also. Note: Make sure you include the single quotes around the ID. Also, this will NOT check whether it is the category ID’d. Just the subcategory.

Source(s):

Tested with:

  • WordPress 3.8, Child Theme of Twenty Thirteen

Please let me know if you find any of these helpful.

The following two tabs change content below.
I am an Buffalo, NY based WordPress Web Developer and Graphic Designer who loves to learn, read and write. My wife says that I got stuck at age 4 (the "wonder years"). I am a recovering Little Debbie's Nutty-Bar-a-holic and I have written a song about falling in love with a Llama. A Designer by trade, thinker by pursuit, and comedian in my dreams.
  • Jason Tutak

    Very impressive. Nice work Miles!