Skip to content
Advertisement

User ID remove @ and transform “.” into “-“

I have this function in order to set a link for my user to go to their custom profile page :

add_shortcode( 'current_user_link', 'wppbc_current_user_link' );
function wppbc_current_user_link( $atts, $content ) {   
    if ( is_user_logged_in() ) 
    {      
    $current_user = wp_get_current_user();
    $id = $current_user->user_login;
       
return "<a class='lienprofils' href='https://mywebsite/author/{$id}'><i class='fas fa-user' aria-hidden='true'></i>  MON PROFIL</a>";   }
   return ;
}

The issue is that if the user ID is his email like john@example.com the link https://mywebsite/author/john@example.com will have an 403 error. But if the is the link is https://mywebsite/author/johnexample-com it works.

So is there a way to clean the user ID into my function in order to remove @ and transform “.” into “-” ?

Best regards, Clément

Advertisement

Answer

Try this

$id = $current_user->user_login;

//Remove @ 
$id = str_replace('@', '', $id);

// Replace . with - 
$id = str_replace('.', '-', $id); 
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement