Thord Daniel Hedengren - Smashing WordPress_ Beyond the Blog-Wiley (2014)

(avery) #1

CHAPTER 7 • Making the Most of WordPress Plugins 183


Most of the time, you won’t see the priority or number of arguments in plugins, but they come
in handy when needed. As they are optional, you can just leave them out when you don’t need
them. At other times, you really need the priority because you might clash with other plugins
otherwise. If that seems to be the case, it’s a good idea to adjust the priority to make sure that
your plugin loads first, or last for that matter.


Filters work more or less the same way, but you use add_filter() instead of add_action().
The parameters are the same, and you pass them the same way as well. The only difference is that
you can’t use the action hooks with add_filter(); you use the filter hooks instead. Other than
that, the two behave in the same way.


Adding functions to a hook, whether it is of the action or filter kind, is easy enough, but what
about removing functionality? Sometimes you don’t want an action or filter hook to run, and
that means you need to make sure that it gets removed. This is done with remove_action()
and remove_filter() for action and filter hooks, respectively. The syntax is simple:


remove_action( $hook_name, $function_name )


And the same for remove_filter():


remove_filter( $hook_name, $function_name )


This is not just used to remove functionality you have added; you can also remove core
functionality from within WordPress with these. Any action or filter function can be removed
this way, from pinging to deleting attachments, so some plugins may actually be designed only
to limit WordPress functionality rather than extend it, all depending on what your goal is.


CREATING YOUR OWN TEMPLATE TAGS


Another way to access your plugin functionality is by creating your own template tags, much
like bloginfo(), the_title(), and so on. This isn’t hard at all; in fact, just by creating a
function in your plugin (or in functions.php for that matter), you can access that function
with a simple little PHP snippet:


<?php function_name(); ?>


Not very complicated, right? It really isn’t, but that doesn’t mean that it is the best way to
include plugin content, even though it is by far the easiest method. With this method, you
don’t need to add any hooks, and the function will be executed when the plugin template tag
is loaded, which means that you’ll just put it where you want it within your theme files. This is
especially handy if there is no hook available to execute whatever it is you want to do; then
you can just create a template tag and have it do your dirty work. Naturally, it is a better
option to use existing hooks whenever possible.


One thing you need to keep in mind before going down this route is usability. Not everybody
is comfortable with editing the theme files, and if you intend to release your plugin or deliver
it to a client, it may not be a good idea to force template file hacking for usage. If you’ll be

Free download pdf