0

I have created one page on my WordPress that I want to set only woocommerce user can view it. That mean they must logged in first to be able to view it. For unregistered user if they try to access to that page, we will redirect them to my-account page.

I found this similar solution but this only work for woocommerce pages but how to insert to code for wordpress pages. This is the code:

function wpse_131562_redirect() {
    if (
        ! is_user_logged_in()
        && (is_woocommerce() || is_cart() || is_checkout())
    ) {
        // feel free to customize the following line to suit your needs
        wp_redirect(site_url('my-account/'));
        exit;
    }
}

add_action('template_redirect', 'wpse_131562_redirect');

Let say my page is mydomainname.com/wordpress-pages

How to be able to restrict that pages? looking forward your help.

1 Answer 1

0

Add your page slug in if condition too.

function wpse_131562_redirect() {
    if (! is_user_logged_in()
        && (is_woocommerce() || is_cart() || is_checkout() || is_page('wordpress-pages'))
    ) {
        // feel free to customize the following line to suit your needs
        wp_redirect(site_url('my-account/'));
        exit;
    }
}

add_action('template_redirect', 'wpse_131562_redirect');
1
  • Sumit, your solution work perfectly. Thanks you very much for your kind help.
    – kunato
    Commented Feb 17, 2016 at 10:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.