在离开woocommerce产品评论后重定向客户

时间:2015-04-09 13:14:41

标签: php wordpress redirect woocommerce comments

我想在woocommerce中留下产品评论后将用户重定向到感谢页面。我知道代码

  add_filter('comment_post_redirect', 'redirect_after_comment');
    function redirect_after_comment($location)
    {
    return $_SERVER["HTTP_REFERER"];
    }

会将所有评论重定向到某个位置,但我似乎无法找到过滤器或挂钩来仅指定woocommerce产品评论。有没有人这样做过?理想情况下,我只想重定向到标准的wordpress页面,以便将来可以为该页面模板文件添加选项和功能。

1 个答案:

答案 0 :(得分:1)

comment_post_redirect过滤器有第二个参数。此参数是$comment对象,我们可以从中获取帖子的ID。使用帖子的ID,您可以测试post_type并相应地调整返回的变量。

add_filter( 'comment_post_redirect', 'redirect_after_comment', 10, 2 );

function redirect_after_comment( $location, $comment ){
    $post_id = $comment->comment_post_ID;

    // product-only comment redirects
    if( 'product' == get_post_type( $post_id ) ){
        $location = 'http://www.woothemes.com';
    }
    return $location;
}