Adding Theme Support For Custom Functions In WordPress

Recently while working on a client project, one of my co-workers shared a function for WordPress that I was not aware existed. Well, to say that I was not aware that it existed would be untrue. Frankly, I didn’t know that we could create custom functionality with this. The function I am talking about is:add_theme_support();. In the past, I’ve used theadd_theme_support  function as described on the WordPress Codex – primarily to add support for thumbnails, menus, post-formats, etc.

You’re probably wondering why would I take the time to write about a function that most WordPress theme developers are already using? The reason is simple, most WordPress developers love, or should love, to discover new ways to hook into the built-in functionality that WordPress has and do amazing things!

My co-worker Chris Marslender, who is the Technical Co-lead on one of the projects I have been working on, shared that you could add theme support for virtually any custom function in WordPress. This type ability has been crucial in our current project where we have developed a Parent/Child Theme strategy, using functions and elements across multiple sites. Some of the custom functionality we have created are being shared by some but not all sites. The ability to create the function in the parent and add theme support has proven to be a step in the right direction that allows for one point of failure and less code to edit when additions would need to be made or bugs are discovered.

Let’s take a look at an example of how you could use this type of function in your theme.

Create a Custom Class or Function

Let’s say that we would like to create a function that will create a Portfolio Custom Post Type. Here is the code we would use to do this:

As you can see, this is a standard class that creates a Portfolio Custom Post Type. Simple enough right? You would then call this class in your theme’s function.php file using the following line of code:

Adding Custom Theme Support

Now that you have written your function or class, you can add theme support for it in two easy steps. Let’s first look at how to add this to your class or function. In the case of the class above, we have aninit() function that is usingadd_action()  to call theregister_cpt()  function. To add support for this in your theme, you could do the following:

Notice the new code on line two that sayscurrent_theme_supports . This checks for a theme’s support for a theme function, in this caseportfolio .

This is the first step to adding custom theme support for your function or class. The second step is much easier and should be familiar.

In your theme’s function.php, you would add the following line of code:

With these easy steps, you can add support for your custom functionality in your theme. While this may not apply for every theme, it can prove useful in a situation where a Parent/Child theme strategy is being used with shared functionality. I’d love to hear your thoughts on this!

And once again, shout out to my co-worker, Chris Marslender for teaching me this handy piece of code.