WordPress.org

Ready to get started?Download WordPress

Forums

Using add_action on login to set localstorage (9 posts)

  1. bbatie
    Member
    Posted 7 months ago #

    I am using the add_action with wp_login to set a localstorage item on login. Its working fine when I try to login at the wp-admin page but instead of going through with the login it goes to the wp-login.php and is just a blank page. Any ideas on this?

    Here is my actual code:

    function set_storage_yes()
    {
    echo '<script type="text/javascript">window.localStorage.setItem(\'loggedIn\', \'true\');              </script>';
    
    }
    add_action('wp_login', 'set_storage_yes', 10);
  2. Timothy Jacobs
    Member
    Posted 7 months ago #

    Have you turned on WP_Debug? If not, turn it out, I imagine you will see headers already sent errors. If so, the problem is that you can't echo out content on to the page that early. You will need to use a later hook to print your javascript. Perhaps on wp_login set a flag, and then use the admin_print_scripts hook to print out your js.

  3. bbatie
    Member
    Posted 7 months ago #

    Thanks for the help. Im not sure how to do that. Is there a better place to add this that would be easy (Im not experienced with WordPress or PHP). I just need to set the localstorage once any user successfully logs in, not just the admin.

  4. bcworkz
    Member
    Posted 7 months ago #

    You could use the 'login_head' action, but your script would run anytime the login page is loaded, not after a successful login.

    It is impractical to use login page actions if a successful login is required because the redirect happens immediately after the login is successful. The next available time to output javascript is the head of whatever admin page the redirect went to. Since you can't know which page that is, you have to add the script to all admin heads. Thus you need to introduce logic to only set the item if it has not already been sent, unless repeated settings do no harm.

    Thus the appropriate action to use is 'admin_enqueue_scripts', the action on which all other admin javascripts are added as well. Note that in this context, 'admin' refers to anyone logged in to the backend, not just admin user roles.

  5. bbatie
    Member
    Posted 7 months ago #

    How would I use the 'admin_enqueue_scripts'? Is this in the WordPress docs?

    Maybe explaining what I'm trying to accomplish will help, and maybe there is a better way to achieve it. I am calling the the wordpress login page from an iOS and Android app. Once logged in I want to set the local storage so I can check this in the app and if they are registered and have logged in I can open other access points in the app. It doesnt have to be localstorage, I just know this is one way I can easily communicate with a website and my app. I was hoping there was a POST call I could make with a form to the WordPress site, but I dont have the skill set in PHP to do that. Anyway, any other suggestions would be greatly appreciated.

    I do appreciate all the help and the speed with which you guys reply.

  6. Timothy Jacobs
    Member
    Posted 7 months ago #

    You could try this.

    function prefix_set_just_logged_in( $username, $user )
    {
        update_user_meta( $user->ID, 'user_just_logged_in', true );
    }
    add_action( 'wp_login', 'prefix_set_just_logged_in', 10, 2 );
    
    function prefix_print_local_storage_script()
    {
        if ( get_user_meta( get_current_user_id(), 'user_just_logged_in' ) === true ) {
            echo '<script type="text/javascript">window.localStorage.setItem(\'loggedIn\', \'true\');</script>';
            delete_user_meta( get_current_user_id(), 'user_just_logged_in' );
        }
    }
    add_action( 'admin_print_scripts', 'prefix_print_local_storage_script' );
  7. bbatie
    Member
    Posted 7 months ago #

    @TimothyBlynJacobs
    I tried the code but it doesnt set the localstorage. I placed it in the functions.php, do I need to put it somewhere else?

    I think I got it working without the get_user_meta check though... one more question, instead of just using true in the localstorage is it possible to use the $user variable in the javascript? or would I have to change how that's done.
    ie: echo '<script type="text/javascript">window.localStorage.setItem(\'loggedIn\', $user );</script>';

  8. bcworkz
    Member
    Posted 7 months ago #

    functions.php is fine. For the user meta check to work use the equivalence operator '==' instead of equivalence of same type '==='. Storing 'true' in usermeta gets converted to '1'. Still true, but no longer boolean type.

    The user meta check is important if you do not want your script running on every admin page load. You can store whatever you want in local storage, that's what it's for. However, $user is not in the scope of 'admin_print_scripts', but only 'wp_login'. However, you could store the $user value in user meta, then assign it back to $user again in the 'admin_print_scripts' callback. Or simply get it anew. Be aware that $user is the entire WP_User object, not a single variable. Consider storing just the data you actually need.

    As you have seen, the speed of response is variable. It is what it is. Not bad for free volunteer support I should think.

  9. bbatie
    Member
    Posted 7 months ago #

    @bcworkz
    Taking out the extra = does fix the checking problem. I will see if I can work out getting the user name into the meta data and back to use it in the localstorage.

    Yes, I do appreciate all the help. I'm pretty good in javascript, but PHP is still new to me. Hopefully with the help of the forum I can learn more.

    Thanks again for everyone's help on this matter.

Reply

You must log in to post.

About this Topic

Tags

No tags yet.