How to create a login form shortcode for your WordPress blog

October 12, 2011 at 3:27 pm

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!

Tags: