Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

By default WordPress prints http:// in front of URLS users add in their profile, which are called using the following code:

<?php the_author_meta('user_url'); ?>

Could somebody provide me the code to strip these URLS of "HTTP://" by default? (While still working as a link and not touching WP's backend... I assume this could be a function?)

share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

Assuming that the author's id is $author_id, the following code should work. You can of course use this to create your own function.

<?php $url = get_the_author_meta('user_url', $author_id); ?>
<a href="<?php echo $url; ?>"><?php echo str_replace('http://', '', $url);?></a>
share|improve this answer
    
Thanks, that was a BIG help. –  AndrettiMilas Aug 22 '11 at 19:24
add comment
<?php
function get_the_user_url_printable( $author_id = false ) {

    if ( $author_id ) {
        $user_url = get_the_author_meta( 'user_url', $author_id );
    } else {
        $user_url = get_the_author_meta( 'user_url' );
    }

    if ( !$user_url ) {
        $user_url = get_author_posts_url( $author_id );
    }

    return preg_replace( '/^(\w+:)\/*(.*)\/$/', '$2', $user_url );
}
?>

The aforementioned code block...

  1. meets the WordPress Codex Coding Standards, as confirmed by mrchrisadams' WordPress Coding Standards CodeSniffer ruleset;

  2. guesses the $author_id if inside a Loop;

  3. falls back on the url delivered by get_author_posts_url() if the author has not explicitly entered a URL;

  4. is agnostic about the protocol (stripping http://, https://, git://, or even mailto:);

  5. and is a function, as per the request.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.