Last active
December 30, 2015 14:09
-
-
Save miya0001/7840745 to your computer and use it in GitHub Desktop.
WordPressに独自のURLこの例では、/json/を追加する際のサンプル。
プラグインの無効化時にルールをキャッシュから削除 & 他のプラグインや管理者の操作によってflush_rewrite_rules()が発火した際に再登録を行う。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// プラグインの有効化時/無効化時の処理を登録 | |
register_activation_hook( __FILE__ , 'my_activation_callback' ); | |
register_deactivation_hook( __FILE__ , 'my_deactivation_callback' ); | |
// 他のプラグインや管理者の操作によってflush_rewrite_rules()が発火した際にこのプラグイン用のrewrite ruleを再登録する | |
add_action( 'delete_option', 'my_delete_option', 10, 1 ); | |
// 有効化時の処理 | |
function my_activation_callback() { | |
/* | |
* プラグインが有効化されていることをオプションに保存する | |
* この時点でis_plugin_active()の戻り値はfalse | |
*/ | |
update_option( 'my_plugin_activated', true ); | |
flush_rewrite_rules(); | |
} | |
// 無効化時の処理 | |
function my_deactivation_callback() { | |
/* | |
* プラグインが無効化された! | |
* この時点でis_plugin_active()の戻り値はtrue | |
*/ | |
delete_option( 'my_plugin_activated' ); | |
flush_rewrite_rules(); | |
} | |
// delete_optionフックのコールバック関数 | |
function my_delete_option($option){ | |
/* | |
* flush_rewrite_rules()が発火&プラグインが有効化されている場合に限りrewrite ruleを再登録 | |
* register_activation_hook()発火時にはまだis_plugin_active()の戻り値はtrueのままなのでget_option()の値で評価する必要がある。 | |
*/ | |
if ( 'rewrite_rules' === $option && get_option('my_plugin_activated') ) { | |
add_rewrite_endpoint( 'json', EP_ROOT ); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment