• Luke

    (@danceyrselfclean_admin)


    Hi I am using user_url on my site to display the website a user has entered on their profile. However I need the output to display as whatever they enter in the field, instead of it adding http:// every time.

    Any assistance would be appreciated.

Viewing 1 replies (of 1 total)
  • I’m assuming WordPress is applying esc_url() to that value to ensure a valid URL. A domain would not qualify as a proper URL and therefore esc_url() adds http:// to it to ensure it’s a URL.

    See https://codex.wordpress.org/Data_Validation for more on why they force http://.

    There are a couple things you can do.

    One is replace http:// in the string with nothing on the front end:

    $replacements = array(
    	'http://' => '',
    	'https://' => '',
    );
    
    echo strtr( $user_url, $replacements );

    That, of course, would honor WordPress’s addition to http:// in the database, but replace it on the front-end.

    If you want the DB stored value to not have http:// you’ll have to do something like this:

    function dont_http( $raw_url ) {
    	$replacements = array(
    		'http://' => '',
    		'https://' => '',
    	);
    
    	return strtr( $raw_url, $replacements );
    }
    
    add_filter( 'pre_user_url', 'dont_http' );

    See https://developer.wordpress.org/reference/hooks/pre_user_url/ for more.

    The other option is not use $user->user_url. You could create a new User Meta field called user_domain where the http:// won’t be added automatically, it will be whatever you store in there.

    http://wpengineer.com/2173/custom-fields-wordpress-user-profile/ has a pretty simple guide on adding custom meta for users.

Viewing 1 replies (of 1 total)
  • The topic ‘Output user_url without http://’ is closed to new replies.