1

I am adding register and login to WordPress website. I am using the following code:

<?php
        $c_user = wp_get_current_user();
        if( !is_user_logged_in( $c_user->ID ) ) :
        echo '<a href="' . esc_url( wp_login_url() ) . '" alt="' . esc_attr_e( 'Login', 'textdomain' ) . '">';
            echo _e( 'Login', 'textdomain' );
        echo '</a>';
        wp_register('', '');
        else :
            echo 'Hello ' . $c_user->display_name;
        endif;
        ?>

After using the code, only the login text is showing. But I want to add "regsite & login". If the user is login, it should be replaced with display "name & logout" text

0

integrating some of the suggested functions into your code:

<?php
        $c_user = wp_get_current_user();
        if( !is_user_logged_in( $c_user->ID ) ) :
        echo '<a href="' . esc_url( wp_login_url() ) . '" alt="' . esc_attr( 'Login', 'textdomain' ) . '">';
        echo _e( 'Login', 'textdomain' );
        echo '</a>';
        wp_register(' / ', '');
        else :
            echo 'Hello ' . $c_user->display_name . ', ';
            echo ' <a href="' . esc_url( wp_logout_url( home_url() ) ) . '" alt="' . esc_attr( 'Logout', 'textdomain' ) . '">';
        echo _e( 'Logout', 'textdomain' );
        echo '</a>';

        endif;
?>

some remarks:

  • for the 'Register' link to show, you need to have ticked in the dashboard: 'Settings' - 'General' - 'Membership [ ] Anyone can register

  • the 'Logout' will redirect to the home page; if you don't want that, remove home_url() from the code

| improve this answer | |

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.