最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Detect what link user clicks and Redirect to a specific page for logged in users only

matteradmin8PV0评论

I have a link in my WP website Account link. I would like this to work normally for logged out users. However for Logged-in users i would like this link to take them to another page url.

I have tried:

 function my_logged_in_redirect_sub() {
    if ( is_user_logged_in() &&($_GET['account'] )  
) {
        wp_redirect( get_permalink( 15498 ) );//the redirect page id
    die;
}
}


 add_action( 'template_redirect', 'my_logged_in_redirect_sub' );

The $_GET['url'] doesn't work

Question: How can i target a specific URL with PHP?

I have a link in my WP website Account link. I would like this to work normally for logged out users. However for Logged-in users i would like this link to take them to another page url.

I have tried:

 function my_logged_in_redirect_sub() {
    if ( is_user_logged_in() &&($_GET['account'] )  
) {
        wp_redirect( get_permalink( 15498 ) );//the redirect page id
    die;
}
}


 add_action( 'template_redirect', 'my_logged_in_redirect_sub' );

The $_GET['url'] doesn't work

Question: How can i target a specific URL with PHP?

Share Improve this question edited Dec 6, 2018 at 11:37 xbass540 asked Dec 6, 2018 at 10:29 xbass540xbass540 1336 bronze badges 1
  • 1 Possible duplicate of How to get URL of current page displayed? – Jacob Peattie Commented Dec 6, 2018 at 10:31
Add a comment  | 

1 Answer 1

Reset to default 0

Here are 2 examples which you will need to modify slightly to get it working for your specific needs.

add_action( 'admin_init', 'redirect_non_logged_users_to_specific_page' );

function redirect_non_logged_users_to_specific_page() {

if ( !is_user_logged_in() && is_page('add page slug or ID here') && $_SERVER['PHP_SELF'] != '/wp-admin/admin-ajax.php' ) {

wp_redirect( 'http://www.example.dev/page/' ); 
    exit;
   }
}

Put this in your child theme functions file, change the page ID or slug and the redirect url.

You could also use code like this:

add_action( 'template_redirect', 'redirect_to_specific_page' );

function redirect_to_specific_page() {

if ( is_page('slug') && ! is_user_logged_in() ) {

wp_redirect( 'http://www.example.dev/your-page/', 301 ); 
  exit;
    }
}

You can add the message directly to the page or if you want to display the message for all non logged in users, add it to the code.

http://codex.wordpress/Function_Reference/wp_redirect

Post a comment

comment list (0)

  1. No comments so far