Pippins Plugins
  • Email
  • Facebook
  • Feedburner
  • Github
  • Google
  • Twitter
  • Vimeo
  • Youtube
  • Rss
  • About
  • News
  • Join the Site
    • Member Benefits
    • Member Plugins
    • Email Notifications
  • Plugin Store
    • Affiliate Area
    • Checkout
  • Plugins
    • Plugin Portfolio
      • Plugin Portfolio – List View
    • Free
    • Premium
    • Member Plugins
    • Coding Standards
    • Get Plugin Support
  • Tutorials
    • Series
      • Plugin Development 101
      • Creating a User Follow System Plugin
      • Customizing Restrict Content Pro
      • Displaying Content with Easy Content Types
      • Writing Your First WordPress Plugins, Basic to Advanced
      • Working with Widgets
      • User Submitted Image Galleries
      • Plugin Thoughts
      • Integrating Stripe.com with WordPress
      • WordPress Rewrite API
    • Member Exclusive
      • Free Members
      • Subscriber Only
    • Difficulty
      • Beginner
      • Intermediate
      • Advanced
    • Action and Filter Hooks
    • Ajax
    • Custom Post Types
    • External APIs
    • Short Codes
    • Taxonomies
    • Video Tutorials
    • Widget Tutorials
    • WordPress Admin / Dashboard
    • Working with jQuery
    • WordPress Database
    • Writing Plugins
    • Tag Index
  • Reviews
  • Support Forum
  • Contact
    • Support the Site
    • Request Code Review
    • Plugin Support

WordPress Login Form Short Code

Posted on May 27, 2011 by Pippin in Beginner, Free, Plugins, Quick Tips, Short Codes, Short Codes, Tutorials, Utilities 55 Comments
Home» Tutorials » Beginner » WordPress Login Form Short Code
Tweet
Love It - 9

A lot of people want the ability to display the WordPress login form within the content of one of the site’s pages. By default, WordPress does not provide any way to do this, so we are going to write a really quick, simple short code to display the login form.

This is a quick tip. Check out more Quick Tips

The plugin only requires one simple function that will return the contents (the login form) of the wp_login_form() function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function pippin_login_form_shortcode( $atts, $content = null ) {
 
	extract( shortcode_atts( array(
      'redirect' => ''
      ), $atts ) );
 
	if (!is_user_logged_in()) {
		if($redirect) {
			$redirect_url = $redirect;
		} else {
			$redirect_url = get_permalink();
		}
		$form = wp_login_form(array('echo' => false, 'redirect' => $redirect_url ));
	} 
	return $form;
}
add_shortcode('loginform', 'pippin_login_form_shortcode');

Inside of the function we’ve added a call to the extract function in order to retrieve the attributes passed to the shrotcode, which allows us to specific the redirect url, or the location the user should be sent upon successful login.

After we get the shortcode attributes, we do a quick check to see if the current user is logged in, as there is no reason to display the form if they are already logged in. If the user is not logged in, then we return the wp_login_form() function, which will display the actual login form.

Finally, after the closing } we use the add_shortcode() function to make the shortcode available for use in the content editor.

Now we can display our form on any page or post like this:

[loginform redirect="http://my-redirect-url.com"]

Download the working plugin.
Download


Related Items
  • Plugin Development 101 – Intro to Short Codes
Tweet Follow @pippinsplugins
login, shortcode

55 comments on “WordPress Login Form Short Code”

  1. Justin says:
    June 12, 2011 at 11:44 am

    Hi, Really loving this, i’ve been looking for something like this for ages!

    Is there anyway to easily add a “Forgotten Password” link? or have the form change to enter the username/email account?

    Thanks

    Reply
    • pippin says:
      June 14, 2011 at 1:58 am

      That is not difficult to do. Take a look at this tutorial.

  2. Arvs says:
    July 28, 2011 at 10:19 am

    Hi Pippin, I’ve been searching this feature for nearly months now, and I am so lucky to have stumbled upon your site. What I was looking for was actually to a code or much better if there’s a plugin wherein I can use the $_POST in PHP to get the WordPress user_login and user_pass user entry so I won’t have to make a separate login to access my own PHP script that I embed in WordPress that requires my users to login to be able to view their scores for example. Is there such way to do this? I would appreciate if you can lead me to the right track. Thanks.

    Reply
    • pippin says:
      July 28, 2011 at 1:45 pm

      @Arvs – The only way I have found to do this so far is to add a line to the wp-login.php file that stores the $_POST['pwd'] in a $_SESSION variable. However, this is really not a good idea for two reasons: 1. it’s not a good idea to store the unencrypted password in a session variable, and 2. It’s never a good idea to modify WP core files.

  3. Arvs says:
    July 29, 2011 at 2:30 am

    Hi Pippin, thanks, at least I know now the pros and cons of modifying the wp-login.php file. Thanks, I am on the right track now. :)

    Reply
  4. johnny perez says:
    January 10, 2012 at 12:10 am

    hey thanks. You saved me a lot of time! Keep up the great work! Johnny Perez.

    Reply
  5. Richard London says:
    January 12, 2012 at 8:57 am

    Can a shortcode be created for the registration form as well, for multisite?

    Your site is really useful!, already bought a couple of your plugins!

    Reply
    • Pippin says:
      January 12, 2012 at 11:09 am

      I’ve already done it: http://pippinsplugins.com/front-end-registration-and-login-forms-plugins/ There is a tutorial listed on that page as well that will show you how to do it yourself.

  6. V27DESIGNS says:
    February 20, 2012 at 3:34 am

    Thanks for the code and the short plugin version have made it even handy… thanks again.

    Reply
  7. Vince says:
    March 1, 2012 at 7:13 pm

    Nice Plugin. I am sure it is doing what it is supposed to but I have an issue I cannot get beyond.

    I used Wishlist Member which redirects every front-end login to a static page. I was hoping this plugin would allow me to embed the login in a partially protected post (where non-members cannot see the rest of the post until they login) and set the post link as the redirect. I can set all of that and the login displays in the post, but when I login using it, the site still takes me back to the home page–which is the default in Wishlist Member.

    While I realize it is ‘their’ problem, I am wondering if you have any idea on how to get around this–my understanding is they encrypt their PHP so others online state there is no way to change their code.

    Thanks in advance.

    Reply
    • Pippin says:
      March 2, 2012 at 10:17 am

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

  8. Vince says:
    March 2, 2012 at 1:14 pm

    I set it like this (–loginform redirect=”http://my-redirect-url.com”–) and it did not work for me. Again, I think my Wishlist has hijacked all logins, but thought your re-direction might be a workaround.

    Reply
  9. Jon says:
    March 19, 2012 at 9:36 pm

    Hi – I’ve been looking for this for a while – thanks so much for posting!!!

    Two questions that came up as I added the code:
    1. If a user is already logged in, is there a way to display some text that will only show up when they’re logged in? i.e. a link to the redirect page set in the shortcode

    2. is there an easy way to display a ‘logout’ link on a page?

    Thanks!

    Reply
    • Pippin says:
      March 20, 2012 at 10:48 am

      1. You can use the function is_user_logged_in() to check if a user is logged in or not. Something like this:

      1
      
      if(is_user_logged_in()) { echo 'this is displayed to logged in users'; }

      2. WordPress has a function to show the logout URL:

      1
      
      <a href="<?php echo wp_logout_url(); ? rel="nofollow">">Log out</a>
    • Kelsey says:
      December 16, 2012 at 12:02 pm

      Hi !

      Where so I add the code below to your plugin??

      Thanks

    • Kelsey says:
      December 16, 2012 at 12:03 pm

      oops..

      Where do I add the code above to the plugin?

      Thanks

    • Pippin says:
      December 16, 2012 at 12:25 pm

      You could add it just above the return $form;

  10. Jon says:
    March 20, 2012 at 9:56 pm

    Great – thanks so much for your help! I think I’m very close to having this do everything I wanted. I really appreciate you posting the code and responding so quickly…

    One other question – if a wrong username/password is entered on the custom login page, it redirects to the standard WP login page. How can I keep the user on the same custom page and display something like ‘wrong username or password’?

    Thanks!

    Reply
    • Pippin says:
      March 21, 2012 at 9:44 am

      There is a hook we can use to redirect to a specified page when there is a failed login.

      1
      2
      3
      4
      5
      6
      7
      8
      
      function pippin_login_fail( $username ) {
           $referrer = $_SERVER['HTTP_REFERER'];  // where did the post submission come from?
           // if there's a valid referrer, and it's not the default log-in screen
           if ( !empty($referrer) && !strstr($referrer,'wp-login') && !strstr($referrer,'wp-admin') ) {
      		wp_redirect('http://yoursite.com/login?attempt=failed'); exit;
           }
      }
      add_action( 'wp_login_failed', 'pippin_login_fail' );  // hook failed login

      This will cause the user to be redirected back to your /login page with a query variable of “attempt=failed”. You can then use the query variable to display a “Failed to login” message.

  11. Rob Booker says:
    April 17, 2012 at 12:46 pm

    You are awesome. I appreciate that you took the time to build this and explain it.

    Reply
    • Pippin says:
      April 17, 2012 at 12:55 pm

      You’re welcome!

  12. Martis78 says:
    May 4, 2012 at 3:16 am

    For logout redirect Try code

    wp_logout_url($redirect_url)

    And dont forget move extract function outside of IF

    Reply
  13. JJHerman says:
    May 28, 2012 at 9:41 pm

    I’m sorry, but where are we supposed to place this code/file?

    Reply
    • Pippin says:
      May 29, 2012 at 5:55 am

      You can place it in your theme’s functions.php.

  14. Bryan says:
    July 27, 2012 at 12:21 pm

    Hi Pippin,

    Thank you for this awesome plugin. I’m having some trouble getting the form to re-direct after logging in though. For some reason, it keeps bringing the users to the dashboard instead of back to the page they’re trying to access.

    I’ve copied and pasted the plugin line-for-line into my functions file. Do you have any suggestions?

    Thanks in advance.

    Reply
    • Pippin says:
      July 27, 2012 at 3:50 pm

      Did you add the redirect parameter?

  15. Menard says:
    September 11, 2012 at 3:36 pm

    Hi sir,

    Is there a way to embed Captcha [SI Captcha Anti-Spam] for this plugin

    Thanks

    Reply
    • Pippin says:
      September 11, 2012 at 5:30 pm

      Not without modifying the plugin.

    • Menard says:
      September 11, 2012 at 5:40 pm

      how can I do it sir?

    • Pippin says:
      September 11, 2012 at 6:44 pm

      Are you comfortable with custom PHP?

    • Menard says:
      September 11, 2012 at 7:17 pm

      I have a knowledge in PHP. can you guide me on how can i embed Captcha in login form using your plugins?

      thanks.

    • Pippin says:
      September 12, 2012 at 11:45 pm

      Oh, I just realized you were asking about the specific SI Captcha. Sorry, I do not know about that.

      I have done reCaptcha integration. Interested in that one?

    • Menard says:
      September 13, 2012 at 4:52 am

      sure, as long as it ontegrates with the login form.

      thanks.

    • Pippin says:
      September 13, 2012 at 11:51 am

      Check out the docs on reCaptcha and then let me know if you have specific questions: https://developers.google.com/recaptcha/

    • Menard says:
      September 13, 2012 at 5:20 pm

      Hello,

      I tried to modify my wp-login.php code and integrate the codes from google’s reCaptcha docs

      but it doesnt appear the captcha validation..

      am I doing it on the right track?

      thanks!

    • Pippin says:
      September 14, 2012 at 10:01 am

      Do not modify wp-login.php. You should only modify the plugin file itself.

    • Menard says:
      September 14, 2012 at 10:30 am

      how? :D

    • Pippin says:
      September 14, 2012 at 4:34 pm

      I can’t give you step by step instructions (that’d be a full tutorial), but if you show me what you have tried, I can tell you if you are on the right path.

  16. Filt says:
    September 13, 2012 at 7:08 am

    Awesome, dude. But here comes the million-buck question: how do you keep the user on the same page if the login fails? I don’t want to bring forth the awful WordPress login page, which defeats the whole purpose. Would it be possible execute the whole login process without ever seeing the ‘wp-login.php’ screen, and displaying the errors (‘wrong password, empty fields, etc) right there and then? Thanks, and good luck.

    Reply
  17. Filt says:
    September 13, 2012 at 7:11 am

    Sorry, dude. My bad for not reading ALL the comments… Feel free to delete, and thanks once more.

    Reply
  18. Filt says:
    September 13, 2012 at 7:19 am

    Okay, third time’s the charm. :) I have absolutely no idea how to implement the (vague) solution you suggested to Jon, so I guess my first reply is still valid: How to manage the whole login from the shortcode, without ever going to wp-login.php? Regards.

    Reply
    • Pippin says:
      September 13, 2012 at 11:52 am

      Take the code snippet I gave to Jon and paste it into the bottom of the plugin file (of this plugin). That’s all you need to do.

  19. B£achy says:
    October 29, 2012 at 8:39 am

    I love you for this. :)

    Reply
  20. PJ says:
    November 4, 2012 at 7:03 pm

    thanks for this amazing…but what happened to logout?

    Reply
    • Pippin says:
      November 5, 2012 at 11:04 am

      I’d suggest using the admin tool bar. When a user is logged-in, there will be a logout link in the menu in the top right.

  21. jaypee says:
    November 28, 2012 at 9:24 pm

    i simply love this plugin. thanks a lot bro..

    Reply
  22. Nora McDougall-Collins says:
    December 2, 2012 at 3:04 pm

    Nice little plugin! I intend to show it to my WordPress programming students as a model. However, I have noticed one little glitch.

    The situation is that we have a certain login form for speakers to use to upload their presentation materials to the media library. When I use the plugin, it always redirects to the main dashboard instead of the media library address I added in the redirect. I have tried it with the fully qualified domain name in the shortcode and without it.

    I have noticed that other people have had a problem with using the redirect parameter with wp_login_form()

    Reply
    • Pippin says:
      December 2, 2012 at 8:03 pm

      Can you show me an example?

    • Nora McDougall-Collins says:
      December 2, 2012 at 10:47 pm

      Sure! http://westernforestry.org/Events/wp-admin/media-new.php The form is in the lower right hand corner. However, others are having similar problems with the redirect parameter; so, it may not be anything to do with the plugin.

    • Pippin says:
      December 4, 2012 at 7:43 pm

      Can you give me a sample user login and password to test?

    • Nora McDougall-Collins says:
      December 5, 2012 at 10:46 pm

      My client has OK’d it, but how do I send it to you without putting it in a public forum?

    • Pippin says:
      December 6, 2012 at 12:29 pm

      Use my contact page.

  23. timDesain says:
    December 27, 2012 at 1:23 am

    Nice custom login and combined with custom post type for the message

    http://bisnisukm.com/wp-login.php

    Reply
  24. Phil Johnson says:
    April 22, 2013 at 10:04 am

    I have installed and activated your plugin. What code do I need to insert into the page to get it to work?
    Thanks

    Reply
    • Pippin says:
      April 23, 2013 at 8:56 am

      Just [login_form]

Leave a Reply Cancel reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • Login

Lost your password?

Please enter your username or e-mail address. You will receive a new password via e-mail.

  • Facebook Become a Fan Like

  • Twitter Subscribe on Twitter Follow

  • YouTube Follow my Videos Subscribe

  • RSS Feed Subscribe with RSS Subscribe

Easy Digital Downloads

Most Loved

  • Love It Pro for WordPress
  • Write a “Love It” Plugin with Ajax to Let Users Love Their Favorite Posts / Pages
  • Simple Notices Pro Plugin for WordPress
  • User Bookmarks for WordPress
  • Front End Registration and Login Forms Plugin

Similar Plugins and Posts

  • Github Contributors Short Code
  • Front End Registration and Login Forms Plugin
  • Creating Custom Front End Registration and Login Forms for WordPress
  • Show Posts with a Short Code and Display Posts in Any Page
  • Redirect to Custom Login Page on Failed Login

Latest Premium Content

  • Plugin Development 101 – Introduction to Adding Dashboard Menus
  • Plugin Development 101 – Intro to Loading Scripts and Styles
  • User Follow System – Part 5
  • Plugin Development 101 – Intro to Short Codes

Latest Tutorials

  • Storing Session Data in WordPress without $_SESSION (19)

    The term Session in web development refers to...

  • Test Your Plugins with RTL (1)

    Right-To-Left languages are those that...

  • Submitting Your First Pull Request to a WordPress Plugin on Github (5)

    Github is an extremely popular tool for managing WordPress plugins, and one...

Enter your email to receive automated updates when new posts are published

WP Core Contributions

  • [24316]

View the ticket on Trac.

WP Codex Contributions

  • Function: shortcode exists
  • Function: has shortcode
  • Function: shortcode exists
  • Function: shortcode exists
  • Function: has shortcode

View all 41 changes in the Codex.

Latest Tweets

  • Could not fetch Twitter RSS feed.

Topics

the_content featured register_setting wp_enqueue_script attachments shortcodes contextual help add_options_page Tom McFarlin get_user_meta hook Sugar Event Calendar meta box forms do_action plugin mail chimp login short codes authors Related posts attachment image apply_filters comments recent posts post types short code bbpress taxonomies custom post type Ajax gallery images Stripe taxonomy jquery widgets users add_filter easy content types add_action widget restrict content pro easy digital downloads

Weekly Newsletter

Useful Links

  • Join the Site
  • Plugin Store
  • Affiliate Area
  • Tag Index
  • Support the Site
  • Suggest a Tutorial
  • Random Post
  • Contact

Monthly Archives

(c) 2013 Pippin's Plugins