DISCLAIMER: this post is older than one year and may not be up to date with latest WordPress version.

Creating login forms on your WordPress blog used to be a complicated task. Now, thanks to the wp_login_form() function, displaying login forms on your blog has become easier. But what about a shortcode? This is what I’m going to show you on today’s recipe.

Paste the code below into your functions.php file:

function devpress_login_form_shortcode() {
	if ( is_user_logged_in() )
		return '';

	return wp_login_form( array( 'echo' => false ) );
}

function devpress_add_shortcodes() {
	add_shortcode( 'devpress-login-form', 'devpress_login_form_shortcode' );
}

add_action( 'init', 'devpress_add_shortcodes' );

Once done, you can now use the shortcode as shown in the following example. Simply paste it on the post editor, where you want the login form to be displayed.

[devpress-login-form]

Thanks to DevPress for the cool tip!

7 Comments

  1. Hey, why use a shortcode when we can use a widget for login-form that is available by default each we’ve done installing WP?

  2. Tried it but does not work.

  3. function devpress_login_form_shortcode() {
    if ( is_user_logged_in() )
    return ”;

    return wp_login_form( array( ‘echo’ => false ) );
    }

    It worked when i change array( ‘echo’ => false ) to array( ‘echo’ => true ).

  4. I tried it, also changed false to true, didnt work.

  5. Tested it out as well, with both true and false echos… no luck.

  6. the function is set to return ” when the user is logged in. As you’re likely logged in after inserting the function it’s returning nothing.
    Try logging out or using a chrome incognito window…

  7. Does it work if you set the redirect parameter of the short code?

    I would also like to know if a shortcode be created for registration page?

Leave a Comment

Your email address will not be published. Required fields are marked *