$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Redirect guest if he tries to access a specific page|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Redirect guest if he tries to access a specific page

matteradmin8PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far