Activating and Deactivating Functions for WordPress Plugins
During the plugin you’ll want to utilize some important functions. This function register_activation_hook() is called first when you develop a plugin. In WordPress When your plugin is activated then this function is executed Plugins SubPanel . There are two parameters accepts by this function: the function to execute when the plugin is activated and the path to the main plugin file In this article you use plugin as a function and variable prefix, a descriptive name for your plugin as well as. Below an example executes the function plugin_install() when the plugin is activated in WordPress:
When your plugin is activated this is an extremely useful function if you need to execute any actions. Let’s suppose, The WordPress current version you may want to check to verify that your plugin is compatible. Some default option settings you may also want to create. The first important thing is that you should to do always check when your plugin is activated is to verify that the version of WordPress the user is running is compatible with your plugin which is you developed. It is ensures any functions if using hooks and so on that your plugin uses are available in WordPress.
The global variable $wp_version is uses the preceding function, in this function store the currently running version of WordPress, and verifies that it is not running a version lower than 3.7. The version_compare() PHP function by this function you do the version comparison. The WordPress version if loxer than 3.7 you can display an error message to the users that they need to update the WordPress version.
When a plugin is deactivated there is also a function that executes this is called register_deactivation_hook(). When your plugin is deactivated in the WordPress PluginssubPanel this function is executed. Same two arguments as the register_activation_hook function accept by this. Bellow is an example using the deactivation function of plugin:
NOTE: You should to know important thing that deactivating is not uninstalling plugin.The uninstall functionality should never include in your deactivation function. Let’s suppose a user accidentally deactivates your plugin after deactivation all of their settings are deleted. So this is not be a good user experience and should be avoided this plugin.