Skip to content

Instantly share code, notes, and snippets.

@boonebgorges
Created May 8, 2013 00:27
Show Gist options
  • Save boonebgorges/5537311 to your computer and use it in GitHub Desktop.
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
/**
* 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