This forms part of my WordPress Plugin Development Series. In this section we add the functions that trigger when your plugin is activated, deactivated and uninstalled.
Essential WordPress Plugin Functions
The WordPress core includes a system called hooks that can be used when building plugins. These hooks are functions that allow a developer to use their own functions throughout wordpress.
I will be looking at the following three hooks: Activate, Deactivate and Uninstall.
These functions can be called by any name, but it is important to keep names unique so that your plugin does not conflict with another.
Since my plugin name is WT Hello World. I will be looking at using:
- wt_hello_world_activate
- wt_hello_world_deactivate
- wt_hello_world_uninstall
Add the code below to your main plugin file just before your php closing tag. I am using wt-hello-world.php.
Activate
Add the activate hook to your plugin. This hook triggers whenever the plugin is Activated. I will use them in later sections to manage databases.
function wt_hello_world_activate() {
//This will run when your plugin is activated
}
register_activation_hook( __FILE__, 'wt_hello_world_activate' );
Deactivate
Add the deactivate hook to your plugin. This hook triggers whenever the plugin is deactivated.
function wt_hello_world_deactivate() {
//This will run when your plugin is deactivated
}
register_deactivation_hook( __FILE__, 'wt_hello_world_deactivate' );
Uninstall
Add the uninstall hook to your plugin. This hook triggers whenever the plugin is Uninstalled.
function wt_hello_world_uninstall() {
//This will run when your plugin is uninstalled
}
register_uninstall_hook( __FILE__, 'wt_hello_world_uninstall' );
Full Code
Once you added the above, Your full plugin file should look as follows:
<?php
/* Plugin Name: WT Hello World
WordPress Plugin Development
description: Hello World WordPress plugin that alerts text via shortcode
Version: 1.0.1
Author: Willie Theron
Author URI: http://willietheron.com
License: GPL2 */
function wt_hello_world_activate() {
//This will run when your plugin is activated
}
register_activation_hook( __FILE__, 'wt_hello_world_activate' );
function wt_hello_world_deactivate() {
//This will run when your plugin is deactivated
}
register_activation_hook( __FILE__, 'wt_hello_world_deactivate' );
function wt_hello_world_uninstall() {
//This will run when your plugin is uninstalled
}
register_activation_hook( __FILE__, 'wt_hello_world_uninstall' );
?>
Once this is done, update your plugin on WordPress.
this can be done by either replacing the plugin files via FTP.
Or you could delete the plugin via the WordPress Dashboard and Re-upload.>
If you need it, here is the Source Code: wt-hello-world