I have a page with mixed content in three different tabs. The first tab is open access to all but the content of the last two are for logged in users only so by sending the users to login with:

<p>You need to be logged in in order to view this content. Would you like to <a href="<?php echo esc_url(wp_login_url()); ?>"><?php _e('log in') ?></a> now?</p>

I'm then handling this in functions.php by using the login_form action where I'm declaring the wanted redirect url with a redirect_to (see code block).

How do I limit this action to only work from one specific page? I've tried using the code below but it only works without the if (is_page()) part. I need it to work only when user clicks on the link from this page.

function my_login_redirect()
    global $redirect_to;
  // Check if the page is "about us"
    if (is_page('about-us')) {
        if (!isset($_GET['redirect_to'])) {
            $redirect_to = site_url('/about-us');
        } else {
            $redirect_to = $_GET['redirect_to'];

add_action('login_form', 'my_login_redirect');

Any help most appreciated! Thank you

  • Why not just add the redirect_to parameter when outputting the login link? Commented Jul 2, 2021 at 11:14
  • @JacobPeattie How would that work now again? :) I'm sorry I'm actually a designer and not a php developer so if you don't mind I would love the help. Thank you!
    – Seblito
    Commented Jul 2, 2021 at 11:19

1 Answer 1


In your login link you call the WordPress function wp_login_url() to generate the login URL. Mr Peattie's question in the comments refers to the documented parameter redirect supported by this function. When in doubt, always check the documentation: https://developer.wordpress.org/reference/functions/wp_login_url/

There are actually two parameters for wp_login_url() but for the purpose of your question, the first one is all you need to provide.

Making use of the redirect parameter will tell WordPress where to send a user that completes login successfully. Assuming your link to the login form is coming from a template for the About Us page that you wish to return to, the current page permalink can be used as the value for the parameter like this:

wp_login_url( get_permalink() )

Look at the output of that command and you will see the query parameter on the login URL that is output by WordPress.

If you are using the login URL function somewhere else and need the About Us page as the redirect target, simply enter it as a string, like this:

wp_login_url( '/about-us' ) )

For a better user experience, check the current user's login status before showing the login link. Putting that together with the rest of your code:

<?php if ( ! is_user_logged_in() ) : ?>
    <p>You need to be logged in in order to view this content. Would you like to <a href="<?php echo esc_url( wp_login_url( '/about-us' ) ); ?>"><?php _e('log in') ?></a> now?</p>
<?php endif; ?>
  • Thank you very very much @jmd2112 ! I've searched through this forum for days and read the developer docs. I even came across this one function but didn't understand how to actually use the redirect parameter. Unbelievable how things like these can be that easy but then again, everything is easy when you have the right knowledge. Again, thank you!
    – Seblito
    Commented Jul 3, 2021 at 6:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.