WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Front-End Login using PHP (12 posts)

  1. NickGuitarify
    Member
    Posted 10 months ago #

    Hello all,

    I'm trying my best to add a login box to the front-end of my site. Using plugins hasn't worked well so far aesthetically.

    My preferred method so far has been to install ExecPHP and paste code into the widget in the footer, which works ok.

    Further to what I've done so far, I'd like to:

    • Add "Register | Forgot your password?" links for non-logged in users
    • Add a link to account management for logged in users
    • Remove the "Remember Me" box entirely, as it doesn't sit well with the theme

    Would anyone be kind enough to help me out? My current code is below.

    <?php
    if ( ! is_user_logged_in() ) { // Display WordPress login form:
        $args = array(
            'redirect' => admin_url(),
            'form_id' => 'loginform-custom',
            'label_username' => __( 'Username' ),
            'label_password' => __( 'Password' ),
            'label_remember' => __( 'Remember Me' ),
            'label_log_in' => __( 'Log In' ),
            'remember' => true
            );
        wp_login_form( $args );
    } else { // If logged in:
        wp_loginout( home_url() ); // Display "Log Out" link.
        echo " | ";
        wp_register('', ''); // Display "Site Admin" link.
    }
    ?>

    Thanks,

    Nick :)

  2. graphical_force
    Member
    Posted 10 months ago #

    What do you mean by account management? Are you referring to the dashboard?

    The other things are covered with the following code:

    <?php
    if ( ! is_user_logged_in() ) { // Display WordPress login form:
        $args = array(
            'redirect' => admin_url(),
            'form_id' => 'loginform-custom',
            'label_username' => __( 'Username' ),
            'label_password' => __( 'Password' ),
            'label_log_in' => __( 'Log In' ),
            'remember' => true
            );
        wp_login_form( $args );
        echo wp_lostpassword_url( $redirect );
        echo wp_registration_url();
    } else { // If logged in:
        wp_loginout( home_url() ); // Display "Log Out" link.
        echo " | ";
        wp_register('', ''); // Display "Site Admin" link.
    }
    ?>
  3. NickGuitarify
    Member
    Posted 10 months ago #

    Thanks for responding! The Dashboard, that's correct.

    I've entered the new code into the widget, which results in this (Chrome):

    http://i.imgur.com/CzT4e5L.jpg

  4. graphical_force
    Member
    Posted 10 months ago #

    Just to be clear, you want the admin link to show if they are logged in correct? If not they would just be redirected to another login screen which would be pointless unless I'm missing something.

    Try this for displaying lost password and registration links:

    <?php
    if ( ! is_user_logged_in() ) { // Display WordPress login form:
        $args = array(
            'redirect' => admin_url(),
            'form_id' => 'loginform-custom',
            'label_username' => __( 'Username' ),
            'label_password' => __( 'Password' ),
            'label_log_in' => __( 'Log In' ),
            'remember' => true
            );
        wp_login_form( $args );
        wp_lostpassword_url( $redirect );
        wp_registration_url();
    } else { // If logged in:
        wp_loginout( home_url() ); // Display "Log Out" link.
        echo " | ";
        wp_register('', ''); // Display "Site Admin" link.
    }
    ?>
  5. NickGuitarify
    Member
    Posted 10 months ago #

    That's correct, yes.

    I've copied in the latest code, and now nothing displays for the

    wp_login_form( $args );
        wp_lostpassword_url( $redirect );
        wp_registration_url();

    part (whereas before we had plain text as the picture in my last post illustrates).

  6. graphical_force
    Member
    Posted 10 months ago #

    Sorry, try this. Of course you may need to style this with css:

    <?php
    if ( ! is_user_logged_in() ) { // Display WordPress login form:
        $args = array(
            'redirect' => admin_url(),
            'form_id' => 'loginform-custom',
            'label_username' => __( 'Username' ),
            'label_password' => __( 'Password' ),
            'label_log_in' => __( 'Log In' ),
            'remember' => true
            );
        wp_login_form( $args );
        echo '<a href="' . wp_lostpassword_url( $redirect ) . '">Lost Password?</a>';
        echo '<a href="' . wp_registration_url() . '">Register</a>';
    } else { // If logged in:
        wp_loginout( home_url() ); // Display "Log Out" link.
        echo " | ";
        wp_register('', ''); // Display "Site Admin" link.
    }
    ?>
  7. NickGuitarify
    Member
    Posted 10 months ago #

    Thank you so much graphical_force, that has cracked it! Now I'll spend some time figuring out how to make it appear like this:

    Register | Lost Password?

    I'm trying

    echo '<a href="' . wp_registration_url() . '">Register</a>';
        echo " | ";
        echo '<a href="' . wp_lostpassword_url( $redirect ) . '">Lost Password?</a>';
  8. graphical_force
    Member
    Posted 10 months ago #

    There are a few ways to do that. The way you are doing it there should work as well.

  9. NickGuitarify
    Member
    Posted 10 months ago #

    Would you be kind enough to share the other methods with me? Mine didn't end up displaying the | at all unfortunately.

  10. graphical_force
    Member
    Posted 10 months ago #

    Try this:

    <?php
    if ( ! is_user_logged_in() ) { // Display WordPress login form:
        $args = array(
            'redirect' => admin_url(),
            'form_id' => 'loginform-custom',
            'label_username' => __( 'Username' ),
            'label_password' => __( 'Password' ),
            'label_log_in' => __( 'Log In' ),
            'remember' => true
            );
        wp_login_form( $args );
        echo '<a href="' . wp_lostpassword_url( $redirect ) . '">Lost Password?</a> | <a href="' . wp_registration_url() . '">Register</a>';
    } else { // If logged in:
        wp_loginout( home_url() ); // Display "Log Out" link.
        echo " | ";
        wp_register('', ''); // Display "Site Admin" link.
    }
    ?>
  11. NickGuitarify
    Member
    Posted 10 months ago #

    Perfect, thank you so much graphical_force. Your help is greatly appreciated! :)

  12. graphical_force
    Member
    Posted 10 months ago #

    No problem. :)

Reply

You must log in to post.

About this Topic