Plugin Uninstall
in the process of developing plug-ins, it is unavoidable to create some fields and even tables in the database, or create a number of timed tasks, when the plugin is deleted, these things will be left on WordPress, into garbage, as the responsible developer, It is necessary to help the user remove the traces we left when removing the plugin.
uninstall.php file
There are two ways to do this, a moment in the root directory of Plug-ins to create a uninstall.php file, before your plug-in is deleted will invoke the execution of this file, but to be aware of is to prevent someone malicious access to this file we need to judge the Wp_uninstall_plugin Whether the constants are defined or not, end the program:
<?php
//prevent someone from malicious access to this file, so end the program
if (!defined (' Wp_uninstall_plugin ')) exit () without a Wp_uninstall_plugin constant;
You can do something when you want to uninstall, such as deleting some fields, logging off the timed task
delete_option (' endskin_name ');
Delete_option (' endskin_name2 ');
Uninstall Hook
The second method, called Uninstall Hook, will perform the uninstall hook when your root directory does not have uninstall.php files.
Example:
Register_uninstall_hook (__file__, ' bing_uninstall_func ');
function Bing_uninstall_func () {
//You can do something when you want to uninstall, such as deleting some fields, logging off the timed task
delete_option (' endskin_name ');
Delete_option (' endskin_name2 ');
}
This code directly into the plug-in file, but the uninstall hook can not use the class function, otherwise the $this will be saved to the database, so if not the last resort, please use the uninstall.php file as much as possible.
Remove some components of a custom article type
WordPress Custom article type with many components, when we do not need to use the Remove_post_type_support () function to remove, below is a can be removed a list of components:
- Title
- Editor
- Author
- Thumbnail
- Excerpt
- Trackbacks
- Custom-fields
- Comments
- Revisions
- Page-attributes
- Post-formats
For example, remove the comment function of the article type that you brought with you:
/**
* Remove the comment function of the article
*http://www.endskin.com/remove-post-type-support//
function Bing_remove_post_ Type_support () {
remove_post_type_support (' post ', ' comments ');
}
Add_action (' init ', ' bing_remove_post_type_support ');