在 Woocommerce 中,我试图找到一种解决方案来检查用户是否登录自定义页面,如果是,则将用户重定向到“我的帐户”页面。
对此的任何帮助表示赞赏。
在 Woocommerce 中,我试图找到一种解决方案来检查用户是否登录自定义页面,如果是,则将用户重定向到“我的帐户”页面。
对此的任何帮助表示赞赏。
尝试以下操作,您将在其中替换'some-page'为您的真实页面 ID、slug 或名称。该代码将针对已定义的特定页面登录用户重定向到我的帐户页面:
add_action('template_redirect', 'specific_logged_in_redirect');
function specific_logged_in_redirect() {
if ( is_page('some-page') && is_user_logged_in() ) {
wp_redirect( get_permalink( get_option('woocommerce_myaccount_page_id') ) );
exit();
}
}
代码位于您的活动子主题(或活动主题)的 function.php 文件中。测试和工作。
对于 2 页,您将使用:is_page( array( 'some-page', 'some-other' ) )
您应该使用 $_SESSION。这可以帮助您验证用户是否在页面中登录。
if(isset($_SESSION['UserID'])){
header('Location: [url]');
}
使用 WordPress:
$current_user = wp_get_current_user();
if ( 0 != $current_user->ID ) {
$template = get_page_template_slug($post->ID);
if($template == "your_custom_template_name"){
wp_redirect( wp_login_url() )
}
}