1

I am working on a WordPress website and using the following code for registration and login. This code contains a default register and login link. But I want to change default register/login box with example.com/my-account link.

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

else :
    echo 'Hello <a href="your_url">' . $c_user->display_name . '</a>, ';
    echo ' <a href="' . esc_url( wp_logout_url( home_url() ) ) . '" alt="' . esc_attr( 'Logout', 'textdomain' ) . '">';
echo _e( 'Logout', 'textdomain' );
echo '</a>';

endif;
?>
4
  • If I understand you correctly, you want the default example.com/wp-login.php to redirect to your own line example.com/my-account. Use .htaccess file (if you are using apache for http) and rewrite the URL from one to the other. Apr 16 '20 at 23:04
  • @AdmiralNoisyBottom yes, I want to use custom registration. I don't want to redirect the link to the custom link. I just want to add a custom link in registration and login link in the code.
    – Imtango30
    Apr 17 '20 at 6:19
  • are you unable to use a custom html widget. The risk of adding a custom link is that anyone from the outside can still call /wp-login.php or /wp-admin and get the old login / registration method. If you want to add it to code you might be able to create a function in functions.php. I'm out of my depth with that stuff :( Apr 18 '20 at 0:03
  • @AdmiralNoisyBottom I added code in header.php. Whenever a user click on register button, they redirect to example.com/wp-login.php?action=register. I want to place example.com/my-account link instead of that link and the same on login button.
    – Imtango30
    Apr 18 '20 at 6:05
0

Got the answer

<?php
   $c_user = wp_get_current_user();
   if( !is_user_logged_in( $c_user->ID ) ) :
   echo '<a href="' . esc_url( site_url("my-account") ) . '" alt="' . esc_attr( 'register', 'textdomain' ) . '">';
 echo _e( 'register', 'textdomain' );
        echo '</a>';
   echo '<a href="' . esc_url( site_url("my-account") ) . '" alt="' . esc_attr( 'Login', 'textdomain' ) . '">';
   echo _e( 'Login', 'textdomain' );
   echo '</a>';

   else :
       echo '<a href="' . esc_url( site_url("my-account") ) . '">';
   echo _e(  $c_user->display_name );
   echo '</a>';

       echo ' <a class="omlogin" href="' . esc_url( wp_logout_url( home_url() ) ) . '" alt="' . esc_attr( 'Logout', 'textdomain' ) . '">';
   echo _e( 'Logout', 'textdomain' );
   echo '</a>';

   endif;
 ?>

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.