After using the wp_login hook, is it possible to add a snippet of javascript code to the page seen immediately after the login in wordpress?

I want to use this top implement a notification system that notifies the user when they login.

  • 1
    Search for admin_notices hook. If you need it after an redirect, you maybe need to do it with Cookies and a $_SESSION.
    – kaiser
    Jan 9, 2012 at 1:09

2 Answers 2


I would do something like this:

function wpse38285_wp_login( $user_login ) {
    set_transient( $user_login, '1', 0 );
add_action( 'wp_login', 'wpse38285_wp_login' );

function wpse38285_wp_footer() {
    global $current_user;

    if ( ! is_user_logged_in() )

    if ( ! get_transient( $current_user->user_login ) )

    $js = <<<JS
    <script type="text/javascript">
        jQuery('<div />')
            .html('<p>You are now logged in as <strong>$current_user->user_login</strong><br /><small>(click to close)</small></p>')
                'width': '300px',
                'position': 'absolute',
                'left': '50%',
                'marginLeft': '-160px',
                'top': '100px',
                'backgroundColor': '#cdcdcd',
                'textAlign': 'center',
                'padding': '10px'
            .on('click', function() { jQuery(this).remove(); } );
    echo $js;
    delete_transient( $current_user->user_login );
add_action( 'wp_footer', 'wpse38285_wp_footer' );

Set a never-expiring transient for the user after logging on. If a transient is set for that user, inject some JavaScript code and delete the transient.

  • 3
    Injecting JS straight into the page is not the WordPress Way. Use wp_enqueue_script() instead.
    – Pat J
    May 29, 2013 at 4:35
  • 1
    You should add a prefix to the transient name to prevent potential clashes
    – Tom J Nowell
    Jun 25, 2014 at 9:07
  • 1
    Clashes where a user name ends up the same as some other transient name. We can also make the name more meaningful. e.g. "user_" . $user_login . "_just_logged_in" (since the existence of the transient indicates that the user has only just logged in, and it should get deleted as the next page loads)
    – Harry Wood
    Sep 16, 2021 at 15:09

The typical method to store pending action in WordPress is to set short-lived transient (set_transient()). Since this would need to distinguish between users you can incorporate user info as part of transient key or use different storage API, such as user option (update_user_option()), instead.

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.