这是到目前为止我得到的最好的结果,但我得到了很多错误。
从主要问题开始;它确实重定向到正确的url,但页面没有显示任何内容。
这就是我正在尝试的:Reference code
这是我的代码。
function pre_process_shortcode() {
if (!is_singular()) return;
global $post;
$user = wp_get_current_user();
$product_id = 27;
$product_bought = wc_customer_bought_product( $user->user_email, $user->ID , $product_id );
if (!empty($post->post_content)) {
$regex = get_shortcode_regex();
preg_match_all('/'.$regex.'/',$post->post_content,$matches);
if (!empty($matches[2]) && in_array('protactPage',$matches[2]) && $product_bought && is_user_logged_in()) {
wp_redirect('https://learndigital-mena.com/cart-2/');
exit();
} else {
// login form or redirect to login page
wp_redirect('https://learndigital-mena/my-account/');
exit();
}
}
}
add_action('template_redirect','pre_process_shortcode',1);发布于 2020-12-20 04:19:40
注意,这只是客户端,而不是服务器端:
您可以尝试将wp_redirect('https://learndigital-mena/my-account/');替换为echo '<script>window.location="https://learndigital-mena/my-account/"</script>';或header("Location: https://learndigital-mena/my-account/"),但标头位置将位于脚本的顶部。
https://stackoverflow.com/questions/65373993
复制相似问题