2017-06-16 10 views
0

私は、ワードプレスでクラスをインスタンス化する必要があるため、get_post_types関数を使用して、 publish_postフック(私は、publish_CPTフックの周りにあると仮定します)。ここでpublish_CPTフックの前にget_post_typesを使用できるようにするクラスをインスタンス化するWordpressフック

はここに、これまで私が持っているコード

class Transient_Delete { 

    /** 
    * @var array of all the different post types on the site 
    */ 
    private $postTypes; 
    /** 
    * @var array of wordpress hooks we will have to assemble to delete all possible transients 
    */ 
    private $wpHooks; 

    public static function init() { 
     $class = __CLASS__; 
     new $class; 
    } 

    public function __construct() 
    { 
     $this->postTypes = array_values(get_post_types(array(), 'names', 'and')); 
     $this->wpHooks = $this->setWpHooks($this->postTypes); 
     add_action('publish_alert', array($this, 'deleteAlertTest')); 
    } 

    private function setWpHooks($postTypes) 
    { 
     $hooks = array_map(function($postType) { 
      return 'publish_' . $postType; 
     }, $postTypes); 

     return $hooks; 
    } 

    private function deleteAlertTest($post) 
    { 
     $postId = $post->ID; 
     echo 'test'; 
    } 
} 
add_action('wp_loaded', array('Transient_Delete', 'init')); 

別のノートで、これは、MU-pluginsディレクトリにあることです。

注:publish_alertの「警告」はカスタムの投稿タイプです。

答えて

0

これは私のせいです。私がdeleteAlertTest関数をpublicに変更すると、publish_alertフックがうまく動作するように見えます。なぜそれが私的な機能であるのかについてのアイデアは、その効果を持っていますか?同クラス内にあります。

+0

アクションをフックすると、そのアクションに渡された関数は参照なしで直接呼び出されます。関数がクラス内のプライベート関数である場合、それは実行アクションによって識別できません。オブジェクト参照でdeleteAlertTestを呼び出す必要があります。あるいは、どこでも使用できるpubilc関数をフックし、その関数内のオブジェクト参照を通じてこのプライベート関数を呼び出すことができます。 – Junaid

関連する問題