在WooCommerce注册后自定义重定向

时间:2017-07-01 作者:Kajal Solanki

用户使用WooCommerce的注册表进行注册后,我想将他们重定向到自定义页面,例如我的其他网站,而不是my-account

add_action(\'woocommerce_registration_redirect\', \'ps_wc_registration_redirect\',10); 
function ps_wc_registration_redirect( $redirect_to ) {  
    $redirect_to ="http://example.com";
    return $redirect_to;
}
当目标是当前站点上的页面时,上面的挂钩成功重定向用户,但当重定向位置离开当前站点时,它不起作用。

在用户注册重定向发生后,是否有其他可用的挂钩?

2 个回复
最合适的回答,由SO网友:Dave Romsey 整理而成

在内部,WooCommerce使用WordPress\'wp_safe_redirect() 不允许重定向到外部主机。为了解决这个问题,我们必须将我们想要的主机添加到白名单中。可以使用allowed_redirect_hosts 具体如下:

/**
 * Adds example.com to the list of allowed hosts when redirecting using wp_safe_redirect()
 *
 * @param array       $hosts An array of allowed hosts.
 * @param bool|string $host  The parsed host; empty if not isset.
 */
add_filter( \'allowed_redirect_hosts\', \'wpse_allowed_redirect_hosts\', 10, 2 );
function wpse_allowed_redirect_hosts( $hosts, $host ) {
    $hosts[] = \'example.com\';

    return $hosts;
}
将上述代码与原始代码一起使用(根据需要自定义主机),以允许WooCommerce用户在完成注册过程后重定向到外部域。

SO网友:Regolith

尝试按以下方式设置该功能,如果手动设置该功能中的链接,则该功能也不需要参数

function custom_registration_redirect_after_registration() {
    return home_url(\'/post_slug_or_page_slug\');
}
add_action(\'woocommerce_registration_redirect\', \'custom_registration_redirect_after_registration\', 2);

结束

相关推荐

如何使用EMPLATE_REDIRECT传递消息

我使用以下函数使用自定义字段将帖子重定向到另一个帖子。在我添加line#8 要传递ID以便我可以将其抓取到帖子中(重定向到的位置),并显示一条自定义消息,您将从该特定帖子重定向:function project_do_redirect() { if( !is_singular( \'mycpt\' ) && !is_single() ) return; global $post; $redirect_post