Skip to content

Instantly share code, notes, and snippets.

@tripflex
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tripflex/8dd22c6a3b7472ec3461 to your computer and use it in GitHub Desktop.
Save tripflex/8dd22c6a3b7472ec3461 to your computer and use it in GitHub Desktop.
Output twitter link from username
<?php
$twitter_username = get_custom_field('company_twitter');
if( ! empty( $twitter_username ) ) {
$clean_twitter = sanitize_text_field( $twitter_username );
// Check for http: or https: in $clean_twitter
if ( $clean_twitter && ! strstr( $clean_twitter, 'http:' ) && ! strstr( $clean_twitter, 'https:' ) ) {
// Check if twitter.com is not in string (means they used username only)
if( ! strstr( $clean_twitter, 'twitter.com' ) ){
$clean_twitter = "https://twitter.com/{$clean_twitter}";
} else {
$clean_twitter = 'http://' . $clean_twitter;
}
}
echo "<a href=\"{$clean_twitter}\"><img src=\"//yourdomain.com/twitter-icon.png\"></a>";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment