Site logo

Archived topic

Create a redirection for a page

24 replies · Started by esteban on September 3, 2019

Viewing posts 16–25 of 25

Still doesnt work...

What we can do?

Can you show us all the redirect functions you have added. After adding code in the forum, please highlight all the code and click the code button in the editor.

Sure,

1)

add_action( 'wp', 'redirect' );
function redirect() {
  if ( is_page('mi-cuenta') && !is_user_logged_in() ) {
      wp_redirect( home_url('/acceder') );
      die();
  }
}

2)

//Ocultar admin bar a todos los suscriptores
add_action('after_setup_theme', 'bld_ocultar_admin_bar');
function bld_ocultar_admin_bar() {
if (current_user_can('subscriber')) {
add_filter( 'show_admin_bar', '__return_false' );
}
}

3) This one is the one that do not work

add_action( 'template_redirect', function() {
    if ( is_page( 'cursos' ) && ! is_user_logged_in() ) {
        wp_redirect( home_url( '/acceder' ) );
        die();
    }
} );

For #3 try:

add_action( 'template_redirect', function() {
    if ( is_post_type_archive( 'sfwd-courses' ) && ! is_user_logged_in() ) {
        wp_redirect( home_url( '/acceder' ) );
        die();
    }
} );

I assume sfwd-courses is the name of your Courses archive?

Now it is working!

Thanks you so much for you support!

Glad to be of help!

Hi,
This code works great for me for a single page, but I'm looking to do it for multiple pages so I can set it once and forget about it.

Could I enable this for a content type instead of a single page?
Or if not, how about a wildcard? eg mysite.com/restricted-page/*

Thanks! Joe

Thank you David,

I am using a custom post type, so I can add post_type_exists from the conditional tags.

I had made a post type called 'Student Pages' for logged in students, so I can use this to redirect if they aren't logged in.

Thanks for the support! This is resolved.

Joe

Glad to hear that!

This archived topic is closed to new replies.