Advanced WordPress: Plugin Creation

 2 years ago 10,810 views
Presented by Peter MacIntyre (@pbmacintyre)

You say you can't find that perfect WordPress plugin in the many thousands that exist? Maybe you have something unique that you want to add to WordPress that no one else has thought of? Maybe you want to customize your site in a way that makes your site extra unique? Attend this session to see just how easy it is to create your own plugin and to get a brief look at the internal functions that WordPress makes available to the developer. Bend WordPress to your will and make the world your oyster, at least the WordPress plugin world. We will also briefly look at the codex for WordPress functions so that you, the budding WordPress developer can get a handle on the inner workings of the development tool set. Additionally, we will look at some of the best practices for plugin creation and maintenance.

We will also create a simple plugin in this session with a widget and a short code aspect as well as creating the admin interface for some of our custom plugin processes. All this while looking at the PHP code needed to make this happen.

Peter MacIntyre has over 28 years of experience in IT, primarily in PHP. Author of "Building Exceptional Sites with WordPress & Thesis" and "PHP: The Good Parts"; co-author: "Programming PHP-3rd Ed" & "Pro PHP Programming", among others. Peter has spoken at PHP Central Europe 2017 (Warsaw, Poland); PHP[World] 2016 (Washington, DC); ZendCon 2016 (Las Vegas), NortheastPHP (Boston), CA-World (New Orleans); CA-TechniCon (Köln, Germany); and CA-Expo (Melbourne, Australia). Peter is a past co-chair and co-founder for the Northeast PHP Developer's Conference, and is Zend certified for both PHP 5.3 and 4.0.

Transcription (beta):


This presentation is brought to you by RingCentral developers. Ringcentral developers provides you with APIs for voice, SMS, team messaging, meetings, facts and more allow you to take your current communications, the way you communicate today and completely revolutionize them to be communicated in the 21st century. As I like to say, the phone was invented in 1876. Isn't it time you start communicating like not 2018 but 2019. All right. With that, Peter, why don't you take it away?

Okay. Okay. So a little bit about myself. I do have my own company called bat palette and business solutions and I do want to thank a nomad PHP for the invitation to come and talk to you about WordPress and plugins and getting into the nitty gritty behind the scenes of WordPress. As I have on my slide here, I'm twice and certified in the industry over 30 years almost 30 years involved in the Northeast PHV conference. And I also have a travel blog if anyone's interested in that. I've spoken at conferences around the world. Those, the list of the places I've been, I'm a big rugby fan. I'm proud of that. And I played rugby when I was in high school and after in the university ages. But this is a picture I took the New Zealand all blacks taking care of France in one of the quarter finals in Cardiff, Wales in 2015. So I'm kind of happy with that. I'm going to do Zeeland fan.

These are some of my books I've worked on programming PHP individual author of BHP, the good parts and we're just now starting a revision of a programming PHP. So that should be out by the end of the year. I hope that's a fourth edition covering PHP version seven. So this is about a little bit about what we do. We develop websites and e-commerce basically based on WordPress mostly. But I do do some custom PHP programming if it's needed. And I do the ho coordinate the hosting and the domain name consulting and so on for my clients. Whenever you want to see any of my clients, they're from many parts of the world. You can see them at the my portfolio page.

So we're going to talk today about advanced plugin development. And I want to talk about what does that mean advanced? Well, we're going to look at the ins and outs of creating at the basics and then get into some of the actual code for developing a little plugin. We're going to look at the rules around plugging creation and looking at and consider it the best practices. We'll create a a demand menu. What's some form management and connecting of course to a my sequel database. We are going to create a widget which is a form that would be based on the front end of a website, WordPress website. Typically on the sidebar. And we'll look quickly at just doing a dashboard widget within the admin area. And as well we're going to duplicate or alter that widget code a little bit and turn it into a short code as well. And then of course, we're going to talk about what do we do with this plugin once we create it, how do we install it? How do we update it? And how are we going to go through the WordPress pool of plugins and try and distribute it that way? Are we going to distribute it on our own? So there's a few different caveats on how to deal with that.

So the basic tenants of a plugin, number one is don't mess with the WordPress core code. Never do that because you'll just shoot yourself in the foot. Keep your code separate from other plugins just so there's no collisions in the code. And use the API functions whenever possible because they are native to WordPress and therefore considered to be a solid, they've been around for awhile and they're native, so they should be quite efficient. So we're going to create a little plug in, like I said today and what's going to be called nomad plugin. So be sure to prefix your code, then your plugin tables and anything else that's related with some kind of naming convention that's unique to you so that you can see your code and your plugins separate from the rest of the environment. So in our case, we're using a nomad hyphen plugin, or if we're defining a table, it's up to you of course. And P for a nomad plugin is kind of a prefix we're using.


Other considerations are trying to use object oriented programming wherever possible. It also helps through encapsulation to mitigate any code collisions and we will certainly want to follow any secure coding guidelines. Always be protective in that secure your input, escape your output. And we certainly don't want Bobby tables to happen, especially in if you don't know what that is, what I'm referring to with Bobby tables I've put the little link there to the Khartoum and you'll have a little laugh on that when you, when you look at that later. So we're going to look at the basic structure of a plugin. Obviously we have to install WordPress. We're going to place our project in the WP content folder and name it as a sub folder to the WP content. If you're going multiple names, you want to put a hyphen or dash in between the names. So like we're using here nomad hyphen plugin and the main plugin code file will be the same name as the folder, but with a dot PHP cause that's the PHP code. If you're adding the plug into the WordPress pool you will need some other files. Go along with it. We need to read me text file and we'll look at the content of that a little later in this presentation. And you'll need an install PHP file. So you can direct your plugin to clean up after itself if it's removed or disabled.

The basic folder structure is here listed. So underneath your main plugin name folder, you want some folders with assets. If you're going to the w people, again, that's where you're just going to have your screenshots that you'll see. On the WordPress pages for plugins where complimentary screenshots about your plugin, you'll want a CSS folder, obviously for your style sheets, place for your images, your include files. And that's where the bulk of the code is going to go. And any supporting JavaScript or jQuery code will go in the JS folder underneath that. So that's just another way to organize your work. Keep things separate and hopefully, hopefully make it easier for you to debug or add features in the future. Additional considerations are if you want to separate your admin code from your front end code, you might add a an admin folder or a settings folder. Again, the additional folders are up to your discretion. You can name them Fred or Barney or whatever you want, but at least try and keep the minimum told structures for organization and for if you keep in mind that you're plugging, you're going to create by and large is open source software. So it helps not to make things confusing and follow a patterns for other people that might want to take your code and extend it.

So looking into that plugin file, the main file, we need to have a comment block at the top. So we named the plugin nomad sample. In our case we have a plugin URI, which points to the homepage for the plugin on your own website. If you have one short description, who the author is the version. And so on. Now these features these plug in names in the UI and so on show up on the plugin when you install it. After that, if it's going to be a free plugin, not a professional loan or for sale one you have to have a GPL to reference. So at the very least have your license you or in there and preferably to have this little two paragraph blurb about the open source software license free software license. So once this is installed, you'll see this is a screenshot of nurse nomad sample and these are the references that I'm pointing to mentioned earlier, like the plugging names version of the description and so on, which should all be very familiar if you've done installed any kind of plugin to WordPress.

So before we get into that, I also want to add to this slide so we could talk about planning the plugin. What are you going to do with your plugin? Many times people just start writing code and have a brilliant idea and off they go to the races and then eventually they get into trouble. So you want to consider what your plugins proposed to do, who you think it's for. Is it for financial people, is it for everyday people that are doing blog sites and things like that. So take a few days if you need to or, or a couple of hours depending on the size of your plugin to consider those first two points other to vex of is it going to integrate with other parts of WordPress or other plugins? Where is it going to reside? And by that I mean is it a folder that is, is possibly a sub plugin?

Do another plugin. Typically you want to keep it in your own folder, but there may be cases I'm working on a plugin right now that's integrating, going to integrate with WooCommerce. So you have to consider those kinds of things. How deep is your plugin going to go into other areas and where, where do the lines cross? So just kind of plan that out because you don't want to get into will commerce have your plugin working and released and get a bug in it that may trip up with commerce or some other plugin that you're interacting with. So be careful with interactions. And of course you have to consider whether you're going to make it a free one plug in or a commercial one, or if you're going to have two different levels or multiple levels for different features and so on. In that case you have to plan your code base, or are you going to have a one big code base that interprets trying to [inaudible] figure out whether it's a free version or a commercial version, or are you going to have two or three separate code bases?

And the last point is that, is it a simple one in the sense that it's only gonna affect the admin area. If that's the case, then you really don't have to worry about widgets and short codes and things like that that that are typically on the public side. So you also want to, once you get into building the thing, this plugin, you want to look at where it's going to reside in the admin menu. So WordPress has all these different features of functions that allow you to place your, your plug in, your interaction with your building within other areas of the admin area. So if you want to add it to the dashboard level, you can do that. If it's under the posts menu, you can do that. And so on. The only two that are kind of non intuitive are the last two is I had management page, which actually puts it under the tools menu and add options page, which actually puts it under the settings menu.

So the next thing we want to consider is when we're actually building this file. In our case where we're dealing with multiple levels of folders. So instead of trying to write folder paths all the time, it's a good idea to look at any of these defined constants. Use PHPs to find constant, a tool to build these folders like nomad plugin directory. It's going to be where your nomad plugin are. Again, this is the one we're calling it where you're plugging in is going to be and then you don't have to figure out on my up one level from WP content and went down two levels and so on. And as you see in the middle one here where we're actually making a separate one to find constant, that actually includes the include function, the include folder. So we know that we're going to be going into that folder often.

So we make, we make a defined constant for that right away where you using the multiple defined constants within the defined concept. So if you look at the last one here for the logo we're using the home I had plugged into or the first one we went created to help define where the logo is and we're just adding a, another sub folder for that images and the actual logo item itself, nomad hyphen logo, that P. And. G. So some of these defining concepts are going to just help you navigate through your plugin as you bring in folders and includes and images. And so on and CSS code and all that stuff, it's going to help build your plugin.

Also, you're going to have to really learn and get on a handle around these functions that are built into WordPress. The one you're going to see the most is add action and it's a function that brings in an, adds an action to or a hook to the WordPress environment. So you can, what it does is it allows you to tag on, to the load process of WordPress as it, as WordPress builds its pages. And that allows you, in our case, you'll see it we're going to build our menu structure based on the add action function. It's very important to go to this URL, the codex here and you'll see all the action references and we might take a little look at that later on here. Another one is he had filter that one we use not as much, but I've used it in the past to add links just to the plugin.

The list of plugins that are beside the are the list of links that are beside the plugin that you create. So the URL of, of where the plugin is, you can add other additional ones and you'll see it a little bit later. And one of them I've added a donation link more specifically, there's going to be a registered register widget, a function, and then we'll see that for building or widget. And we use our ad short code function to trigger the short codes and define those. And the, another important one is the register activation hook, which triggers a when you install your plugin or doing an update. So here's our first function which is really just going to call in our Java script supporting code. So that's what I'm saying after we've put in our descriptions and I'll show you the code here in a minute from top to bottom.

But after we put in our descriptions of who the author is, what the plugins about in the content in the comment block above, then we use our defined Constance, and then we're going to bring in our supporting fast of any JavaScript or anything else you want to bring into your plugin CSS as well. For example. And again, that's the last line. Here's our add action. So the action has at least two parameters. The first one is in it. In this case, that's what happens when the page initializes and we're referring to the WordPress admin page, which will in turn build our plugin in relation to the whole and admin environment. And then we're just calling. No man, Jay has add scripts. So that's just our function, which is just above here at the top which brings in through the WP and Q script, our JavaScript.

So it's a little back and forth here. But you'll see again the same idea here for our CSS. We just do the same idea. We add an action and initializing an inaction, and we call our CSS script. And then that function points to that file and brings it in. So again, you can see at the second line here in this a slide that we're actually bringing using the defined constant nomad and plug in DIR to bring in to point to where our CSS file is. So I have at the bottom here, just in it, it fires after the WordPress is finished loading, but before any headers are sent. So it's, it's something you can jump jump into and take advantage of when we're building our plugin. So then we do start building our, our menu function. So our menu, our system that we're trying to build here as as an example is just a small plugin that through widgets and short codes, which again, we'll get into a little bit later. But through those two processes, we're going to allow our, on the public side of our website visitors to sign up for, to be notified when a new post is published, a new blog post. So we're also want to be able to manage those requests, those signups from within the admin side as well. So the first thing we're going to do is build an air admin menu. And again, we have to use that add action WordPress function.

It's action in this case is admin underscore menu. So it's not in it and it's like we had before, but this is admin menu. So we're going to add something to the admin menu. So at the top level, if you remember that whole list of actions before that you could add it to the tools menu or the settings menu or the post menu. This is a particular one that adds it to the admin menu. And the second parameter here just simply calls that nomad menu function when it's ready to go. Then within this nomad menu function, we start building our menu. So we add, we call an add menu page a function, and we give it these parameters, the nomad free. That's just our page title. Then we'll get into menu title. What's going to show up on the admin menu itself?

The manage options that we'll talk about in a little more detail later, but it's the permissions level given to the person that signed in, whether they're an editor, admin level and so on. Then we'll have a menu slug. So that's a unique internal control for the menu position and name. And then we're going to call it that builds the configuration page. And again, we're using now the next parameter, no man plugin dirt. Again, we're using a defined constant, but then we're pointing to where our images are for our logo on the side of the plugin. And you'll see that here in a second as well. And the last one is the menu position. And now this is just kind of an arbitrary number. I'm using 25. You can play with this number and it's, it's a vertical position of where the menu that we're building shows up in relation to the other admin menu items. So 25 is probably, you can almost use it as a percentage of halfway, like 25% down menu. That's arbitrary though. But if you put in other plugins, they might push themselves up or down. If you've changed that to 50, then it would probably be further down on the menu and so on. But it gives you a relative kind of positioning of where you want it to show up.

And then we build a sub menu. So in this case, we're going to build the same settings menu under the sub menu of the main plugin menu that we're building. It's a little redundant in this case because we want, we want the first menu items to do the same thing as if we clicked on the plugin menu item itself. So that goes right below it. And I'm showing it a little dot, dot, dot here with more code. Cause we're going to look at that here in a minute as well. Now, the basic roles I talked about earlier on the manage options, we saw that in our, when we're building the menu, so the basic roles in WordPress administration roles or access roles are super admin, administrator, editor, author, and so on. But within that, those basic roles or many other sub roles and manage options is one of the administrators, not a super admin, but it's administrator level. So anyone that logs into the admin with administrator rights should be able to see and work with the plugin we're building.

If someone logs in that's only an author, they won't see this menu that we're building, this plugin that we're building because it doesn't have the level of permission. And then there's a URL here for this for all the different roles and capabilities. So you can, if you want, when you're planning your plugin and you can also consider who's going to use it and where should it show up, who, who would have rights to looking at it. And then we finish our admin menu function by looking by adding a manage subscribers lists. There's a few others, and I'll show you this code here in a minute. But when we build this, this is what our, when we run this code, we're going to see the nomad here. Here's our logo beside it that we pointed to. Here's your settings and our list subscribers and an add subscribers. Now again, all we're building so far in this plugin is a menu structure was in the admin side.

So this is what we're going to do to look at it in the code now. So I have a little slide here that says, look at the code. Let's look at the code. So the structure here, hopefully you can see that the structure, like I said, is under WP content. It's our own folder under plugins. And it's here has the other sub folders that we talked about. The basics, assets, CSS images, includes JS. And here's our folder or file that's named the same as that folder. So here we are looking at this code again, we're looking at nomad sample who the author is and so on. We've talked about that before. Here's our GPL license. And again, that's just a comment block that's at the top of the plugin. Here's our define constants. We talked about the plugin directory. Basic worthy includes our where our file name is. If we ever want to point at some of these. If you're making a a pro version, you can maybe just add a URL for it if you want to do that later on. And here we're looking at our, our logo for our plugin mogul. Excuse me.

You'll notice this one nomad spacer. I just use that sometimes for debugging that when you do actually put something out on the screen and it's the amendment using the way, I just put that in front of it which actually pushes it over to the area where I can read the, the debug output. So again, these are things that you can use or diff or remove when you turn your plug into a professional level or when you release it onto the world. As we mentioned before, here's our supporting cast of Java script and CSS in this case. And again, we're using the add action function in both of those cases for initializing it. Here's where we make the menu itself. So we're the nomad menu and inside it has these functions as well that are going to trigger.

So here we are adding the action. We're calling the admin menu initial of the trigger. And the nomad menu is the function we're calling, which is just below it here. That plus these sub menu items actually builds this menu. So let's look at what it looks like on the admin side. So here we are, have a, I just have a website built called testing the pallet. And for this purpose I'm done. So I'm looking at the plugins list. So here we are in plugins and this is what I was talking about. Here's nomad sample down here. This is where these menu items are coming in. So the, the plugin name is here, the description is here, and these links if you remember that other filter function I was talking about, you're going to add items to the end of it. So here's one here that I, another little one I built that allows this link to ask for donations or whatever other kinds of things you want. You can add those kind of links to the end of your plugin as well. You're not limited to what's in the opening comment block.

So now that we've built this through those in nets, when we activate the plugin, then we will have this nomad plug in, show up on our admin menu. Again, like I said, remember that position number was 25. This is roughly 25, I guess you could say percent down the down the menu. If we moved it up to 45, it would, it would lower being lower on the, on the menu list. So again, that's just an arbitrary number. So the norm nomad free plugin, here's a little settings page where we bring in a logo. We describe the page itself and have a little forum here. So going back to the code, what we're going to do now is each of these menu items with the right permissions can call their own functions. So the managed subscribers list, sorry, list subscribers, menu item, we'll call this function nomad list subscribers when it's clicked on.

So down further in our code, we're going to look at listing our subscribers. And here's our function, nomad list subscribers. So this is just a little block [inaudible] code that triggers when that menu item is tr is clicked on. And the reason I wanted to go in here and look at that is that here's another chance for protecting yourself against the permission levels. So if the current user can, and has that meant that a admin level then our, sorry, it does not have, we're using the, the not here. If they do not have this level, then we just return, we kick them out because they can't use that. Otherwise we're going to do PHP, require using our define constant and bring in this list of subscribers include file. So we're trying to keep the nomad hyphen pluggy in PHP file as small as possible and then bring in other things through the includes.

So we're gonna carry on where a little late on time here. I just want to, I don't want to get bogged down. So here's the manage options we talked about. And we looked at the config page. So we're going to build a widget and I'll show you this widget in its outside environment. Where did that go? Here we are. So this is the testing website and the public side. We're looking at the blog page and here is the widget to sign up for our website announcements for when blogs are posted. So we're going to still do another add action. In this case the initialization trigger is called widgets in it. And then we're going to call any particular widget function that, that we want. And inside that call function we are registering the kind of call, urge or widget function and that defines our code through a require. So here's our actual code and action widgets. We're going to call Noma and register contacts. And inside nomad register contacts widget, we are going to call another function register the widget and we're going to name that which is another function called nomad context widget.

Now what that is doing, in this case member I talked about trying to use object oriented code as much as possible through our require. Once this contact widget include file is all object oriented. So I'll look at a call contacts widget. Here it is here. So this is being required. I will show you back here where we're bringing it in. Right here, nomad contacts widget. And this is the file that we're bringing in. Again, it's a class it's based on the WP widget, so that is built into WordPress and you just extend it and then you start making your own methods and function calls within the class. So here we have multiple ones. This is the initialize method and then we have what's happening on the form of the widget. So this is the title that goes into the admin part where you're defining the widget.

One thing I do when you're naming the widget, this is just a little trick I use, is that I prefix it with a letter a, so goes to the top. So when we're actually building this widget under appearance widgets in the admin side once it's built inactive, then I just have it here so you can see it here. Nomad contacts, which is but it's got a letter I in front of it because they didn't want to float it to the top while I'm building it. Eventually I'll get rid of that. And it will be in its natural position alphabetically on this available widgets list. So here's the sidebar that we're building and we're just adding newsfeed sign up. So to prove that it's actual newsfeed sign up, I'll just add my own name to this and save it.

Now on the public side, when we refresh, now my name is added to the title of this widget. So you can see that that's actually the one that we're working on. So all we're doing here is we're going to collecting the name and the email of the person that wants to be notified through email of when our, our site publishes a new blog post. Then we click join the list. So I'm not going to get into all the depths of this code because there's a fair bit to it. But what it does is it builds the form. There's a form method and then there's what happens when so here's the form of the HTML form tag and so on. That's in the actual widget method and then further on what happens when you save it on the public side. What happens when you do things on the inside. And w w one thing we want to do is that when someone signs up inside this method, which is the public safe, we go through all some of these is the email properly form some that data edit checks and so on. But when everything's fine what we're going to do is see if they already exist there and how to base, we don't want them in there twice and return them message. If they fail, we'll just say that email is already on file.

Or if it's good, it's a new entry. Then what we're going to do is send the double opt in. Portion is we're going to send the welcome email to that person. So then on the other side is we're going to that that builds the widget. And then what we also have inside here is a way to list the people that have signed up on the inside. So here's lists subscribers. So again, object oriented, it extends a built in WordPress lists table. So take some time and look at that. And that will list the people that have signed up. So back to our, this is our widget that we're planning here. I'll put that back and the list, right. So we're going to look at the list of the people that have actually subscribed to us. And right now I have a test. You're testing thins, you're Peter Montessori, a PB McIntyre. That's my email address. So let's go and sign up a new for another person, which will be Peter testing thing. And I'll use my other email address and click join the list.

We've gone through our edit checks to say that they're not already on their list and we have a little thank you message going out. So then back on the inside, if we list our subscribers again, we'll see we have a new member here now again, we were doing in and so on. We have these other pieces that will allow the person to double opt in and all that kind of security. So where did we go? Widgets. so we're, we're doing the add action, we're building the widgets and we're registering it. And like I showed, here's work builds on the sidebar and where it shows up on the list. And again, my little alphabet, a trick there for you to bring it up at the top while you're building it. And then there's the forum that's built on the public side.

So that was a little backwards. We looked at the code first. And so another little aspect, this is a quick thing, is just a dashboard widget. And that's if you're building a plugin that you want someone to see things on the dashboard a home. So you can add things to the plugin. Here's a, this one here is mine, no, but nomad plugin demo. And it also builds into it, just two or three lines of code, the ability to turn it on or off if you want in that admin side. So the code for this is just an add action and you just do your WP dashboard setup and the function itself. So like I showed here in the code already, here's the screenshot of let's you affect it on the dashboard. So this is just a quick little dashboard. If you want something like this to catch the person's eye in the dashboard from your own particular plugin that's what you, one way you can do that. So the code for this is pretty simple. Like I said, it's just three lines of code and we're putting it in the, again, the main plugin file.

So here's the dashboard set up. We're, again, we're at doing an add action. We're adding it to the dashboard. Here's the title for nomad plugging info. There's a wrap that line here. So you can see it and dashboard example display. So again, this is a function that you're calling from another function. Sometimes it seems a little, why do I have to call two functions and an add action to get something done? But that's the way it works in WordPress. And here's the output and I just put it a color in red to catch the eye so we can see,

Oops, too far. So short codes. So another ad method add, add function and short codes allow you to create content anywhere on your website for WordPress. Well we're going to do here is just very, do a variation on what we've done with the widget is it have the form within a page. So to do that the first parameter is what the short code name will be that's within the two square brackets and another where the short code should reside. Now we're doing the same form, but I'm calling different files because there's a little difference. Slight nuances of difference between the front end of WordPress and the admin aspect of it. So we're calling your subscribers shortcode inc here. But up above here, here's the ad short code function and we're calling it nomad contact. So this is what was going to be, was in the square brackets. Sorry, square brackets. This isn't actual syntax here, but this is how you would define your short code within WordPress, wherever you want to use it. And we'll see that here in a second.

Again, like I said, we're just using the same similar code. We duplicated it, but it has slight little nuances because you're dealing with some things on the front end rather than the admin side. So the short code will look like this on the public side. Here's our little form. We just want a full name email and we're inside our contact page. The method, here's the code we've done. So here's the, when we're building it inside our contact page, all we do is name it with square brackets around it. And that triggers the calling of the short code whenever you want and wherever you want. So again, it's better than a widget in the sense that you can place this short code wherever you want in your website. So we've kind of looked at the code. This is again where it showed up. I just want to show you where on the admin side we can define it in a page. So we were doing it in the contact page. And again, we're just using the text block here. Building the form. Just this, this just triggers whenever that action is triggered, the shortcode action, it calls the the form that we built within the code. So our code then is Nope, nomad con, a nomad subscriber shortcode inc, which is right here.


So we're, we're doing some, some code within here, function show form to display the code, the little form of the name and the email address and so on. It's the same concept. Once you get it built, you're looking at the form and you're collecting information.

Now this is kind of important too. So this is another, something to consider in plugging creation is when things change in their state. In our case, we wanted to know when when people sign up, we want to be able to send them an information that one of our blog posts has been published. So again, you look at this API action reference URL and in this case we want to know when a status is changed on a post. So we're going to go from old status to a new status, but our own, we really only just want to care but the new status in a sense of it's published. So we're going to do an add action and the action is pending to publish and also drafts to publish. Because if you know WordPress posts, they can be go straight from pending to publish or straight from draft to publish or they can go from pending to draft. But in this case or draft pending matters. And in this case, we don't care about that action of draft depending because we just want to know, announced to people that sign up when a new blog post is published. So that's similar code is what we're doing here.

And then we're going to run this code when that action is triggered. So we're going to go and get verify that we actually do want to send out emails. I have a little table and a little control table that triggers whether we actually send out emails or not. So we can publish a post without sending out any, any notification if we want to. We can control that in that way. And then we just check to see if the post is not a revision. So it's already published, but someone come in and changed it. That's still not what were the acts where we're after. So if that is the case, what return and if not, if it is an actual new post, then we will the last line here, we'll send out a mass email. So I'll just show you that code quickly.

So here's our two actions right here pending to publish and draft to publish. And they're both calling the same function. And that's fine because these are two different actions. So, like I said, if it goes from draft to publish or appending to publish at any given point, only one of these will be called, but we're trying to cover both bases. And then again, we'll go inside here and run this code. So the global word that we, P DB is something that we're going to meet used everywhere. It's a word, it's a class object in WordPress that gives you access to the database. So I don't have to, whenever I in my configuration WordPress file, when I put in my credentials from my database throw the whole site afterwards, all I have to do to get access to the database credentials is to use that code. Then I have a, I can run a SQL against it. I'm just prepare statements and here I'm going to get the row and so on. So it's just a little side thing about a database access.

Where are we on our slides? Try and keep going here. So these are the little caveats here and again, installs. So when you're actually going to install a plugin this trigger register our activation hook, we've talked about that in the slides earlier. So this is what's going to happen if when we're initially installing the [inaudible] plugin, the nomad install. And this is what happens when we updated or activated to plug in. So since everything, most everything you want to happen will happen in the activation and updates because you only really install it once, hopefully. And then all you're going to do is update it. You plug in. So in the install, all you really need to do is just check that you have the latest version in here. You can just go version compare what version is it? Is it five? We're talking about the WordPress version. And if it doesn't, if it's not less than or equal to this number then it will then your plugin will not install. So you can just say your word press your little exit here. Message can is too old for this plugin.

If that's, and that's all you really have to do for your installation. Everything else is in the activation. And in the activation you want to go to the top here. Again, we have global access to the database object we need to require once this file, it's in the admin of WordPress and the WordPress admin includes folder and it's upgrade PHP. So this has a lot of features in it. You can look at that later, but at least when you're doing upgrades to your plugins, it gives you access to this function called DB Delta. And you'll notice here I'm creating my tables. Now, this trigger also happens. Activation also happens on an original installation, so you can cover two birds here. So what we're going to do is we're, what the plugin does is it creates a table on installation and during an update with DB Delta, it controls or mitigates anything that's going on in the database itself.

So if you added, maybe you wanted to say add a new title to this, a new column to your table, or if you want to change the field to just nomad name with the DB Delta, it will control, it'll, it'll alter the table and control as much of the data changes as possible. So that saves a lot of time in updating tables and so on. I, I, I could do this with a create table, but when I'm running an update, it's still through DV. Delta will alter my table as well as create a table if it's not there.

So that's good to know. Also in this activation file you can see the table if you want. So here we're adding a new we're inserting into the word control table, some basic information. We're creating another table of contacts where our email and name information goes into. You can here we have, I have a an online help thing tool that I added so I can customize my health and see that table as well as a bunch of inserts. You can also, which is interesting and you'll see if you get into this code that there's pages, you can create pages in WordPress. So with this little bit of code here you can create a page that you might be using. So here's our email confirmation page. When someone does a double opt in, they can use the your plugin can create the page for them and then you can go and then the user of that of your plugin can customize that page, but at least it makes your code not fail because you're going to, you know you want a confirmation page that someone opted in to, did the double opt into your email sign up.

And the same thing if someone unsubscribes, you can do that, the opposite of that as well. So that's the installation process and the updates. So I've explained that through the code kinda again, I did a little bit backwards, looked at the code before I looked at the slides here. Now lastly, you get a little bit over time. But lastly, we want to know, well, there's a lot of code here. What do I get into trouble? There's two plugins that I have nothing to do with but are very good to have and they are the debug one, the bug bar already installed it here. So look up debug bar when you go to add a new plugin and also the query monitor. So these two, I'll activate this here. So when we do do a debug bar, activate it, it creates a new item up here on your right hand side of your menu and it allows you to look through the debug the process of what, when you're building the plugin.

And we also add the query monitor. You'll see another menu option show up here and it gives you just PHP errors. It goes hand in hand with the Dabbagh so you can see the deeper queries here if you want to, and the object cache and so on. So there's a lot of feedback here that you can get that will help you. Here's a slow query. You can click on that. What are we talking about? Well this is a particular plugin and it found that was kind of a query. It was kind of slow, just some helpful information. Maybe your SQL needs to be written more efficiently, who knows?

So those two things are nice plugins to be having as well that are part of WordPress admin so they can help you debug short codes, constants, post types, all that kind of stuff. Crohn's, that he might be depending on what you're, you're building. So go to this URL, a developer, Okay. Plugins, developer hyphen tools, and you'll see some of these query monitors and the debug bar explained a little better in more detail. And lastly, we just want to know a little quick thing about what to do. If you're packaging the plugin. So if you're doing it yourself, you're not doing it through the WordPress pool. You just save all your folders as a zip file and distribute the zip file. And then that a will, if you give it to someone and they say add new plugin and you upload the zip file, it will take what you've built and add it to their sister WordPress system.

The trouble is any updates to the plugin has to be manually announced and manually done with your hosting it through WordPress. Again, go to developer WordPress or plugins,, and that's the guide. Therefore, how to give them the code. They have to eyeball it to make sure there's nothing bad in it. Once they give you the okay. Any updates that you do are done through a subversion to SVN. And if you remember at the very first I said you need to have a read me dot text file. So I'm just going to take a quick look at that. This just describes the plugin itself for the WordPress pool system. So you need to know what version of WordPress it's done to any plugging tags that help it be searched for on the WordPress pool site.

Again, the licenses that you need to put in, but the description and the installation and all this kind of stuff is here. If you have any special installation requirements, you can put it in there a frequently asked questions, what is the fact anyway? And that kind of, obviously we're just doing some fun stuff here. Screenshots that go along with the plugin. And then this is probably the most important is the change log. So, you know, you can tell a WordPress and anybody that installs a, your plugin, the change log things that you've updated and fixed and so on. And then there's a little contribute block as well that you can ask for help or who wants to change and help you do it in their language or multiple languages or help with documentation or things like that. And so that's in all goes in the read me text file.

So additional resources we're getting to the end. Any if you're going to look at some guides, basic guide to writing a plugin, there's a URL for that. Of course, nomad website has bags of PHP content. So if you're into the PHP side of it as opposed to the WordPress side, certainly go there. And all my sample code that I quickly ran through here is on GitHub, and it's under my name, PB MacIntyre and the, the packages nomad hyphen free plugin. So you can take that, download it, play with it break it, have fun with it. So again, I wanted to thank you for your time and your interest and plug Paladins, always looking for projects to work on. So if you're looking to have any interest in getting in touch, please do. Thank you so much for this fantastic presentation.




PHP Tutorials and Videos