1.2 Extensible Code: Methods and Benefits
Let’s start by looking at the benefits of making your code extensible, with examples from some well-known themes and plugins. I’ll also give you an overview of the methods for making your code extensible.
1.2 Extensible Code: Methods and Benefits
Hello and welcome back to this Tuts+ course on making your code extendable with hooks and functions. In this course, you're gonna learn how to use different types of functions and how to use filter and action hooks in order to make your code extendable. So that you or other people can edit that code and amend it without having to completely rewrite it. But first I'm gonna show you some of the methods that you can use to do this before we get into actually doing it. And I'll show you the code from some themes and plugins that use these methods. Now first let's take a look at the Twenty Sixteen theme, which is the default theme. And that uses the technique of plugable functions, which we'll be using in the next part of the course. So let's take a look at the functions file in Twenty Sixteen. So here's Twenty Sixteen's functions file, and that includes a whole range of functions which are used to set up the theme and to add content to it. But let's take a look first at this Twenty Sixteen set up function. This function contains a bunch of code that's used to set up the theme. So for example, you got text domain for translation, theme support for various things such as post thumbnails, title tags and automatic feed links and a lot more. But if you wanted to edit that or change it in a child theme of Twenty Sixteen, you could very easily do so by writing a new function also called twentysixteen_setup. And the reason that works is because this function is pluggable. And you can tell it's pluggable because it's wrapped in this conditional tag here if function_exist. So it's if the function doesn't exist because of the exclamation mark here. So before WordPress runs this function, it will check whether a function already exists. So if a function has already been fired with the same name. And only if that's not the case will it run this function. Now, if you're using Twenty Sixteen as a parent theme and you've created a child theme with its own function called twentysixteen_setup. The function from your child theme will run first because that's the way that WordPress does things with child themes and parent themes. So when it comes to this function in Twenty Sixteen, the parent theme, it will say, hang on a minute. I've already run a function with this name from the child theme, so I'm not gonna do it again. So whatever is in you will function called twentysixteen_setup will replace what's in the function from the parent theme. And in the next part of the course, I'll show you how to create your own pluggable function. In this course, we'll also be looking at hooks, specifically action hooks and filter hooks, and they work in a slightly different way. Action hooks sit somewhere in your plugin or theme and you can attach code to them. But to start with, there's no code or content that they output, they're just empty points that you can hook things to. Filter hooks are a bit different. You wrap a filter hook around an existing piece of code or text and that means that somebody can write a function attached to that filter hook which overrides what you've wrapped your filter hook around. So the content will be changed. So let's take a look at an example of each of these from a commonly used plugin and that's WooCommerce. Now, here's one of the template files from WooCommerce. And this is the template for displaying the page for a single product. Now, as you can see, this has got a bunch of actions in it. So here, you've got the action, woocommerce_before_main_content. And that is an empty hook with no code of its own that you can then hook functions to. So WooCommerce hooks two functions to it, as you can see from these comments here. Woocommerce_output_content_wrapper and woocommerce_breadcrumb. And using the priorities of 10 amend 20, WordPress will run those functions in that order when it comes to this action hook. And if you're running a WooCommerce store and you want to output extra content at that point in the page. You can create another function, hook it to that and give it the relevant priority to get it to output either before or after or in the middle of those two functions. So you can see down here, there's also another action hook called woocommerce_after_main_content. And a final one called woocommerce_sidebar. And if you have a look in WooCommerce, it's absolutely full of action hooks, as well as filters. Which are really useful cuz that makes it easy for you to extend WooCommerce and add your own content to it. Next, let's take a look at an example of a filter. So again, in WooCommerce here, I have a template part which is the part within a single product where the product thumbnails are displayed. And you can see here you've got an apply_filters function. What that does is it sets a default content which is the second parameter and that's three, and a name of a filter. So if you were to create a filter and hook it to this woocommerce_product_thumbnails_columns hook and use that to return a different number other than three, then your store would show a different number of columns when it's outputing the product thumbnails. There's another example of a filter down here and that's the actual image itself. So you can see you've got echo_apply_filters and then the name of the filter which is woocommerce_single_product_image_thumbnai- l_html. And then here are the contents, which are output by default. So if you were to create a function and hook it to that filter using the add_filter function, which you'll see in this course, you could change what's actually output. So you could output a completely different image. And here you can see there are other attributes that are added as parameters to the apply_filters function. Which is something you can do if you need additional attributes when you're setting up your filter. So that's how pluggable functions, action hooks and filter hooks work. Let's move on to starting to use them. In the next part of the course, I'll show you how to replace some code in a theme template file with a function and then make that function pluggable so that somebody can override it in a child theme. See you next time and thanks for watching.