Developing Plugins for Your WordPress Theme Framework

0
332

It can sometimes be difficult to decide whether to use a plugin or your child theme’sfunctions.php file when you want to add functionality to a site which is using your framework.

When deciding what to do, I would ask myself a few questions, as documented in this infographic:

This will help you decide whether you should be using your child or parent theme’s functions file, a plugin, or your starter child theme.

If the functionality you’re adding adds a lot of code, would be useful to others, or will be used on other sites you develop but not all of them, writing a plugin is generally the best idea.

If you’ve decided you need a plugin, then you can make use of the hooks you’ve added to your framework to make them more powerful. For example:

  • If your plugin adds breadcrumb functionality, you can hook its output to thewptp_above_content action hook to display a breadcrumb trail above the content on each page.
  • If your plugin creates a more powerful or relevant search field, you can attach it to the wptp_in_header or wptp_sidebar action hooks.
  • A plugin creating a call to action box (like the function in the last tutorial, onchild themes), would be attached to the wptp_sidebar or wptp_after_contenthooks.

The list goes on!

Obviously there will also be plugins which don’t make use of your framework’s hooks, instead activating via core WordPress hooks, but your own hooks give you extra options.

An example is a navigation plugin I created to use with my own framework. This is activated only on Pages, and it first checks where the current page is in the hierarchy. If the Page has child or parent Pages, it displays the top level Page in that hierarchy with a list of its child Pages, giving you local navigation.

I’ve used this on client sites and attached it to either the before_content hook or thesidebar hook, or sometimes both, with extra conditional tags.

The plugin uses two functions: the first, wptp_check_for_page_tree(), finds where the current page is in the page tree:

The next, wptp_list_subpages(), checks if we’re on a page (but not the home page), then runs the wptp_check_for_page_tree() function and, based on its result, outputs the list of pages:

Having installed and activated the plugin, you would need to activate it in your child theme, which you do by adding the following to the functions.php file:

Of course you could use a different action hook if you wanted to output the list elsewhere.

Plugins are another part of the ecosystem you create as part of your framework. You can create plugins which are specifically designed to be activated via the hooks you’ve added to your framework, as I’ve demonstrated above.

However it’s worth spending some time before you write a plugin identifying if that’s the right thing to do: if in doubt, refer to the infographic earlier in this post to help you make up your mind.

source: http://code.tutsplus.com/tutorials/developing-plugins-for-your-wordpress-theme-framework–cms-21934

LEAVE A REPLY

Please enter your comment!
Please enter your name here