Skip to content

Instantly share code, notes, and snippets.

@MaximeCulea
Created June 23, 2016 18:23
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 MaximeCulea/854d6c3627486dfc455f09ba02df3882 to your computer and use it in GitHub Desktop.
Save MaximeCulea/854d6c3627486dfc455f09ba02df3882 to your computer and use it in GitHub Desktop.
Handle to generate a tiny url with tinyurl.com api.
<?php
/**
* Generate tiny url
*
* @author Maxime Culea
*
* @param string $post_id
*
* @return string
*/
public static function get_tiny_url( $url = '' ) {
$url = ! empty( $url ) ? $url : get_the_permalink();
$results = wp_remote_get( add_query_arg( [ 'url' => urlencode( $url ) ], 'http://tinyurl.com/api-create.php' ) );
if ( is_wp_error( $results ) ) {
return $url;
}
if ( 200 !== (int) wp_remote_retrieve_response_code( $results ) ) {
return $url;
}
$tiny_url = wp_remote_retrieve_body( $results );
return ! empty( $tiny_url ) ? $tiny_url : $url;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment