The first step for an administrator to use a custom plugin in WordPress is to install it; however, in order for the plugin to actually work, it needs to be activated. The activation process allows for a plugin to perform some tasks that enable the plugin to work. Maybe some things will be done on activation, like creating a database table, but for the most part, it's going to allow code to execute, which is going to add hooks in and maybe do some other things as well. In addition, it allows for plugin administrators to turn on and off plugins, without having to worry about actually getting rid of all the files and whatnot.
As we've discussed earlier, the process of installing a plugin in WordPress is really just a matter of putting a PHP file or folder with a PHP file in it into the plugins directory of WordPress. However, until you actually activate it, that code will never be looked at. When you activate it--for example, Hello Dolly, I'll activate--that will then tell WordPress that on every subsequent request it needs to include that file in its execution.
So, this is the key point: anything that's not in a function in that file will be executed on every request. So you can see now, up here I've got the display of what the Hello Dolly plugin is doing. When you go from page to page, it simply puts out some other line from Louis Armstrong's Hello Dolly song. So let's go back into our plugin. This is a little plugin that eventually is going to output some copyright information, just a little stamp copyright, whatever year, whatever company, et cetera.
Before I do all that, I do want to have a check to make sure that I'm using WordPress version 3.0 or higher. So, one way to do that is to put an if statement. Now, in order to test the version, I need to get a variable called wp_version. So, I'm going to actually declare it up here, by saying global $wp_version; that will then make that variable available to me. Then I can use the version_compare function from PHP to test the WordPress version with some version that I say-- so 3.0 for example--and then I can determine how I want to test it; > or >=.
So, in this case what I want to do is if the version is greater than or equal to, then I will execute some statement inside my if statement. Now, what I actually want to do is I want to say, if it's not greater than or equal to--so I am going to use this little 'not' modifier and in here I am going to say 'die,' and I'll put a message, 'You need at least version 3.0 of WordPress to use a copyright plugin.' There is also an exit statement which works basically in the same way.
Now, I can save it. Now upon activation, every subsequent request now is actually parsing that file. Since I have WordPress version 3.0 or higher, it's not a problem; however, if I were to change this and say I require version 3.2 or higher, and then refresh it, you will see it actually executes this code, and it aborts the processing of the code.
So, in order for a plugin to work, it needs to be not only installed, but it actually needs to be activated as well. Once it's activated, however, everything that is not located inside of the function will be executed. So it's something you want to be careful of but also something that you can take advantage of. All plugins have to be installed and activated in order to work. Installation, as we saw, is just a matter of placing a file in the right directory, but the plugin won't do anything until it's activated. Once it's activated, however, it's going to be included in all requests inside of the WordPress environment.
This can be a very dangerous thing, but this is also a very powerful thing, and at the end, this is what makes our plugins work.
Get unlimited access to all courses for just $25/month.Become a member