在提交后添加一个简单的JS警报框

时间:2016-05-25 作者:Daniel Klose

我想在客户添加产品进行审查时添加一个简单的Javascript警报框。但是,使用post状态转换挂钩,会在弹出窗口后创建一个白色页面。是否有在页面重新加载后触发的挂钩?这是我当前的代码:

//JS alert on submit product
    function notify_me_for_pending() {
      $notice = __(\'Thank you for your submission. Your product will be available for purchase as soon as we have approved it!\', \'pressive-child\');
      echo \'alert("\'.$notice.\'");\';
    }
    add_action( \'draft_to_pending\', \'notify_me_for_pending\' );
    add_action( \'auto-draft_to_pending\', \'notify_me_for_pending\' );
    add_action(\'new_to_pending\', \'notify_me_for_pending\');

1 个回复
最合适的回答,由SO网友:Sumit 整理而成

注意,当post更新时,WordPress会从/wp-admin/post.php/wp-admin/post.php?post=41&action=edit 这意味着它会加载两次。因此,在打印内容之前,将跳过转换状态挂钩上的操作。

Solution:-

<在过渡挂钩回调函数存储(post meta)中显示js弹出窗口的一些标志,将新函数绑定到admin_head 挂钩检查是否有标志,显示弹出和重置标志

//Update post meta to display alert
function add_js_for_pending($post) {
    update_post_meta($post->ID, \'trigger_notice\', TRUE);
}
add_action( \'draft_to_pending\', \'add_js_for_pending\' );
add_action( \'auto-draft_to_pending\', \'add_js_for_pending\' );
add_action(\'new_to_pending\', \'add_js_for_pending\');

//JS alert on submit product

function notify_me_for_pending() {
    global $post;
    $current_screen = get_current_screen();
    //Check if we need to display alert
    if ($current_screen->base == \'post\' && get_post_meta($post->ID, \'trigger_notice\', TRUE)) {
        $notice = __(\'Thank you for your submission. Your product will be available for purchase as soon as we have approved it!\', \'pressive-child\'); ?>
        <script type="text/javascript">
            <?php echo \'alert("\'.$notice.\'");\'; ?>
        </script><?php
         delete_post_meta($post->ID, \'trigger_notice\'); //Alert is done now remove it.
    }
}
add_action(\'admin_head\', \'notify_me_for_pending\');

相关推荐