redirect - Determine user destination on wp-login.php

I am adding functionality to require users to login to view a website by hooking into the template_redirect action and s

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!

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!

Share Improve this question asked May 23, 2020 at 1:11 Louis WLouis W 4741 gold badge6 silver badges18 bronze badges 1
  • On what occasion would $_REQUEST['redirect_to'] be empty if the user comes from the frontend? – Himad Commented May 23, 2020 at 1:46
Add a comment  | 

1 Answer 1

Reset to default 0

You can use additional parameters while redirecting users from front-end.
i.e; http://yourdomain/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' );
} );

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742407614a4438172.html

相关推荐

  • redirect - Determine user destination on wp-login.php

    I am adding functionality to require users to login to view a website by hooking into the template_redirect action and s

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信