wordpressshortcodebuddypressob-start

How do I use the buddypress username in a shortcode?


I'm trying to link to Buddypress groups, and I need the username of the logged in user to do this. Here is my current shortcode, which is fine, but just outputs // in the URL with no username between.

// Add Shortcode
function my_groups_button() {
ob_start(); ?>
<a href="https://acaffiliates.coach/members/<?php echo bp_core_get_username($user_id); ?>/groups/"> My Groups</a>
<?php
    return ob_get_clean();
}
add_shortcode( 'my-groups-button', 'my_groups_button' );

Solution

  • You don't need the username, just the user id.

    function my_groups_button() {
       ob_start();
       ?>
       <a href="<?php echo bp_core_get_user_domain( bp_loggedin_user_id() ); ?>groups/">My Groups</a>
       <?php
       return ob_get_clean();
    }
    add_shortcode( 'my-groups-button', 'my_groups_button' );