Post Pic

How to create a login form shortcode for your WordPress blog

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!

6 Responses

Dec 21 2011 02:04

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?

May 18 2012 15:16

Tried it but does not work.

Jul 30 2012 08:06

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 ).

Aug 31 2012 20:32

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

May 03 2013 11:47

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

May 22 2013 14:55

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…

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required

WP Theme of the week

Sponsored Likebox