Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
One of the potential problems with open development environments like WordPress is that after a period of time of using plugins that have options from in the database and new database tables, and all this sort of thing, and then deciding not to use them, it's pretty easy to get some database bloat, and have a lot of things hanging out there. So it's a best practice, as plugin developers, to create some sort of uninstall script that removes these items that are plugin uses when they're removed. As of WordPress 2.7, we have a couple of ways to do this. One way is that we can register an uninstall hook, which will essentially set up a function that will be executed by WordPress on uninstall.
The other way is to create an uninstall.php file. Let's go back to our CC comment plugin. If you recall, it set up a specific option called cccomm_cc_e-mail that we use to save the e-mail that the user use. So we want to get rid of this on uninstall. So if you go into your admin, and you go to the options.php page, you can see there's a cccomm_cc_e-mail that's listed there. So let's make sure that gets removed.
The way to do this is you first create a function that is going to do the removing when the uninstall occurs, and to delete an option from the options database, you simply say delete_option, and you pass the name of the option--in this case cccomm_cc_e-mail. You then need to register this, so that when it uninstalls, it will run this function. So we use a special function called register_uninstall_hook.
We're going to specify that it's going to run something in this file, and that the function is going to be cccomm_uninstall. So make sure before you do this that you back up your CC comment. So go into the plugins directory of your WordPress installation, copy the CC comment and put it somewhere-- wherever you like: on your desktop, or in your Documents folder. Just remember where you put it, so you can use it when you go back.
So now let's go ahead and go into our WordPress Administrator. Let's go into the Plugins page, and let's first deactivate our CC Comment plugin and then go ahead and delete it. It's going to give you a confirmation, and you're going to say, "Hmm! Do I want to really delete this?" In this case, we do. So it will have deleted all the files. Let's go into the options.php file again. You'll see now that your CC comments CC e-mail has been deleted. It used to be right about here.
So it did indeed work. So let's go ahead and go back into Eclipse, and you'll see it's giving you information saying these files are no longer there. If you refresh this, you'll see that they are indeed gone. So let's restore from the directory we left it in. So let's go ahead and copy cc_comment. Let's go back into our Plugin directory and paste it again. So now in Eclipse, we can refresh, and we'll see it there. You could also incidentally paste it through this interface, but it's easy enough to do it from within Macintosh or Windows.
So let's go ahead and open this up again, and let's try using the uninstall.php file. The difference is that in order for this to execute, it actually does have to run through the script. There might be something you have going on that you don't want to happen. So the uninstall.php file gives you a very clean uninstall. So let's go ahead and erase this uninstall and save it. And let's right-click on the cc_comment folder and go to New > PHP File, and we'll call it uninstall.php.
We can then paste in our delete_option. There's one other thing you should do: you should make sure that it's not running this somehow from some other way, because you wouldn't want it to delete the option if the user didn't actually want this to be deleted, and it's possible somehow that this PHP file could run. So the best way to do that is to check for the definition of a specific variable. So we'll say if not defined-- and when the uninstall occurs, there is going to be this constant WP_UNINSTALL_PLUGIN.
That's going to exist. So if it doesn't exist, then we can go ahead and just say, "Hey! Stop it." Then we'll exit. So that will then exit this file, and it will stop executing right at that point. However, on uninstall, this will be defined, which means it'll go ahead and go through and execute this line. So if we go back into our Plugins page, we're going to have to reactivate our cc_comment plugin, and let's make sure that that option got added.
It hasn't yet, because we haven't added it. So let's go into the Settings > General, and you can see there's our plugin, enter an e-mail, and then let's go back to the Options page. We should see our e-mail appear here. So now if we go into the uninstall, we deactivate our plugin and we delete it, then we can go back into the Options page, and we should see that our CC comments has been removed.
So since WordPress 2.7, we've had the ability to create uninstall scripts for our plugins. It's considered a best practice to clean up any database tables or any options that you've created in the WordPress database. This is a best practice that keeps our databases clean, and keeps people using plugins.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 96746 Viewers
56 Video lessons · 110454 Viewers
71 Video lessons · 79283 Viewers
131 Video lessons · 38028 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.