Created
May 3, 2016 02:30
-
-
Save lewayotte/3cdc3fca196a9caaef160699cd211e0d to your computer and use it in GitHub Desktop.
Example of using a URL shortener since leenk.me v2.10.0
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
<?php | |
/* | |
* Add this file to the wp-content/mu-plugins/ directory | |
*/ | |
function my_leenkme_shortener( $permalink, $post_id) { | |
if ( $existing = get_post_meta( $post_id, '_leenkme_short_url', true ) ) { | |
$permalink = $existing; | |
} else { | |
$bitly_api = "http://api.bitly.com/v3/shorten"; | |
$bitly_args = array( | |
'login' => 'YOUR_BITLY_USERNAME', //CHANGEME | |
'apiKey' => 'YOUR_BITLY_API_KEY', //CHANGEME | |
'longURL' => $permalink, | |
'format' => 'txt', | |
'domain' => 'bit.ly' // either bit.ly or j.mp | |
); | |
$bitly_query = $bitly_api . '?' . http_build_query( $bitly_args ); | |
$result = wp_remote_request( $bitly_query ); | |
if ( is_wp_error( $result ) ) { //if we get an error just us the normal permalink URL | |
return $permalink; | |
} else if ( !empty( $result ) && !empty( $result['body'] ) ) { | |
update_post_meta( $post_id, '_leenkme_short_url', trim( $result['body'] ) ); | |
return trim( $result['body'] ); | |
} | |
} | |
return $permalink; | |
} | |
add_filter( 'leenkme_get_permalink', 'my_leenkme_shortener', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment