admin管理员组文章数量:1130349
While I'm developing a website and doing some testing I would like to block access to non-admin (and eventually non-editors) to specific pages such as the woocommerce shop and related pages.
Is there a way, without using plugins, in the functions.php to check:
$list_of_blocked_pages = [ 'shop', 'cart', 'checkout', 'etc...' ];
if ( current_page_is_in( $list_of_blocked_pages ) && !admin && !editor ) {
redirect_to_page( $url );
}
While I'm developing a website and doing some testing I would like to block access to non-admin (and eventually non-editors) to specific pages such as the woocommerce shop and related pages.
Is there a way, without using plugins, in the functions.php to check:
$list_of_blocked_pages = [ 'shop', 'cart', 'checkout', 'etc...' ];
if ( current_page_is_in( $list_of_blocked_pages ) && !admin && !editor ) {
redirect_to_page( $url );
}
Share
Improve this question
edited May 1, 2018 at 12:47
Mark Kaplun
23.8k7 gold badges43 silver badges65 bronze badges
asked May 1, 2018 at 11:33
Dim13iDim13i
1251 silver badge5 bronze badges
4
|
2 Answers
Reset to default 7You can use template_redirect action to redirect Specific users based on your terms. This action hook executes just before WordPress determines which template page to load. It is a good hook to use if you need to do a redirect with full knowledge of the content that has been queried.
You can use is_page function to check weather the current page is in your list of pages or not.
add_action( 'template_redirect', 'redirect_to_specific_page' );
function redirect_to_specific_page() {
if ( is_page('slug') && ! is_user_logged_in() ) {
wp_redirect( 'http://www.example.dev/your-page/', 301 );
exit;
}
}
I ended up solving like this:
function block_woocommerce_pages_for_guest() {
$blocked_pages = is_woocommerce() || is_shop() || is_cart() || is_checkout() || is_account_page() || is_wc_endpoint_url();
if( !current_user_can('administrator') && !current_user_can('editor') && $blocked_pages ) {
wp_redirect('/');
exit;
}
}
add_action( 'wp', 'block_woocommerce_pages_for_guest', 8 );
This way all non-admin and non-editor will be redirected to the homepage.
While I'm developing a website and doing some testing I would like to block access to non-admin (and eventually non-editors) to specific pages such as the woocommerce shop and related pages.
Is there a way, without using plugins, in the functions.php to check:
$list_of_blocked_pages = [ 'shop', 'cart', 'checkout', 'etc...' ];
if ( current_page_is_in( $list_of_blocked_pages ) && !admin && !editor ) {
redirect_to_page( $url );
}
While I'm developing a website and doing some testing I would like to block access to non-admin (and eventually non-editors) to specific pages such as the woocommerce shop and related pages.
Is there a way, without using plugins, in the functions.php to check:
$list_of_blocked_pages = [ 'shop', 'cart', 'checkout', 'etc...' ];
if ( current_page_is_in( $list_of_blocked_pages ) && !admin && !editor ) {
redirect_to_page( $url );
}
Share
Improve this question
edited May 1, 2018 at 12:47
Mark Kaplun
23.8k7 gold badges43 silver badges65 bronze badges
asked May 1, 2018 at 11:33
Dim13iDim13i
1251 silver badge5 bronze badges
4
- Have you looked into codex.wordpress/Conditional_Tags? – Beee Commented May 1, 2018 at 11:47
-
@Beee My problem is not with what conditions to use, but where? Is there a specific
hookto use in thefunctions.phpfile? Essentially I would like to know what is the right and clean way to do that. – Dim13i Commented May 1, 2018 at 12:35 -
@mark-kaplun and @fuxia may I ask why you removed tags such as
woocommerceandroutingas these are key points in answering this questions? Woocommerce pages cannot be "password protected" or made "private" through the page's publishing options. This issue might have also been solved on theroutinglevel I guess. Your clarification could help me in being more precise and correct next time. – Dim13i Commented May 1, 2018 at 13:11 - many people ignore the WC tag, as WC is off topic but still generate a lot of noice. your question was just very partially specific to WC... – Mark Kaplun Commented May 1, 2018 at 14:36
2 Answers
Reset to default 7You can use template_redirect action to redirect Specific users based on your terms. This action hook executes just before WordPress determines which template page to load. It is a good hook to use if you need to do a redirect with full knowledge of the content that has been queried.
You can use is_page function to check weather the current page is in your list of pages or not.
add_action( 'template_redirect', 'redirect_to_specific_page' );
function redirect_to_specific_page() {
if ( is_page('slug') && ! is_user_logged_in() ) {
wp_redirect( 'http://www.example.dev/your-page/', 301 );
exit;
}
}
I ended up solving like this:
function block_woocommerce_pages_for_guest() {
$blocked_pages = is_woocommerce() || is_shop() || is_cart() || is_checkout() || is_account_page() || is_wc_endpoint_url();
if( !current_user_can('administrator') && !current_user_can('editor') && $blocked_pages ) {
wp_redirect('/');
exit;
}
}
add_action( 'wp', 'block_woocommerce_pages_for_guest', 8 );
This way all non-admin and non-editor will be redirected to the homepage.
本文标签: Redirect guest if he tries to access a specific page
版权声明:本文标题:Redirect guest if he tries to access a specific page 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749155661a2324546.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


hookto use in thefunctions.phpfile? Essentially I would like to know what is the right and clean way to do that. – Dim13i Commented May 1, 2018 at 12:35woocommerceandroutingas these are key points in answering this questions? Woocommerce pages cannot be "password protected" or made "private" through the page's publishing options. This issue might have also been solved on theroutinglevel I guess. Your clarification could help me in being more precise and correct next time. – Dim13i Commented May 1, 2018 at 13:11