我有一个问题,我需要在 wordpress 中实例化一个类,以便在构造函数中我可以使用函数 get_post_types 并在 publish_post 钩子之前发生该钩子(我假设它是在 publish_CPT 钩子周围)。
这是我到目前为止的代码
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 的“alert”是自定义帖子类型。