Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
by
Lessons:10Length:1.2 hours

Next lesson playing in 5 seconds

Cancel

Free Preview: 3 Practical Projects to Learn to Code WordPress Plugins

Introduction

00:59
  • Overview
  • Transcript

Part of the reason for the popularity of WordPress is how customizable it is. With the right themes and plugins, you can make WordPress do just about anything you need it to. If you want to get started coding your own plugins for WordPress, this course is a good first step. You'll learn to code plugins with a series of practical projects. With a little work, you could use these plugins in your own sites!

In this course, Rachel McCollin will guide you through creating a series of practical call-to-action WordPress plugins. First, you'll learn how to create a call-to-action plugin that can be activated with a hook from within a theme. Next, you'll create a plugin to output a call to action from a shortcode that users can embed in their content. And finally, you'll create a call-to-action widget that users can edit and add to their sites with the WordPress Customizer or Widget admin screen.

Do you want to learn WordPress development from start to finish? Check out our learning guide: Learn WordPress Development.

1. Introduction

1.1 Introduction

Hello and welcome to this Tuts+ course on practical projects to learn to code plug-ins. My name's Rachel McCollin. And in this course, I'm gonna show you how to create a plugin activated via hook, a plugin to create a short code, and a plugin to create a widget. So firstly, I'll outline some best practice guidelines for creating your own plugins. I'll then move on to creating the plugins in our site. And the first one will be activated via a hook which we'll add to our theme. And that will output this call to action box you can see here. We'll then move on to creating three different kinds of short code. And all of those will output the call to action box within the body of the post where the user adds the short code. And then finally, we'll create a widget function to create a call to action widget with fields that the user can update. And that will output the widget here in the sidebar widget area. Watch this course and find out how to create these plugins.

2. Plugin Best Practice

2.1 Best Practice for Coding Plugins

Hello and welcome back to this Tuts+ course on practical projects to learn to code plugins. In this part of the course, I'm gonna run through some best practices in plugin development, so that you know how best to write your code before you get started. So let's take a look at some key elements of that. The first thing is to be aware of and stick to the WordPress coding standards. So before you start coding your own plugins, take the time to read through the coding standards and make sure that both your PHP, your HTML, your CSS and your JavaScript, that all of those are in line with those standards. So that's really a no-brainer. The next item of best practice is naming conventions. So let's take a look at one of the plugins we'll be developing in this course. So here we've got tutsplus-hooked-plugin. Now, the first thing you need to think about when you're naming it is the folder that your plugin is in. And here I've used tutsplus as my prefix, so I know that this is a plugin that I've developed for Tuts+. You could use your own name or initials, or whatever you need to. Let's take a look at that plugin a bit more. So here we've got the plugin file. And again, that's called tutsplus-hooked-plugin. Within that file, we have some functions, we have two functions. Both of these have got names that reflect two things. Firstly, they have a prefix, which makes sure they're unique. And secondly, they have a name that tells you something about what that function does. So when you're naming your functions, use two elements. Firstly, your own prefix, because that will ensure you don't clash with another function in a plugin or a theme that you've got running on your site with the same name as this, for example. And then the second part should be descriptive. It doesn't matter if it's long, but it should be descriptive of what the function actually does. And you can see that I use underscores between the words, which, again, is how you should do it with a function name. But before you start writing those functions in your plugins, you should also check for duplication. So be sure that you're not writing a function that duplicates something already provided by WordPress. And you can check the WordPress Codex for the functions that are already there. Another thing to think about is your file organization and file structure. So this is really a simple plugin, it only has one folder within it, which has a file and then the main plugin file. But you can see I've added the CSS in a separate folder, and this is best practice. So you should have a folder for your include files, your CSS styles, your JavaScript and anything else that's separate from the main PHP code within your plugin. Now, in some plugins, they'll include classes and other elements of PHP, or other PHP files here in the main folder. And then just put other types of assets such as styles, JavaScript, images and so forth, in subfolders. In other plugins, they might put all of the extra files, everything except the main file, in their own subfolders. So, for example, you could have a classes subfolder for any classes you had, and an includes one as well. The important thing is to make sure that your main plugin file, that makes the plugin work and calls everything else, is here in the main folder. And that it includes, at the top, this information that tells WordPress that this is a plugin, what its name is, its URI, its description, the version, the author and the author URI. And you will see all of those in the WordPress plugin screen. So if we open my Plugins screen, this is my Call to Action Plugin Hooked to Theme and it's got the description, the version and the links here. So that's an essential part of creating a plugin. Let's have a look at another example of a plugin, so you can see in a bit more detail how the folders might be structured. Here's woocommerce, which has a lot of files. So you can see the main woocommerce.php file is here. But then you've got templates, you've got sample data, you've got include files, which is all the PHP that's not included in the main plugin file. And in the case of woocommerce, this is the vast majority of the plugin. So you can see this file isn't actually very big. But what does happen is the other files are included. You've got internationalization and you've also got assets. So all of the other assets that aren't PHP are included in this assets folder, in subfolders under that. And that, again, can be a good way of arranging your folders, if you've got a lot of stuff to put in there. So you've got styles, fonts, images and JavaScript in there. Or you could alternatively put those at the top level, but you'd have each within each own folder. So when you've created those folders and you've got all your other files in different places, how do you then call those in your plugin? Well, when it comes to style sheets, I'll be showing you how to do this in more detail as part of this course. So both style sheets and scripts, you have to enqueue within your plugin using the wp_enqueue_scripts hook. So I'll show you the code that you use to register a style and the way you register a script is very, very similar. You use the same wp_enqueue_scripts hook for both scripts and styles, which may be a bit confusing but once you've remembered it, that's just how it works. Let's go back to our files. And here's another plugin, which I've created for a client site, and you can see that's got include files. So there are a number of include files here. And those, again, need to be called from the main plugin file. So you can see here, we've got require once and then this dirname file refers to the file path of the current file and then inc/widgets.php. So looking at that, we've got inc/widgets.php. So you can include files from those subfolders within your plugin, which is good practice if you're writing a lot of code and it's easy to group that code into similar functionality, rather than creating one massive great core plugin file. So those are some best practices for coding your own plugins. I'll provide links to useful resources on this with this course, so here you can see the coding standards, some best practices and some simple rules in another post on the Tuts+ website. In the next part of the course, we'll move on to looking at how you create plugins that are activated via hooks. See you next time and thanks for watching.