plugins - On button click, redirect users to registration page instead of another page

admin2025-01-07  5

I have a button that I have that leads users to a page on WordPress called /portfolio/, so is there a way that I can redirect users to the /registration/ page until their logged in?

I'm using a plugin on WordPress that uses the below code to change the menu per user logged in/logged out.

function my_wp_nav_menu_args( $args = '' ) {

if( is_user_logged_in() ) { 
    $args['menu'] = 'logged-in';
} else { 
    $args['menu'] = 'logged-out';
} 
    return $args;
}
add_filter( 'wp_nav_menu_args', 'my_wp_nav_menu_args' );

Is there a way that I can use the if( is_user_logged_in() ) to do the same with a redirect? I can't seem to lock the page down to all users.

I have a button that I have that leads users to a page on WordPress called /portfolio/, so is there a way that I can redirect users to the /registration/ page until their logged in?

I'm using a plugin on WordPress that uses the below code to change the menu per user logged in/logged out.

function my_wp_nav_menu_args( $args = '' ) {

if( is_user_logged_in() ) { 
    $args['menu'] = 'logged-in';
} else { 
    $args['menu'] = 'logged-out';
} 
    return $args;
}
add_filter( 'wp_nav_menu_args', 'my_wp_nav_menu_args' );

Is there a way that I can use the if( is_user_logged_in() ) to do the same with a redirect? I can't seem to lock the page down to all users.

Share Improve this question asked Feb 18, 2019 at 20:07 user155484user155484
Add a comment  | 

1 Answer 1

Reset to default 0

You'll need to find out what theme template file is being used to display the /portfolio/ page - page.php, page-portfolio.php, tpl-portfolio.php, or something along those lines.

Then, if you don't already have a child theme, create one.

Then, copy the template file into your child theme, and at the very top - before anything else - add:

// If the user is not logged in
if( !is_user_logged_in() ) {
    // send them to the login screen
    // once they log in, they will automatically return to this page
    auth_redirect();
} else {
    // move all your other code into this condition
}

As the comments note, if the user isn't logged in, they'll be redirected to the login page. Once they are logged in, they'll be redirected back to the portfolio page.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736260898a698.html

最新回复(0)