【发布时间】:2019-10-02 20:08:15
【问题描述】:
在 WordPress 中发布新帖子时,我想显示额外的 admin_notice 以显示自定义消息。我为此使用了admin_notices 钩子。我对此进行了测试,并且可以正常工作。不起作用的是在帖子发布时显示它。
我尝试了几种方法,包括 ' publish_post' 钩子。钩子正在触发,当我输入var_dump('bla');exit(); 时,我可以看到它正在工作。我认为 admin_notices 未显示的原因是页面正在刷新。所以如果有像'after_publish_post' 这样的东西会很棒但是我找不到它。
我还尝试了在类似问题中发现的一些东西:
if( ( $_POST['post_status'] == 'publish' ) && ( $_POST['original_post_status'] != 'publish' ) ) { echo "New post!"; exit();}
这也没有效果
--
我此刻拥有的是:
add_action('publish_post', array($this,'onInsertPost')); //does work, function gets triggered
public function onInsertPost($post_id, $post, $update) {
add_action( 'admin_notices', array($this, 'info_text_news') );
}
public function info_text_news() {
$class = 'notice notice-info';
$message = __( 'My message', 'admin_notice' );
printf( '<div class="%1$s"><p>%2$s</p></div>', esc_attr( $class ), $message);
}
就像我之前说的,所有这些代码都可以正常工作,但是通知没有出现。我想是因为按下发布按钮后页面刷新。
【问题讨论】: