• Resolved riketto

    (@riketto)


    Hello everyone, I’m trying to find a solution to implement the link on a website, that uses the url which is saved in the user info. The link is different for every user. The site is only visible when logged in. Is there a way to use a function like this? Thank you so much in advance.

    • This topic was modified 2 years, 8 months ago by Jan Dembowski. Reason: Moved to Fixing WordPress, this is not an Everything else WordPress topic
Viewing 7 replies - 1 through 7 (of 7 total)
  • Where are you going to show this link? On the user profile page or somewhere else?
    If you could provide more details on your requirement then we may be able to give your possible solution to make it possible.

    Thread Starter riketto

    (@riketto)

    Thanks for the quick answer! I want to show this link on a different page. The current logged in user is supposed to click on a button and behind that button is a link, which is saved as “website” in the user meta. I tried to get this to work with

    get_userdata(get_current_user_id())->user_url;

    but wasn’t successful.

    Please share your complete code using https://gist.github.com/
    I can’t tell what you did wrong without checking the code.

    Thread Starter riketto

    (@riketto)

    That’s not the correct code.

    Here is the correct code.

    /**
     * Displays current user url.
     *
     * @return string
     */
    function custom_display_user_url_button() {
    	$html = '';
    
    	// Check if user is logged in.
    	if ( is_user_logged_in() ) {
    		// Get current user object.
    		$user = wp_get_current_user();
    		// Check if user & user url is available and user url is not empty.
    		if ( $user && $user->user_url && ! empty( $user->user_url ) ) {
    			// Prepare html.
    			$html = sprintf( '<a href="%s" class="btn">%s</a>', esc_url( $user->user_url ), esc_html( 'Click Here' ) );
    		}
    	}
    
    	// Return html.
    	return $html;
    }
    add_shortcode( 'custom_display_user_url', 'custom_display_user_url_button' );

    to display the link/button via shortcode use [custom_display_user_url] shortcode.

    if you have to use this function in php then use echo custom_display_user_url_button() in your php file.

    Thread Starter riketto

    (@riketto)

    Thank you so much!! It worked perfectly fine.

    Hi there, i am trying to do the same.

    I used ACF to add custom meta urls in user post types, and i want to display these values dynamically in Elementor buttons via dynamic tags.

    I was able to add the php snippet into my functions.php file and to add the shortcode in the elementor button url, however when i try clicking the button in my frontend with a logged in account which also has the field + corresponding value `document_url’, i get redirected to: ‘about:blank#blocked’

    When i try to add the shortcode [custom_display_document_url] without a button, nothing happens and ‘[custom_display_document_url]’ appears in the frontend as it is.

    I think I didn’t use correct code in my functions.php template. This is the code i used:

    /**
     * Displays current user url.
     *
     * @return string
     */
    function custom_display_user_url_button() {
    	$html = '';
    
    	
    	// Check if user is logged in.
    	if ( is_user_logged_in() ) {
    		// Get current user object.
    		$user = wp_get_current_user();
    		// Check if user & user url is available and user url is not empty.
    		if ( $user && $user->document_url && ! empty( $user->document_url ) ) {
    			// Prepare html.
    			$html = sprintf( '<a href="%s" class="btn">%s</a>', esc_url( $user->document_url ), esc_html( 'Complete' ) );
    		}
    	}
    
    	// Return html.
    	return $html;
    }
    add_shortcode( 'custom_display_document_url', 'custom_display_document_url_button' );
Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Dynamic button with website from user information’ is closed to new replies.