Skip to:
Content
Pages
Categories
Search
Top
Bottom

Get Logged-In User Profile Link URL?


  • ctyldsley
    Participant

    @ctyldsley

    I’ve created a ‘Members Home’ page with custom images which link to certain pages. On one of the images I want to link to the member’s profile page to manage/edit their profile.

    The URL I need to get looks like “/hbcmembers/charles/profile/” – where charles is I need to somehow get the logged in user’s username and replace it. As the ‘Members Home’ page is static, I need to input a URL which dynamically pulls in the user’s username, and then sends them to their profile page when they click the image.

    How can I get this URL to link to it? I’m not great when it comes to code. Thanks in advance!!

Viewing 8 replies - 1 through 8 (of 8 total)

  • Henry Wright
    Moderator

    @henrywright

    Hi @ctyldsley

    echo '/hbcmembers/' . bp_core_get_username( get_current_user_id() ) . '/profile/';

    Will output /hbcmembers/logged-in-username/profile/

    Hope that helps.


    ctyldsley
    Participant

    @ctyldsley

    Thanks @HenryWright! But how would I get this into a html link? I can add a html block in the theme’s visual composer. Thanks in advance!


    ckchaudhary
    Participant

    @ckchaudhary

    You can wrap it in a shortcode.
    Add following in your functions.php file:

    add_shortcode( 'CUSER-PROFILE', 'cuser_profile_shortocode_handler' );
    function cuser_profile_shortocode_handler( $atts ){
    	$atts = shortcode_atts( array( 
    		'text'		=> 'my profile',
    	), $atts );
    	
    	if( !is_user_logged_in() )
    		return '';
    	
    	$link = home_url( '/hbcmembers/' . bp_core_get_username( get_current_user_id() ) . '/profile/' );
    	return "<a href='". esc_attr( $link ) ."'>" . $atts['text'] . "</a>";
    }

    Then you can add [CUSER-PROFILE text=”Edit Profile”] in html block etc..


    ctyldsley
    Participant

    @ctyldsley

    Thanks for trying to help with this @ckchaudhary! We really need to try solve it within the next 24 hours.

    This doesn’t seem to work in a HTML box but is working when I test it on a blank page. Almost there!

    How would I edit it to make my image file (/wp-content/uploads/2015/01/ManageProfile.png) link to the Profile page for whoever is logged in at the time?


    bp-help
    Participant

    @bphelp

    @ctyldsley
    I am assuming you will be be putting the image that connects each user to their profile in a template although I am not sure which one you would want to place it in but something like this should work:

    
    <a href="<?php echo bp_loggedin_user_domain(); ?>"><img src="<?php bloginfo('url'); ?>/wp-content/uploads/2015/01/ManageProfile.png"></a>
    

    ctyldsley
    Participant

    @ctyldsley

    Thanks for all of your help everyone!! Finally there, although it’s not the most beautiful thing, it works for this case.

    //Shortcode for getting user profile URL
    add_shortcode( 'CUSER-PROFILE', 'cuser_profile_shortocode_handler' );
    function cuser_profile_shortocode_handler( $atts ){
    	$atts = shortcode_atts( array( 
    		'text'		=> 'my profile',
    	), $atts );
    	
    	if( !is_user_logged_in() )
    		return '';
    	
    	$link = home_url( '/hbcmembers/' . bp_core_get_username( get_current_user_id() ) . '/profile/' );
    	return "<a href='". esc_attr( $link ) ."'>" . "<img src ='". $atts['text'] . "'> </a>";
    }

    Using the shortcode but rather than inputting text I just input the URL of the image along with it and it links the image.


    bp-help
    Participant

    @bphelp

    @ctyldsley
    Use the below code if you only want the image linking to their profile for logged in users.

    
    if ( is_user_logged_in() ) {
    <a href="<?php echo bp_loggedin_user_domain(); ?>"><img src="<?php bloginfo('url'); ?>/wp-content/uploads/2015/01/ManageProfile.png"></a>
    }
    

    ctyldsley
    Participant

    @ctyldsley

    Thanks @bphelp! We’ve already made it so that the page content isn’t displayed if you’re not logged in, and instead it asks you to login. But that’s handy to know for the future! Really appreciate everyone’s help.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Get Logged-In User Profile Link URL?’ is closed to new replies.
Skip to toolbar