Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Daniel Racowsky
Daniel Racowsky
1,574 Points

Redirecting ONLY logged in, non-administrators for a specific page in Wordpress/Woocommerce?

Hi there,

I run an online store, www.vapefalcon.com.

I am trying to redirect already logged in users from page A, if they attempt to view it, to page B.

In other words, I don't want already logged in users to see page A, but instead, page B.

However, I want non-logged in users to be able to access page A if they wish.

The site runs on WordPress and WooCommerce.

Any suggestions? I've tried searching Google for various scripts and plugins, but no dice. My programming skills are very limited thus far.

Thanks!

Daniel

2 Answers

Casey Ydenberg
Casey Ydenberg
15,622 Points

Something like this in your functions.php:

<?php
function redirect_customers()
{
    if ( is_page('redirect-from') && is_user_logged_in() && ! current_user_can('publish_posts') ) {
        wp_redirect( home_url( '/redirect-to/' ) );
        exit();
    }
}
add_action( 'template_redirect', 'redirect_customers' );
?>

You might want to change 'publish_posts' to one of WooCommerce's capabilities, but it may not matter.

You also may need to change is_page() depending on the nature of where you're redirecting from - you may need to use a different conditional tag. Post back if you need more help.

Daniel Racowsky
Daniel Racowsky
1,574 Points

Thanks so much!

The snippet you provided worked.

I only needed to include the page id for ( is_page( PAGEID HERE ) instead of the URL. Using the URL didn't seem to work.

Thanks again. Cheers!