0

I am finding difficulty in returning the wp_login_form() and register link at the same time.

Here is my code which i am working to make shortcode. How to send the login_form and register link if user is not logged in?

function add_login_form() {
    if(!is_user_logged_in()){
        return wp_login_form();
    }
    else {
        return;
    }
}
1

The function wp_register will display the register link if the user isn't logged in.

Edit:

Here's the code:

if ( !is_user_logged_in() )
{
    return wp_login_form( 'echo=0' ) . wp_register( '', '', false );
}
  • I tried using wp_register() in the above code but after adding it box disappeared. :/ – Ryu Aug 22 '12 at 17:09
  • Just updated the code with example – Anh Tran Aug 22 '12 at 17:16
  • 1
    That's an invalid shortcode, shortcodes expect their html returned, you're not meant to output it directly during the function – Tom J Nowell Aug 22 '12 at 17:16
  • 1
    @TomJNowell, thanks. Just updated the code. I didn't notice the 'shortcode' :). – Anh Tran Aug 22 '12 at 17:18
1

Your shortcode is already broken. wp_login_form doesn't return a html string containing a login form. Instead it prints it out directly to the screen.

Instead do this:

function add_login_form() {
    if(!is_user_logged_in()){
        $html = wp_login_form(array('echo'=>false)).' '.wp_register('','',false);
        return $html;
    }
    else {
        return;
    }
}

I've added wp_register to add the registration link, and I've also modified the arguments to return rather than echo out the markup

  • Hi tom, is it possible to add link this way? <a href="example.com/wp-login.php?action=register">Register</…> – Ryu Aug 22 '12 at 17:59
  • Have you actually read my answer? It adds a registration link as asked. – Tom J Nowell Aug 22 '12 at 20:26
  • I have actually tested your code. When i run that shortcode it shows nothing on the page than loginbox. So that is why I asked that question. :/ – Ryu Aug 23 '12 at 6:42
  • 1
    It does work, wp_register only returns a value if registration is allowed. Go into your settings and turn it on – Tom J Nowell Aug 23 '12 at 9:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.