Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Activating a plugin is required for that plugin's code to be included in WordPress's execution. WordPress developers have the option, if they want to, to execute some additional code when the plugin is activated. For example, your plugin might need some additional database tables or maybe you want to set some options up, or maybe you want to check for software requirements, like PHP5 or a certain version of WordPress. Whatever you want to do, activation is the time to do that kind of thing. In order to utilize these features, we are going into our activate_plugin.php.
It's just a simple PHP file I created with comments. I haven't done anything in here yet. I am going to create a function, and this function is going to do something when the plugin is activated. So, we'll just call my_plugin_activate, and in here you could do whatever you want: you could create your database, you could create your options--whatever you want to do. For now, we are just going to write something into the error_log, my plugin activated.
So that's all it's going to do. Now, if I were to save this and run this in the WordPress environment, even if this plugin were activated, nothing would happen, because if you look, there's nothing that occurs outside of this function. There is no code that will execute, and functions will only execute if you've associated them with something where they're going to execute in. So to make it execute on activation, there's a special function called register_activation_hook. You specify the file that contains a function that you want to execute and the name of the function you want to run when the user activates the plugin.
So we are going to use the special variable __FILE--and you can see it comes up in the code-hinting--which represents the current file that you're writing the code in. Then we are going to specify the name of our function: my_plugin_activate. So, now when I activate this, this function will execute. The reason why is because once I activate this plugin, any code that's not in the function--which specifically will be this line of code--will be executed, which will set up that function so that it will run when the user activates it.
So, here you can see in my Plugin page, I have my activated plugin. So I am going to refresh it, just to make sure that that code executes to register that hook. Then I call activate. Now, I can look in my php_error.log, and you can see it said, at this day and time, my plugin was activated. If I want to do something on deactivation, the process is nearly identical.
You write a code--a function rather-- my function, deactivate, you do whatever it is you want to do inside of there, and then instead of calling register_activation_hook, we call register_ deactivation_hook, my deactivate, and save it. Now refresh to make sure that it gets executed, deactivate, I can go back to my log, open it again, and we'll see that it's been deactivated.
So in order for a plugin to work in a WordPress installation, it has to first be activated. This activation process can be a great time to set up anything you need for the plugin to work: creating database tables, setting option fields. We'll learn about these things in later units. Whatever it is you want to do, you can use the register_activation_hook function to take care of this. In addition, if you want to do anything else on deactivation, you can use the register_deactivation_hook.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 99411 Viewers
56 Video lessons · 112649 Viewers
71 Video lessons · 81479 Viewers
131 Video lessons · 39114 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.