【问题标题】:Show message after new post is published (WordPress)发布新帖子后显示消息(WordPress)
【发布时间】: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); 

}

就像我之前说的,所有这些代码都可以正常工作,但是通知没有出现。我想是因为按下发布按钮后页面刷新。

【问题讨论】:

    标签: php wordpress oop hook


    【解决方案1】:

    我找到了解决方案。我正在检查发布日期并将其与今天的日期进行比较。如果帖子今天发布,它会触发钩子。在我的情况下,一天很好。如果您只需要在发布后显示它,您也可以将其调整为检查时间。

            global $pagenow;
            if ( $pagenow == 'post.php' ) {
                $currentPost = get_post( $_GET['post'] );
                $postdate = substr($currentPost->post_date, 0 , 10); 
    
                //if  post type is post and post is published today show info message
                if($currentPost->post_type == 'post' && $postdate == date('Y-m-d')) {
                    add_action( 'admin_notices', array($this, 'info_text_news') );
                }
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-26
      • 2013-12-11
      • 1970-01-01
      相关资源
      最近更新 更多