Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
by
FREELessons:15Length:1.6 hours

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

2.1 When to Write a Plugin

Hello, and welcome back to this Tuts+ course on plugin development. In this part of the course, I'm gonna give you an overview of when you should use a plugin as against coding something into the functions file in your theme. Because it's very tempting just to use that functions file for everything, and to stick loads of code in there that really should be going into a plugin. So let's take a look at the difference between the two. So here's an article that will help you identify which is better. But I'm gonna show you with reference to two plugins and themes that I've already developed for Tuts+. For other courses, so we'll start with this course which is a guide to the loop in WordPress. And that uses a theme. There's no plugin involved in this particular course. And it gives you a good opportunity to see what What sort of functionality you would add in the functions file in a theme. So this is the theme for that particular course. And you can see it's got a number of template files. And it's also got some include files such as the footer, and the sidebar, and so forth. And these include files to including the loop because it's a guide in the loop. But what we're interested in is the functions file. And this includes two functions which are relevant to themes and that you wont add to a plugin. So the first one is to set up the theme. So every well coded theme should have a theme set up function, which is hooked to the after set up theme hook And this includes setting up the title tag, the text name, post format, post thumbnails, RSS feeds, and also the navigation menu. So those are things that are specific to the theme, they're about the way that your site displays. So the functions file and any code that you to the theme Is about display, it's about the way your theme makes your site look, whereas a plugin is about functionality, and code that you put into a plugin will be code that you could use regardless of what theme you're running. So it's code that you want to hang onto if you switch themes in the future. And here's another example of a function that is relevant for using in the theme functions file. So that's a widgets function. And obviously, the widgets are specific to the theme. You wouldn't register widgets via plugin. And bear in mind these are widget areas, not widgets. So I'll be showing you how to create a plugin for widget that you can put in those widget areas. If I'm not confusing you with these terms later on in the course. But here in the functions file is where you register the widget areas, the side bars in your theme. So in this one we've got a side bar and we've got three footer widget areas, and those have to be registered in the theme. So those are good examples of functions you would add to your theme functions style. But before you go adding any and all functions that you want to add to your site to your theme functions file. It's important to consider whether it should be added to a plugin instead. And the real test is, do I want to keep this code if I switch themes in the future? And a good example of that is if you register a custom post type or a custom taxonomy. Because if you were to change your theme in the future, you wouldn't want to lose access to any posts you've added of that custom post type. Or that you've used that custom taxonomy with, because they'll still be in your database. So let's have a look at another example. So here we have the course, how to use custom post types in WordPress. Now this course includes a plugin, to register the custom post type, and it also includes a theme. So if we go back into the code, here's the theme. So I'm using this theme. To display my custom post types, so you can see for example here we've got archive.plusproject.php. But firstly, let's take a look at the plugin. So this plugin registers a post type called Project, and it includes The standard way of registering a post type in WordPress. Now a lot of people add this to their theme, because they think that it's linked to those archive pages and template files that are relevant for that post type. But in reality, it's much better to register a post type using a plugin, because that way if you change your theme in the future, you decide to use a third party theme Or you code a new theme for yourself, it means that you still got your post type. And you've still got access to all of the data in the database using that post type. Now that data will stay in your database, even if the post type isn't registered. So if you were to remove this plugin deactivate it, the database tables wouldn't change and you'd still have those posts in your database. So as soon as you reactivate the plugin You find that your post types and all those posts that you created using that post type are straight back available in your site. So it's important that you don't lose that if you change your theme in the future. So, the functions file for this particular theme, and if I just take you back. That's this Tuts+ custom post types theme, that I developed for that same course that functions file includes, including a style sheet Because it's a chart of a parent theme, and that's why it hasn't got this theme set up up here, because that's already included in the parent theme. And it also includes this function here, which displays projects on the homepage. And that's add it to the theme here because it's about the way the content is displayed. But arguably you could add this via a plug-in. You could create a completely separate little plug-in all of it's own That changes the way that projects are added on your homepage. Or alternatively, you could add it to this particular plugin. You could add an include file with that code in it, and I'll show you how to use include files later on in this course. So here's the plugin I created for that course. And that registers the post type. And that post type is then displayed in these two template files within the theme. So we've got all kind of types for this project. And what I've done here is copy the archive template file from the parent theme. And use it to create a custom template file for my custom post type. And there's also single post project which in the same way I copy the single template file from the parent theme and add it to my child Here, and that will display my post type that I've registered using that plugin. Now you might be thinking, if I was then to switch themes and I've still got my plugin registered, it gives me a problem because I haven't got this archive page. I haven't got this template file WordPress will always default, to another template file. It'll work its way through the template hierarchy, and it will display your project. Using the most appropriate template file it can come across, using that hierarchy. So it doesn't mean that you won't be able to display your projects anymore. It just means that they'll be displayed, using the template files from your new film. So that's the difference between the functions file and a plug-in. Use the functions file to add code that is about the way that your theme works. So for example Navigation menus and so forth. And there's also about the way that things are displayed in your theme. It's about design, and the look of your site. Use a plugin for any extra functionality. And in particular, ask yourself the question, would I need this functionality if I change things in the future. And if that is the case You would code a plugin. In the next part of the course. I'm going to show you the structure of a complex plugin. And you wouldn't be writing plugins this complex as you get started, but over time you might learn to and it gives you a good idea of how a plugin Is structured and how in particular, a large complex plugin is structured to ensure that the code all makes sense and is easily manageable and editable. See you next time and thanks for watching.

Back to the top