How to add a WordPress login form to any page or post
Sometimes you want to be able to add a login form anywhere on your WordPress website. You can add the following code to your child theme’s functions.php file.
add_shortcode( 'djs-login-form', 'djs_login_form_shortcode' );
/* Use this shortcode to display a login form anywhere - [djs-login-form redirect="http://yoursite.com/somewhere"] */
function djs_login_form_shortcode( $atts, $content = null ) {
$defaults = array("redirect"=>site_url( $_SERVER['REQUEST_URI'] )
);
extract(shortcode_atts($defaults, $atts));
if (!is_user_logged_in()) {
$content = wp_login_form( array( 'echo' => false, 'redirect' => $redirect ) );
}
return $content;
}
Remember that the above shortcode requires a redirect parameter so that the user lands on the correct page after logging in.