wp_login
フックを使用した後、wordpressでログイン直後に表示されるページにJavaScriptコードのスニペットを追加することは可能ですか?
このトップを使用して、ログイン時にユーザーに通知する通知システムを実装します。
私はこのようなことをするだろう:
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コードを挿入してトランジェントを削除します。
WordPressに保留中のアクションを保存するための一般的な方法は、短期間のトランジェントを設定することです( set_transient()
)。これはユーザーを区別する必要があるので、ユーザー情報を一時キーの一部として組み込むか、またはユーザーオプション( update_user_option()
)のように別のストレージAPIを使用することができます。代わりに。