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

How to include error message on login

The thing here is that you keep all code in your template file, which will prevent some of it from executing. Once you load your template without being logged in auth_redirect() will be run. This function stops the page loading and redirects to the login page, thus your login_head callback will never be called. Try dividing your code like this:

your-template.php

if (!is_user_logged_in()) {
    auth_redirect();
}

functions.php

add_action('login_head','ref_access');
function ref_access() {

    global $error;

    if( !empty($_GET['ref']) && 'access' == $_GET['ref'] )
        $error="Restricted area, please login to continue.";
}

While this will make ref_access execute on login_head it also means that it will run each time the login page is displayed. To account for this, you will have to find a way to let WordPress keep track of when and from where auth_redirect() is being called, and make ref_access() check for some kind of referrer. The first thing that comes to mind is using sessions to store information about each ongoing redirection and clear the session data in ref_access() or on each new page load as needed.

An alternative option is using the login_message filter to display messages on the log in page, but the redirect/referrer issues applies there as well. Here’s an example:

add_filter('login_message', 'wpse_87333_login_message');
function wpse_87333_login_message($messages) {
    $messages="Restricted area, please login to continue.";
}

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. Firing a function AFTER redirect
  23. How to modify the Register link in the login page?
  24. Changing “Lost Password Email Link” to custom password reset page
  25. How to redirect user to a specific page based on username?
  26. Redirect after password update
  27. How to redirect user after login to a specific page?
  28. Redirect Logged In User if page is wp-login.php and $_Get[‘level’] = X
  29. Redirect when accessing /login when logged in
  30. How to redirect after login getting a variable from url (for example with the language)
  31. If User is NOT Logged in and Page ID is not 6 – Redirect to Login
  32. Redirect to Current Page after Login
  33. Login redirect problem
  34. How to redirect on login to a specific page if a specific meta user is empty
  35. 503 Service Unavailable error was encountered
  36. redirect Login problems
  37. Redirect user to previous page after signup from custom form
  38. How can I force the user to log in, even if they’re already authenticated?
  39. Unable to access folders in same level as wordpress installation
  40. User is logged in after Stripe payment, then redirected, but is then logged out again
  41. Login Redirect – Multiple Scenarios
  42. WooCommerce – Redirect to a product after login
  43. Usage of the login_redirect filter
  44. Login/Register redirect user with message
  45. Redirect user to original url after login?
  46. Prevent users from going to wordpress profile after login
  47. Custom form redirect after login
  48. Custom redirect user after login based on metadata
  49. Custom Login Form – Redirect user to login page if not logged in
  50. Redirection on Custom page
  51. Why does WP HTTP API switch the method (POST/PURGE) to GET when redirecting (302)?
  52. Redirect user to specific link after login
  53. Load function with login_redirect
  54. Prevent “/login” to redirect to “/wp-login”
  55. Redirect User to login page
  56. All URLs Redirect to Main Page
  57. How to redirect user after login to the page called by his first name?
  58. redirect doesn’t work while using example code
  59. Why I am not able to direct user to my blog section without login?
  60. Redirect After Login Error
  61. Redirect certain pages if user logged in
  62. Force User Login
  63. wp_login_form redirect
  64. custom login form redirect problem
  65. Login redirect to previous page
  66. Blog post is redirecting to home page
  67. Trying to re-direct users to specific page based on an ACF variable
  68. wp_redirect is only working in Chrome Network, page is not redirecting
  69. Disable redirect to homepage after successful Login, Stay on Current Page
  70. Redirect to custom login page if not logged in and redirect to home if logged in user try to login, how?
  71. wp_login_form() redirect same page
  72. How force WordPress redirect to current custom page after login
  73. Hide a Post everywhere except on redirect
  74. Conditional Login Redirect
  75. Keep the old referrer after failed login
  76. WordPress website down with error “redirected you too many times.”
  77. Determine user destination on wp-login.php
  78. Redirect user after login to prior page through a wp_login hook
  79. How can force redirected to login for two or more pages to view or access
  80. How to redirect user to Referrer Page using wp_signon in Custom Login Page in WordPress
  81. Redirect after login when WordPress in subdirectory
  82. HOW do you Redirect buddypress login to EDIT tab not PROFILE tab on profile page? [closed]
  83. I get “too many redirects” problem when migrating localhost site
  84. Login redirects to home page and doesn’t log in
  85. WordPress not loggin user and redirecting after custom registration
  86. custom error message or redirect to custom page if any error occurs
  87. redirect_to ignoring the SSL
  88. Redirect user on login using extra field value on login form
  89. Woocommerce login to specific page or referrer
  90. wp-login.php redirect problem
  91. login redirect problem – db charset issue?
  92. How to modify the WordPress comes with redirect
  93. Conditional redirect on login using referring URL
  94. Redirect problem: How to redirect to the original URL after login
  95. Custom Login Plugin Redirects to wp-login.php After Site Migration
  96. Login Form Redirection
  97. Redirect user to login and then to page where they came from
  98. WordPress Login Page redirecting after failed login
  99. Redirect after empty register form
  100. Logging back into WordPress after logging out and having issues with ‘loggedout=true’ in redirect URL
Categories redirect Tags errors, login, redirect
Multiplicate entry on update_post_meta
Checkboxes in registration form

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