register_uninstall_hook() - WordPress 函数参考中文文档



发布于 2017-09-11 字数3497 浏览 1080 评论 0

register_uninstall_hook( string $file,  callable $callback )

Set the uninstallation hook for a plugin.


Registers the uninstall hook that will be called when the user clicks on the uninstall link that calls for the plugin to uninstall itself. The link won’t be active unless the plugin hooks into the action.

The plugin should not run arbitrary code outside of functions, when registering the uninstall hook. In order to run using the hook, the plugin will have to be included, which means that any code laying outside of a function will be run during the uninstall process. The plugin should not hinder the uninstall process.

If the plugin can not be written without running code within the plugin, then the plugin should create a file named ‘uninstall.php’ in the base plugin folder. This file will be called, if it exists, during the uninstall process bypassing the uninstall hook. The plugin, when using the ‘uninstall.php’ should always check for the ‘WP_UNINSTALL_PLUGIN’ constant, before executing.



Plugin file.


The callback to run when the hook is called. Must be a static method or function.


File: wp-includes/plugin.php

function register_uninstall_hook( $file, $callback ) {
	if ( is_array( $callback ) && is_object( $callback[0] ) ) {
		_doing_it_wrong( __FUNCTION__, __( 'Only a static class method or function can be used in an uninstall hook.' ), '3.1.0' );

	 * The option should not be autoloaded, because it is not needed in most
	 * cases. Emphasis should be put on using the 'uninstall.php' way of
	 * uninstalling the plugin.
	$uninstallable_plugins = (array) get_option('uninstall_plugins');
	$uninstallable_plugins[plugin_basename($file)] = $callback;

	update_option('uninstall_plugins', $uninstallable_plugins);


Version 描述
2.7.0 Introduced.



  • wp-includes/l10n.php:
  • wp-includes/functions.php:
  • wp-includes/plugin.php:
  • wp-includes/option.php:
  • wp-includes/option.php:

User Contributed Notes

  1. Skip to note content

    You must log in to vote on the helpfulness of this noteVote results for this note: 1You must log in to vote on the helpfulness of this note

    Contributed by sangeethksin

    Perform Uninstall hook inside register_activation_hook

    function your_prefix_activate(){
    	register_uninstall_hook( __FILE__, 'your_prefix_uninstall' );
    register_activation_hook( __FILE__, 'your_prefix_activate' );
    // And here goes the uninstallation function:
    function your_prefix_uninstall(){
    	//	codes to perform during unistallation


需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。