save_post にフックした関数が実行されない?
-
まったく狐につままれている思いです…
ひとつの条件分岐を経たために
save_post
に引っ掛けたアクションが実行されないことはありえますでしょうか?投稿タイプ、タクソノミーごとに特定のフォルダ内のクラスを実行するためのルーティング部分でひとつの条件分岐前に
add_action
した場合はフックした関数が実行されるのに、条件分岐の後にadd_action
した場合は関数が実行されません。
(実際は読み込んだ先でフックしているのですが、いろいろ検証するとこのルーティング部分にいきついたので)下記コードの
init_admin
メソッドの部分です。<php namespace WPDW; class Router { /** * Domain name * @var string */ private $ns; /** * カスタム投稿名orタクソノミー名 => ドメイン($rewite['slug']) * @var array */ private $domains_alias; /** * Input vars definition. use in admin. * @var array */ private static $def = [ 'post_type' => \FILTER_SANITIZE_ENCODED, 'taxonomy' => \FILTER_SANITIZE_ENCODED, 'post' => \FILTER_VALIDATE_INT, // and more ]; /** * Constructor * @access protected * @uses WPDW\Options * @return (void) */ protected function __construct() { $this->domains_alias = Options::get_domains_alias(); ! is_admin() ? $this->template_redirect() : $this->admin_init(); } /** * Frontend hook * @access private * @return (void) */ private function template_redirect() { // フロントエンド用のルーティング } /** * Admin hook * @access private * @return (void) */ private function admin_init() { add_action( 'admin_init', [ $this, 'admin_parse_request' ], 0 ); add_action( 'admin_init', [ $this, 'init_admin' ], 1 ); } /** * @access public * @return (void) */ public function admin_parse_request() { global $pagenow; $q = filter_input_array( \INPUT_GET, self::$def, false ) ?: []; switch ( $pagenow ) { case 'edit.php' : case 'post-new.php' : $maybe_domain = array_key_exists( 'post_type', $q ) ? $q['post_type'] : null; break; case 'post.php' : $maybe_domain = array_key_exists( 'post', $q ) ? get_post_type( $q['post'] ) : null; break; case 'edit-tags.php' : $maybe_domain = array_key_exists( 'taxonomy', $q ) ? $q['taxonomy'] : null; break; case 'index.php' : // _var_dump( 'Dashboard!!!!!' ); break; } if ( isset( $maybe_domain ) && array_key_exists( $maybe_domain, $this->domains_alias ) ) { $this->ns = $this->domains_alias[$maybe_domain]; $this->arguments = $q + [ 'domain' => $this->ns ]; } } /** * @access public * @return (void) */ public function init_admin() { // add_action( 'save_post', [ $this, 'save_post' ] ); // <- ここに入れると効く if ( ! $this->ns ) // <- ここでの条件分岐が原因と return; // しか考えられない。。。!? add_action( 'save_post', [ $this, 'save_post' ] ); // <- ここに入れると効かない $this->exec( 'admin' ); } public function save_post( $post_id ) { if ( defined( 'DOING_AUTOSAVE' ) && \DOING_AUTOSAVE ) return $post_id; var_dump( $post_id ); die(); } /** * @access private * @param string $cl * @return (void) */ private function exec( $cl ) { // 各クラスの初期化 } }
ちなみに
exec
メソッドはちゃんと実行されます。プラグインのコード全体を見なおしたあとでこのような状態になりました。
基本的な構造は見直し前と変わらないはずですが、見直し前にはこのような症状は発生しませんでした。下記gistに全体のコードを載せております。
どなたか解決策をご教示いただけませんでしょうか
2件の返信を表示中 - 1 - 2件目 (全2件中)
2件の返信を表示中 - 1 - 2件目 (全2件中)
- トピック「save_post にフックした関数が実行されない?」には新たに返信することはできません。