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.
The plugin only requires one simple function that will return the contents (the login form) of the wp_login_form() function.
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
Great snippet, Pippin
I noticed a small debug error:
Notice: Undefined variable: form
It can be easily fixed by adding
$form = ”;
Just before
if (!is_user_logged_in()) {
Hi Pippin,
I came across your plugin while scouring for a way to provide a solution to uniquely redirect the user to specific content depending on where they land.
This plugin with its direct url approach was hopeful but I’ve encountered a problem.
The idea was that a user would simply be redirected to the same page they landed on after they login. Your plugin has them going to /wp-login.php and it’s just a white screen, it doesn’t do anything after that.
my shortcode: [loginform redirect=”http://inkochostudios.com/hatchabrand/brand-personality-exploration/”]
I am using this member plugin to hide my content behind a login http://themehybrid.com/plugins/members
Do you have any other plugins that are blocking access to wp-login?
Not sure if my other comment went through or not. Came to a white screen after submitting a reply.
I do not have any plugins that block wp-login.
I have my suspicions that its theme related, but I dont have the skill set to determine that.
Hi Pippin,
Any thoughts on my problem at all?
We will be moving to another theme but I was hoping we might be able to get this working before then.
As a quick test, switch to one of the default themes and see if the problem goes away.
Hi Pippin,
Just gave that a shot and encountered the same problem. Redirects to wp-login.php and it’s just a blank browser window.
Hi Pippen, Just thought I’d touch base and see if you’ve come up with any ideas why. Would you be willing to have a look if I gave you access?
I appreciate you’re smoking busy, let me know if it’s possible.
Thanks!
D.
Hi Pippin,
Just one more update. I had only the your plugin and the members plugin running on a default wordpress theme and still had the same problem.
Sorry for the delay. As Kyle said below, could you try disabling the members plugin to see if that makes a difference?
Why not disable the Members plugin and see if the problem gives away. Then you will know which plugin is causing the problem. If it goes away you know it is probably permissions caused by the Members plugin, then it is something else.
Kyle
Not sure why, but looks like the plugin isn’t working on WP 3.9. Tried on both localhost (only this plugin activated) and live site (few other plugins activated), but the form didn’t show up. Any thought?
First, love your plugin. It is what I am looking for. My question is (and I am a novice, sorry) You mention that you can set the redirect for a successful login. Where do you add that information?
Thanks
Thank you! This worked perfectly!
Hello! Love this plugin!
Just curious… Is it possible to modify this to work with the Woocommerce login? Keeping all features the same?
Thanks!
Sharon
Hi Sharon!
Are you wanting to replace the standard WooCommerce login form with this or just allow customers to log into their WooCommerce accounts on a stand alone page?
Hi Pippin!
Thanks for getting back to me so quickly!
I’d love to have the Woocommerce login form show up on a specific page using shortcode, with a redirect to a specific URL.
That would be dreamy. 🙂
Thanks again!
Sharon
Thank you for the clarification. I’m sorry but that is not supported.
Totally understand… Just planting a seed for later, maybe… 😉
Thanks, Pippin!
Thanks for the login form short code. Sharing is caring and this might help others WooCommerce shortcodes plugin guide https://www.cloudways.com/blog/woocommerce-shortcodes-comprehensive-guide/
Would be nice to dress it with some CSS
Help -I really need this plugin to work.
I installed the plugin used the shortcode on a page but its not displaying?
Do I need to install that bunch of code in functions.php?
I would recommend using this plugin instead: https://wordpress.org/extend/plugins/restrict-content
It’s a more complete version of the same thing.
Hey, It’s redirect me to my admin dashboard. Can you please help..:(
You probably didn’t change the placeholder url in shortcode to your url >> [loginform redirect=”http://my-redirect-url.com”]
Hi,
Once the user is already logged in, it is possible to show a message like: “You are already logged in, click to see your dashboard”, or something like that?
I tried to do it, but dint works.
You can achieve that by using the is_user_logged_In() function to determine if the user is logged in.
Check out the updated list of most helpful woocommerce shortcodes: https://www.reviewsed.com/woocommerce-shortcodes-guide/
Hello, this shortcode is really helpful and I implement on on wp blog.
Hi,
Thank you very much for this plugin. However, I have a bit issue. How I want to change the GET method to POST method if I used wp-login to logout? I mean to redirect from logout page to home page.
Thank you