Created
May 8, 2013 00:27
-
-
Save boonebgorges/5537311 to your computer and use it in GitHub Desktop.
Take the input of a "Twitter" field, decide whether it's a handle or a URL, and generate a URL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Note that it uses trailingslashit(), a WP function. Write your own logic if not using WP | |
*/ | |
function sanitize_twitter_handle( $handle ) { | |
$parts = parse_url( $handle ); | |
$url = ''; | |
if ( empty( $parts['host'] ) ) { | |
// not a url | |
if ( 0 === strpos( $handle, '@' ) ) { | |
$handle = substr( $handle, 1 ); | |
} | |
$url = 'https://twitter.com/' . $handle . '/'; | |
} else { | |
$url = trailingslashit( 'https://twitter.com' . $parts['path'] ); | |
} | |
return $url; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment