0

I need to redirect users to profile page using a single url.

Ex: Static link: www.domain.com/profile/

when clicked,

www.domain.com/profile/[USERNAME]/ <-- adds username

i need to change the [USERNAME] according to logged in user's username. can we do this in wordpress?

Ex2: if user "Paul" logged in and click on www.domain.com/profile/ then he will redirect to www.domain.com/profile/Paul/

1
  • 1
    Yes, this can be done. Are you looking for a plugin or help to implement it yourself?
    – kero
    Commented Feb 17, 2021 at 14:05

1 Answer 1

0

If you want to create a link with the user nickname as a part of it I would do something like this

<?php
// check if user is logged in
if (is_user_logged_in()) {
    // get current user object
    $current_user = wp_get_current_user();
    
    // get user nickname
    $user_nickname = $current_user->data->user_nicename;

    // set the link href
    $link_href = 'www.domain.com/profile/' . $user_nickname;

    // output the link html
    echo '<a href="' . $link_href . '" title="title text here">Link text here</a>';
}
?>

Now, because I don't know where or how you want to add the link I made some generic code that will output the link with the current users nickname only if user is logged in

EDIT

In order to output the link in a when using a wordpress text editor (wysiwyg) you will need a shortcode.

Same code as above but now in a shortcode

Add the following code inside your functions.php

add_shortcode('bt_redirect_user_link', 'bt_redirect_user_link');
function bt_redirect_user_link ($atts) {
    // check if user is logged in
    if (is_user_logged_in()) {
        // get current user object
        $current_user = wp_get_current_user();
    
        // get user nickname
        $user_nickname = $current_user->data->user_nicename;

        // set the link href
        $link_href = 'www.domain.com/profile/' . $user_nickname;

        // output the link html
        return '<a href="' . $link_href . '" title="title text here">Link text here</a>';
    }
}

To use the shortcode inside a text editor simply add [bt_redirect_user_link]

9
  • Thank you for your quick reply. Much appreciated. Im trying to add a global redirection where i redirect users to specific url (www.domain.com/profile/) and code will add {username} then redirects to that page.
    – radiax
    Commented Feb 17, 2021 at 14:21
  • That's to broad, when do you want to redirect the user? does the user has to enter a specific url or does he have to click a button, or something like that? Commented Feb 17, 2021 at 14:24
  • Yes user will click a button where he will be redirected to www.domain.com/profile/. then the code will do the rest. code will automatically add username and redirect to url www.domain.com/profile/ -> www.domain.com/profile/username
    – radiax
    Commented Feb 17, 2021 at 14:26
  • Well, my code outputs a link that a logged in user will be able to see and click, after clicking he will go to the url with his nickname. If thats all you need now i only need to know the location to place the link at Commented Feb 17, 2021 at 14:28
  • link will be on wordpress page content (placed via text editor). when user clicked on this link then your code executes. Hope this explanation is clear if not please let me know i will try to explain further. not much of a code savvy guy. apologize for that
    – radiax
    Commented Feb 17, 2021 at 14:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.