Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

login link with auto_redirect

You can try one of these 2 solutions.
The first solution create a shortcode that returns a url that you can put on your button, teh second creates the entire button and url for the login. If the person is already logged in nothing shows, if not the button shows.
The first option shows the button even if hte person is alreayd logged in, to fix that you can target your button using css to hide it if the user is not logged in.
Create the button and give it a classname or id like this:

Then you can hide that button of the person is already logged in:

.logged-in .my_custom_login_button {
   display: none;
}

That will hide the button to a person who is logged in. If someone is logged in on WordPress the .logged-in class is applied to the body tag.
You can also hide a button to someone that is not logged in like this:

/* Hide the button by default */
.your-button-class {
    display: none;
}

/* display the button when a user is logged in */
.logged-in .your-button-class {
    display: inline-block; /* or block, inline, flex, etc., depending on your layout */
}

Step 1: Create a Shortcode Function
Add this code to your theme’s functions.php file or in your custom plugin file. This function will generate the login URL with the redirect to the current page.

function wpb_custom_login_shortcode() {
    // Check if the user is already logged in
    if ( is_user_logged_in() ) {
        return ''; // Return empty if the user is logged in
    }

    $redirect_url = urlencode( get_permalink() ); // Get the current page URL
    $login_url = wp_login_url( $redirect_url ); // Create the login URL with the redirect

    return esc_url( $login_url ); // Return the login URL
}
add_shortcode( 'custom_login_url', 'wpb_custom_login_shortcode' );

Step 2: Use the Shortcode
Now you can use the shortcode [custom_login_url] in your posts, pages, or widgets. To include it in an existing button, you would typically do something like this in your editor:

<a href="[custom_login_url]" class="your-button-class">Login</a>

Or, if you are using a drag and drop page editor that has a button you can put the shortcode in the link part of the button: [custom_login_url]

If you’re using a page builder like Elementor, you can insert the shortcode in a text or HTML widget and link it to your button.

Step 3: Ensure Proper Rendering
Some page builders or contexts might not automatically parse the shortcode when used in attributes. If the shortcode doesn’t work directly in the href attribute, you might need to use it in the content area and then use JavaScript to set the button’s href. Here’s a basic example:

jQuery(document).ready(function($) {
    var loginUrl = $('#login-url').text(); // Assume the shortcode is in an element with ID 'login-url'
    $('.your-button-class').attr('href', loginUrl); // Set the href of your button
});

And in your WordPress content:

<span id="login-url" style="display:none;">[custom_login_url]</span>

Step 4: Styling and Testing
Ensure your button is styled according to your site’s design.
Test the shortcode in various scenarios to ensure it works as expected.
This approach allows you to use the [custom_login_url] shortcode wherever you need the dynamic login URL, giving you the flexibility to add it to any existing button or link on your site.


Ok, so now that I have said all that if it were me I would create a shortcode that shows a button if the person is not logged in, otherwise nothing shows. here is how to do that:

Step 1: Add Shortcode Function to Your Theme’s functions.php or Custom Plugin
Open your theme’s functions.php file or a custom plugin file, and add the following code:

function wpb_custom_login_button_shortcode() {
    // Check if the user is already logged in
    if ( is_user_logged_in() ) {
        return ''; // Do not display anything if the user is logged in
    }

    // Define the URL to redirect after login
    $redirect_url = urlencode( get_permalink() );
    $login_url = wp_login_url( $redirect_url );

    // Define the HTML for the button
    $button_html="<a href="" . esc_url( $login_url ) . '" class="custom-login-button">Login</a>';

    return $button_html;
}
add_shortcode( 'custom_login_button', 'wpb_custom_login_button_shortcode' );

In this code, the wpb_custom_login_button_shortcode function checks if the user is logged in. If not, it creates a login button with a URL that redirects back to the current page after login.

Step 2: Style the Button
Add CSS to style your button. You can add this in your theme’s style.css file:

.custom-login-button {
    /* Add your button styling here */
    padding: 10px 20px;
    background-color: #0073aa;
    color: #ffffff;
    text-decoration: none;
    border-radius: 5px;
    display: inline-block;
}

To create a shortcode in WordPress that outputs a complete HTML button for login, which only displays when a user is not logged in, follow these steps:

Step 1: Add Shortcode Function to Your Theme’s functions.php or Custom Plugin
Open your theme’s functions.php file or a custom plugin file, and add the following code:

function wpb_custom_login_button_shortcode() {
    // Check if the user is already logged in
    if ( is_user_logged_in() ) {
        return ''; // Do not display anything if the user is logged in
    }

    // Define the URL to redirect after login
    $redirect_url = urlencode( get_permalink() );
    $login_url = wp_login_url( $redirect_url );

    // Define the HTML for the button
    $button_html="<a href="" . esc_url( $login_url ) . '" class="custom-login-button">Login</a>';

    return $button_html;
}
add_shortcode( 'custom_login_button', 'wpb_custom_login_button_shortcode' );

In this code, the wpb_custom_login_button_shortcode function checks if the user is logged in. If not, it creates a login button with a URL that redirects back to the current page after login.

Step 2: Style the Button
Add CSS to style your button. You can add this in your theme’s style.css file:

.custom-login-button {
    /* Add your button styling here */
    padding: 10px 20px;
    background-color: #0073aa;
    color: #ffffff;
    text-decoration: none;
    border-radius: 5px;
    display: inline-block;
}

Adjust the styling as needed to match your site’s design.

Step 3: Use the Shortcode
You can now use the [custom_login_button] shortcode in your posts, pages, or widgets. When added, it will display the login button if the user is not logged in. If the user is logged in, nothing will be displayed.

Example of using the shortcode in a post or page:

[custom_login_button]

Step 4: Test the Shortcode
Check the shortcode in different scenarios (logged in and logged out) to ensure it behaves as expected.
Verify the button styling and functionality on various devices and browsers.
This shortcode provides a simple way to add a login button to your WordPress site, which is only visible to logged-out users.

Related Posts:

  1. Redirect user to original url after login?
  2. Login redirect to previous page
  3. Redirect to referring page after logging in
  4. check first time login only
  5. How to change the default logout link on WordPress Admin
  6. How to redirect after login, the working way?
  7. Password change when the user login first time
  8. Custom login form redirect to external site
  9. handling login/logout redirects
  10. Restrict Access to wp-login.php
  11. Using `auth_redirect` : keeps asking me to login even when I’m logged in
  12. Redirect users after first login
  13. Redirect to current page after wordpress login
  14. wp-login.php — redirect logged in users to custom URL
  15. Redirect user after login/registration globally
  16. Redirect to login page
  17. You do not have sufficient permissions to access this page
  18. Redirect user after logout
  19. how to do logout redirect to current url
  20. How to redirect user to specific page based on the input password
  21. How do I redirect a user when he is logged in after clicking on a menu
  22. How to modify the Register link in the login page?
  23. Changing “Lost Password Email Link” to custom password reset page
  24. How to redirect user to a specific page based on username?
  25. Redirect after password update
  26. How to redirect user after login to a specific page?
  27. Redirect Logged In User if page is wp-login.php and $_Get[‘level’] = X
  28. Redirect when accessing /login when logged in
  29. How to redirect after login getting a variable from url (for example with the language)
  30. If User is NOT Logged in and Page ID is not 6 – Redirect to Login
  31. Redirect to Current Page after Login
  32. How to include error message on login
  33. Login redirect problem
  34. How to redirect on login to a specific page if a specific meta user is empty
  35. redirect Login problems
  36. Redirect user to previous page after signup from custom form
  37. How can I force the user to log in, even if they’re already authenticated?
  38. User is logged in after Stripe payment, then redirected, but is then logged out again
  39. Login Redirect – Multiple Scenarios
  40. WooCommerce – Redirect to a product after login
  41. Usage of the login_redirect filter
  42. Login/Register redirect user with message
  43. Redirect user to original url after login?
  44. Prevent users from going to wordpress profile after login
  45. Custom form redirect after login
  46. Custom redirect user after login based on metadata
  47. Custom Login Form – Redirect user to login page if not logged in
  48. Redirection on Custom page
  49. Redirect user to specific link after login
  50. Load function with login_redirect
  51. Prevent “/login” to redirect to “/wp-login”
  52. Redirect User to login page
  53. All URLs Redirect to Main Page
  54. How to redirect user after login to the page called by his first name?
  55. redirect doesn’t work while using example code
  56. Why I am not able to direct user to my blog section without login?
  57. Redirect After Login Error
  58. Redirect certain pages if user logged in
  59. Force User Login
  60. wp_login_form redirect
  61. custom login form redirect problem
  62. Login redirect to previous page
  63. Trying to re-direct users to specific page based on an ACF variable
  64. Disable redirect to homepage after successful Login, Stay on Current Page
  65. Redirect to custom login page if not logged in and redirect to home if logged in user try to login, how?
  66. wp_login_form() redirect same page
  67. How force WordPress redirect to current custom page after login
  68. Hide a Post everywhere except on redirect
  69. Conditional Login Redirect
  70. Keep the old referrer after failed login
  71. Determine user destination on wp-login.php
  72. Redirect user after login to prior page through a wp_login hook
  73. How can force redirected to login for two or more pages to view or access
  74. How to redirect user to Referrer Page using wp_signon in Custom Login Page in WordPress
  75. Redirect after login when WordPress in subdirectory
  76. HOW do you Redirect buddypress login to EDIT tab not PROFILE tab on profile page? [closed]
  77. Login redirects to home page and doesn’t log in
  78. WordPress not loggin user and redirecting after custom registration
  79. redirect_to ignoring the SSL
  80. Redirect user on login using extra field value on login form
  81. Woocommerce login to specific page or referrer
  82. wp-login.php redirect problem
  83. login redirect problem – db charset issue?
  84. How to modify the WordPress comes with redirect
  85. Conditional redirect on login using referring URL
  86. Redirect problem: How to redirect to the original URL after login
  87. Custom Login Plugin Redirects to wp-login.php After Site Migration
  88. Login Form Redirection
  89. Redirect user to login and then to page where they came from
  90. WordPress Login Page redirecting after failed login
  91. Logging back into WordPress after logging out and having issues with ‘loggedout=true’ in redirect URL
  92. login page not working [closed]
  93. Redirect after user changes password
  94. redirect users to the login page
  95. WordPress redirect to splash page once a day for the first week
  96. Accept (once only) policy page on login
  97. login_redirect for Mobile Web
  98. wp_login_form: Redirect to dynamic url according to username
  99. How to disable login and registration pages?
  100. Redirecting logged in users to custom URL
Categories redirect Tags login, redirect
Redirect WordPress multilingual website to the domain
I am trying to implement some logic on real time order sync using scheduler

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress