register_activation_hook

函式
register_activation_hook ( $file, $callback )
引數
  • (string) $file The filename of the plugin including the path.
    Required:
  • (callable) $callback The function hooked to the 'activate_PLUGIN' action.
    Required:
定義位置
相關方法
register_deactivation_hookregister_taxonomyunregister_taxonomyregister_uninstall_hookshutdown_action_hook
引入
2.0.0
棄用
-

register_activation_hook: 這是一個WordPress的動作鉤子,用來註冊一個回撥函式,當一個外掛被啟用時將被觸發。這個鉤子對於執行那些在外掛第一次啟用時只需要做一次的任務很有用,比如建立自定義資料庫表或初始化預設設定。

設定一個外掛的啟用鉤子。

當一個外掛被啟用時,’activation_PLUGINNAME’鉤子被呼叫。在這個鉤子的名稱中,PLUGINNAME被替換為該外掛的名稱,包括可選的子目錄。例如,當外掛位於wp-content/plugins/sampleplugin/sample.php,那麼這個鉤子的名字將變成’activate_sampleplugin/sample.php’。

當外掛只有一個檔案,並且(預設情況下)位於wp-content/plugins/sample.php中時,這個鉤子的名稱將是’activate_sample.php’。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function register_activation_hook( $file, $callback ) {
$file = plugin_basename( $file );
add_action( 'activate_' . $file, $callback );
}
function register_activation_hook( $file, $callback ) { $file = plugin_basename( $file ); add_action( 'activate_' . $file, $callback ); }
function register_activation_hook( $file, $callback ) {
	$file = plugin_basename( $file );
	add_action( 'activate_' . $file, $callback );
}

常見問題

FAQs
檢視更多 >