register_uninstall_hook

函式
register_uninstall_hook ( $file, $callback )
引數
  • (string) $file Plugin file.
    Required:
  • (callable) $callback The callback to run when the hook is called. Must be a static method or function.
    Required:
定義位置
相關方法
register_taxonomyregister_activation_hookregister_initial_settingsunregister_taxonomyunregister_meta_key
引入
2.7.0
棄用
-

register_uninstall_hook: 這個函式允許你註冊一個函式,當外掛被解除安裝時將被呼叫: 當你需要清理你的外掛在安裝過程中建立的任何資料時,這很有用。

設定外掛的解除安裝鉤子。

註冊解除安裝鉤子,當使用者點選解除安裝連結時,該鉤子將被呼叫,該連結要求該外掛自行解除安裝。該連結將不會 該連結不會被啟用,除非該外掛鉤住了該動作。

在註冊解除安裝鉤子時,該外掛不應執行函式之外的任意程式碼。為了使用該鉤子執行,該外掛
將必須被包含在內,這意味著在解除安裝過程中,任何鋪設在函式之外的程式碼都將被執行。該外掛不應妨礙解除安裝過程。

如果不能在不執行外掛內的程式碼的情況下編寫外掛,那麼外掛應該在基本外掛資料夾中建立一個名為’uninstall.php’的檔案。這個檔案將被呼叫,如果它存在的話,在解除安裝過程中繞過解除安裝鉤。外掛在使用”uninstall.php”時,應該在執行前檢查”WP_UNINSTALL_PLUGIN”常量。

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

	/*
	 * 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' );
	$plugin_basename       = plugin_basename( $file );

	if ( ! isset( $uninstallable_plugins[ $plugin_basename ] ) || $uninstallable_plugins[ $plugin_basename ] !== $callback ) {
		$uninstallable_plugins[ $plugin_basename ] = $callback;
		update_option( 'uninstall_plugins', $uninstallable_plugins );
	}
}

常見問題

FAQs
檢視更多 >