Redirect guest if he tries to access a specific page

admin2025-06-06  2

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 hook to use in the functions.php file? 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 woocommerce and routing as 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 the routing level 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
Add a comment  | 

2 Answers 2

Reset to default 7

You 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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749155686a316838.html

最新回复(0)