在wp_LOGIN钩子之后运行Java脚本代码?

时间:2012-01-08 作者:shammer

使用后wp_login hook,是否可以在登录wordpress后立即看到的页面中添加一段javascript代码?

我想使用这个top实现一个通知系统,在用户登录时通知用户。

2 个回复
SO网友:Daniel

我会这样做:

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;
    get_currentuserinfo();

    if ( ! is_user_logged_in() )
        return;

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

    $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>\')
            .css({
                \'width\': \'300px\',
                \'position\': \'absolute\',
                \'left\': \'50%\',
                \'marginLeft\': \'-160px\',
                \'top\': \'100px\',
                \'backgroundColor\': \'#cdcdcd\',
                \'textAlign\': \'center\',
                \'padding\': \'10px\'
            })
            .appendTo(\'body\')
            .on(\'click\', function() { jQuery(this).remove(); } );
    </script>
JS;
    echo $js;
    delete_transient( $current_user->user_login );
}
add_action( \'wp_footer\', \'wpse38285_wp_footer\' );
为用户登录后设置永不过期的瞬态。如果为该用户设置了瞬态,请插入一些JavaScript代码并删除该瞬态。

SO网友:Rarst

在WordPress中存储挂起操作的典型方法是设置短期瞬态(set_transient()). 由于这需要区分用户,您可以将用户信息合并为临时密钥的一部分,或者使用不同的存储API,例如用户选项(update_user_option()), 相反

结束

相关推荐

Login redirect problem

嘿,我有点奇怪的问题。。我得到了一个表单,可以根据类别和用户级别登录某些单曲。。它工作得很好,但windows(网站所在的服务器)返回的“当前页面”值错误意思这是:\'redirect\' => site_url( $_SERVER[\'REQUEST_URI\'] ), 返回:www.somewebsite。com/sub/sub/?p=100而不是:www.somewebsite。com/sub/?p=100This is the actuall form: <