BuddyPress - How can I create a link with a variable like domain.com/members/USERNAME/path

Hello!

Is it possible to create a link within a post/page/widget that will use the logged-in member's username to complete a URL with a variable?

domain.com/members/USERNAME/path

James

  • Predrag Dubajic

    Hey @James Wolfensberger,

    Hope you're doing well today :slight_smile:

    May I suggest creating new shortcode that you can use to add the link anywhere you want.
    You can create new shortcode by using this code:

    // Add Shortcode
    function custom_user_link() {
    
    	$current_user = wp_get_current_user();
    	echo '<a href="http://domain.com/members/'. $current_user->user_login .'/path">Click Here</a>';
    
    }
    add_shortcode( 'link_shortcode', 'custom_user_link' );

    You can add this to your themes functions.php file or create new .php file in your mu-plugins folder and just add <?php before using the above code.

    You will need to adjust the link of course and add text that will show for that link, currently Click Here, and then you can show this link by adding [link_shortcode] to your post, page, text widget and so on.

    Let me know if this works for you.

    Best regards,
    Predrag

  • Predrag Dubajic

    Hi @James Wolfensberger,

    Sure thing, why didn't I think of that right away :slight_smile:

    Here is the new code to use:

    // Add Shortcode
    function custom_user_link( $atts , $content = null ) {
    
    	$current_user = wp_get_current_user();
    	echo '<a href="http://domain.com/members/'. $current_user->user_login .'/path">'.$content.'</a>';
    
    }
    add_shortcode( 'link_shortcode', 'custom_user_link' );

    Best regards,
    Predrag

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.