0

I require a URL that links the current logged-in user to their author archive page. I came across some code from a similar question here (see below). It works for me but unfortunately, it links to the author page by using a shortcode.

I don't know how to edit this to make it work with just a URL, instead of the shortcode. (I am a total beginner but I try!). My end goal is to create a menu item using this URL.

function my_users_shortcode( $atts, $content ) {
    if ( is_user_logged_in() ) {
        echo '<a href="' . esc_url( get_author_posts_url( wp_get_current_user()->ID ) ) .
             '">My personal page</a>';
    } else {
        echo '<a href="' . esc_url( wp_login_url() ) . '" title="Login">Login</a>';
    }
}
add_shortcode( 'my_shortcode', 'my_users_shortcode' );

Any help with this would be really appreciated. If possible could any answer be provided in layman's terms that even I can understand!

Thank you

Edit:

I also found this code:

<?php 
global $current_user;
get_currentuserinfo();
echo get_author_posts_url($current_user->ID); 
?>

This could do the trick (?) but I'm not sure how to use this as an HTML link a URL.

1 Answer 1

0

UPDATE

As per the comment, here is the updated solution.

Note: Before copy pasting, don't forget to change somePrefix with any other prefix to avoid conflict with any other shortcodes.

Step 1: Make sure you have this filter in functions.php (If not, copy paste it there)

add_filter('wp_nav_menu_items', 'do_shortcode');

Step 2: Use this snippet to return Author URL. Paste this in functions.php

add_shortcode( 'somePrefix_author_url', function() { 
    
    $author_link = get_author_posts_url( get_current_user_id() ); // original link
    $without_protocol = trim( str_replace( array( 'http://', 'https://' ), '', $author_link ), '/' );
    return  $without_protocol; // returns author link without protocol
    
});

Step 3: Go to Navigation Menu and add Custom Menu Link and use [somePrefix_author_url] in the URL field and 'My personal page' or something in the Name field.

Step 4: Test the link from the front-end if it works.

Reason for using link without protocol:

By default http/ without colon is appended to the shortcode for some reason after this update. This could be a bug or maybe not as this behavior can be intentional. Anyways, the above code is safe to use as WP will automatically append the protocol to the custom link in this case.


Yes, this will do the trick. For HTML link, instead of just the function that outputs the URL only, use this from your snippet:

    echo '<a href="' . esc_url(get_author_posts_url( wp_get_current_user()->ID )) .'">
      My personal page
    </a>';

It should output 'My personal page' text hyperlinked to the current user's author page.

Additional Tip: You can get the user object with this:

    $currentUser = wp_get_current_user();
    $currentUserID = $currentUser->ID;
    $userAuthorLink = get_author_posts_url($currentUserID);
    $userFirstName = $currentUser->first_name; 

// and so on 

Later in your code instead of repeating the call for same object with wp_get_current_user() and global $current_user get_currentuserinfo();, you can call the user object for once and use its properties as shared in the tip.

See WP_User Class for available object properties (ID, first_name, ...) and methods (has_role() ..)

4
  • Hi, thanks very much for the reply! Apologies, I just noticed that I made an error in my question. I am trying to work out how to get a URL from the bottom code (not an HTML link), that I can use as a custom link in the menu. If possible, could you provide the exact code that I would have to input into functions.php? E.g. Do I need to insert anything before the 'global $current_user;'? Like 'function...'? Thanks, sorry if these seem like silly questions Jan 3, 2022 at 10:49
  • @DanielMoore I have updated the answer. Please check and mark it answer once it works. :-)
    – Aqsa J.
    Jan 3, 2022 at 13:43
  • Wheyyyyy it works!! You're a legend! Thanks very much :) Jan 3, 2022 at 14:17
  • You are welcome :)
    – Aqsa J.
    Jan 3, 2022 at 14:18

Your Answer

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

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