0

I am adding functionality to require users to login to view a website by hooking into the template_redirect action and sending non-authenticated users to wp-login. This is all working fine.

I would like to add a message to the wp-login screen for these visitors, but I do not want the message to be displayed to backend users who are also logging in through the same page. I'm using the login_message filter to add the text.

Is there a way, when on the wp-login page to determine if the person is trying to login into the frontend or wp-admin? I'm seeing a $_REQUEST['redirect_to'] variable, but it can sometimes be empty, especially after repeated login attempts, and is just a string so would be a messy conditional.

Thanks!

  • On what occasion would $_REQUEST['redirect_to'] be empty if the user comes from the frontend? – Himad May 23 at 1:46
0

You can use additional parameters while redirecting users from front-end.
i.e; http://yourdomain.com/wp-login.php?frontend=true

And get this parameter to check and determine whether the users have been redirected from the front-end or not.

if( isset($_GET['frontend']) && $GLOBALS['pagenow'] === 'wp-login.php' ) {
    echo "<h2 id='fr-msg'>Message To display</h2>";
}

Use this code inside your function.php or in plugin. You can use css and javascript adjustments to display the message in correct position.

jQuery(document).ready(function(){
    jQuery("#login").prepend(jQuery("#fr-msg"));
// Use as per your requirement.
}

This will have a dependency of jQuery hence it is necessary to include this script with a jQuery dependency. Include this in your function.php or plugin.

add_action( 'login_enqueue_scripts', function() {
wp_register_script('custom_script',  [SCRIPT_URL]/customScript.js, array('jquery'),'4.2', true);
wp_enqueue_script( 'custom_script' );
} );
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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