Login Form Shortcode
Adds a [wpide_login_form] shortcode that shows a login form on any page, or a "you are logged in" message with a logout link.
Opens WPIDE on
Code
add_shortcode( 'wpide_login_form', function ( $atts ) {
$atts = shortcode_atts( array( 'redirect' => '' ), $atts, 'wpide_login_form' );
$current = get_permalink() ? get_permalink() : home_url( '/' );
$redirect = $atts['redirect'] ? wp_validate_redirect( $atts['redirect'], $current ) : $current;
if ( is_user_logged_in() ) {
return sprintf(
'<p class="wpide-logged-in">You are logged in as %1$s. <a href="%2$s">Log out</a></p>',
esc_html( wp_get_current_user()->display_name ),
esc_url( wp_logout_url( $current ) )
);
}
$html = '<div class="wpide-login-form">';
$html .= isset( $_GET['login'] ) && 'failed' === $_GET['login'] ? '<p class="wpide-login-error" role="alert">Wrong username or password. Please try again.</p>' : '';
$html .= wp_login_form( array( 'echo' => false, 'redirect' => $redirect ) );
$html .= '<p><a href="' . esc_url( wp_lostpassword_url( $current ) ) . '">Lost your password?</a></p>';
$html .= '</div>';
return $html;
} );
Notes
Optional attribute: [wpide_login_form redirect="/members/"].
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.