code example for uninstalling Plug-ins in WordPress and removing article type components _java

Source: Internet
Author: User

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 ');

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.