Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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;
    }
}
share|improve this question
add comment

2 Answers

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

share|improve this answer
    
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
    
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
add comment

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 );
}
share|improve this answer
    
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 –  Rilwis Aug 22 '12 at 17:16
    
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
    
@TomJNowell, thanks. Just updated the code. I didn't notice the 'shortcode' :). –  Rilwis Aug 22 '12 at 17:18
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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