WordPress includes some really handy pluggable functions one of which is
wp_redirect()
This function can easily be written into a custom function with a conditional tag and hook to create a 301 redirect.
Here’s a working example which redirects from a page with the slug ‘shop’ to the blog page:
add_action( 'template_redirect', 'redirect_to_blog_page' );
function redirect_to_blog_page() {
if ( is_page('shop') && ! is_user_logged_in() ) {
wp_redirect( 'http://www.example.dev/blog/', 301 );
exit;
}
}
You can also use code like this to redirect to the home page.
add_action( 'template_redirect', 'redirect_to_home_page' );
function redirect_to_home_page() {
if ( is_page('shop') && ! is_user_logged_in() ) {
wp_redirect( home_url(), 301 );
exit;
}
}
You don’t need to use a 301 redirect with this function if you don’t want. You can use the wp_redirect() function for any type of redirect.
Brad, you post some of the most useful, creative stuff.
Thanks got sharing!
Thanks Chad