Put a WordPress login form on any page with [nahnu_login_form], and show a welcome message to users who are already logged in.
add_shortcode( 'nahnu_login_form', function ( $atts ): string {
$atts = shortcode_atts( [ 'redirect' => '' ], $atts, 'nahnu_login_form' );
if ( is_user_logged_in() ) {
return sprintf(
'<p>You\'re logged in as %s. <a href="%s">Log out</a></p>',
esc_html( wp_get_current_user()->display_name ),
esc_url( wp_logout_url( get_permalink() ) )
);
}
return wp_login_form( [
'echo' => false,
'redirect' => $atts['redirect'] ? esc_url_raw( $atts['redirect'] ) : get_permalink(),
'remember' => true,
] );
} );
Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.
Sending users to wp-login.php takes them away from your site’s design. A login form on a normal page, such as /members/, feels part of the site. This shortcode adds one anywhere.
wp_login_form() builds WordPress’s own login form, which posts to wp-login.php, so all normal login security, plugins and two-factor checks still apply. With 'echo' => false it returns the HTML for the shortcode.
Logged-in users see their name and a log out link instead. Add redirect="/dashboard/" to send people somewhere specific after logging in.
Add [nahnu_login_form] to a page and view it logged out. The form should appear. Log in through it and you should return to the same page.