【问题标题】:Redirect To URL when a comment is approved in WordPress在 WordPress 中批准评论时重定向到 URL
【发布时间】:2018-06-28 18:33:24
【问题描述】:

我正在处理自定义帖子类型,其中帖子内容会使用已批准的评论内容进行更新。

我的代码是:-

add_action('comment_unapproved_to_approved', 'hgkb_update_post_content_on_comment_approval');
function hgkb_update_post_content_on_comment_approval($comment)
{
    $post_id = $comment->comment_post_ID;
    $comment = $comment->comment_content;
    $post_type=array('hgkb','hg-questions');
    if (in_array(get_post_type( $post_id ),$post_type))
    {
        $update_answer=wp_update_post( array('ID' => $post_id, 'post_content' => $comment) );
        if($update_answer)
        {
           //How to reload the page or redirect to another url?
        }
    }
}

一旦帖子内容更新,我希望内容立即反映在内容编辑器中或重新加载页面,以便用户可以看到所做的更改。

提前致谢。

【问题讨论】:

    标签: wordpress redirect hook


    【解决方案1】:

    我已经使用另一个钩子完成了它。于是代码变成了:-

    add_action('comment_unapproved_to_approved', 'hgkb_update_post_content_on_comment_approval');
    function hgkb_update_post_content_on_comment_approval($comment)
    {
        $post_id = $comment->comment_post_ID;
        $comment = $comment->comment_content;
        $post_type=array('hgkb','hg-questions');
        if (in_array(get_post_type( $post_id ),$post_type))
        {
            $update_answer=wp_update_post( array('ID' => $post_id, 'post_content' => $comment) );
            if($update_answer)
            {
               //How to reload the page or redirect to another url?
            }
        }
    }
    

    然后:-

    add_action('admin_footer-post.php', 'hgkb_reload_after_approval');
    function hgkb_reload_after_approval()
    {
        global $post;
        $post_type=array('hgkb','hg-questions');
        if (stristr( $_SERVER['REQUEST_URI'], 'post.php' ) !== false && is_object( $post ) && in_array(get_post_type( $post->ID ),$post_type) )
        {
            ?>
            <script>
                jQuery(document).ready(function(){
                // Reload after 1 second when a comment approved
                jQuery(document).on('click', '.vim-a', function (e) {
                alert("Answer Updated!! The page will be reloaded after you click on OK. ");
                setTimeout(function() {
                window.location.href = "<?php echo site_url(); ?>/wp-admin/post.php?post=<?php echo $post->ID; ?>&action=edit"
                }, 1000);
                });
                // END Reload after 1 second when a comment approved and Reload the Page when a comment approved
                });
            </script>
            <?php
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-21
      • 2021-06-03
      • 2012-02-04
      • 2011-11-19
      • 2018-07-30
      • 1970-01-01
      相关资源
      最近更新 更多