Forums

[resolved] How to integrate a login form? (6 posts)

  1. tmallen
    Member
    Posted 4 years ago #

    I'd like the form fields on /wp-login.php included in my theme's header.php. I could just copy the source selectively, but I imagine there's a cleaner way to include the form.

    Bonus: What's the best way to check if the user's logged in (using a PHP if statement, I guess) and replace this form with a sort of "Hello, {name} | [Logout] | My Account" line?

  2. Otto
    Tech Ninja
    Posted 4 years ago #

    First question: Just make the same form fields as the login form has. Here's the basics of it:

    <form action="wp-login.php" method="post">
    Username: <input type="text" name="log" id="user_login" class="input" />
    Password:<input type="password" name="pwd" id="user_pass" class="input" />
    <input type="hidden" name="testcookie" value="1" />
    <input name="rememberme" type="checkbox" id="rememberme" value="forever" /> Remember Me
    <input type="submit" name="wp-submit" id="wp-submit" value="Log In" />
    </form>

    If you like, you can add a field to the form to tell the login where to redirect the user afterwards, like back to the homepage:
    <input type="hidden" name="redirect_to" value="<?php echo get_option('siteurl'); ?>" />

    Bonus Question:

    The $user_ID global will be set when the user is logged in and won't be set when they're not.

    <?php global $user_ID; if ( $user_ID ) : ?>
    Hello <a href="<?php echo get_option('siteurl'); ?>/wp-admin/profile.php"><?php echo $user_identity; ?></a> | <a href="<?php echo get_option('siteurl'); ?>/wp-login.php?action=logout" title="Log out">Log out</a>
    <?php endif; ?>
  3. tmallen
    Member
    Posted 4 years ago #

    OK, thank you very much. The only part that isn't working is
    <?php echo $user_identity; ?>
    It's returning a blank value when I'm logged in.

  4. tmallen
    Member
    Posted 4 years ago #

    Sorry to nag, but one more quick question. Is there a GET argument or something similar I can provide to redirect a logged-out user to the homepage instead of /wp-login.php?

  5. Otto
    Tech Ninja
    Posted 4 years ago #

    Make sure you're using the global $user_identity instead of a local one. Forgot about that bit.
    global $user_identity;

    Redirecting the logout: Add redirect_to=http://whatever to the request to redirect them somewhere else. Like so:

    <a href="<?php echo get_option('siteurl'); ?>/wp-login.php?action=logout&amp;redirect_to=<?php echo get_option('siteurl'); ?>" title="Log out">Log out</a>

  6. tmallen
    Member
    Posted 4 years ago #

    Damn you PHP and your lack of clear namespaces :^)
    I always hate the hassle of declaring globals like this.

    Thanks for the help!

Topic Closed

This topic has been closed to new replies.

About this Topic