How to use your URL shortener with your WordPress site
How to use your URL shortener with your WordPress site
Now that you set up your own URL shortener using Premium URL Shortener, you may want to use it to shorten the URL of your WordPress blog post or page. In this tutorial, I will show you how to achieve that in under 5 minutes.
Things to know before
We will be using the API of your URL shortening site to achieve this, therefore you must enable that option in the admin panel. Also, all of the URLs requested to be shortened will still go through the validation process. Let's start!Setting up the function
First we will start setting up a function to send the request via the API and to display the result. That function must be added to the functions.php file of your theme. If you are using a decent WordPress theme, chances are that that file already exists. If not you can simply create one in your theme folder. Once that file has been created, you must paste the code below in it and add your API key and the site to your URL shortener (e.g. http://s.ly).function shorten_url($url){ $siteurl="THE URL TO YOUR SHORTENER"; // FOR Example http://gempixel.com/short $apikey="YOUR API KEY"; // You can get it from the user account if($apikey && $siteurl){ $short=@file_get_contents("$siteurl/api?api=$apikey&url=".strip_tags(trim($url))); $short=json_decode($url,TRUE); if(!$short["error"]){ return $short["short"]; } } }Make sure to replace $siteurl by your actual site URL (e.g. http://s.ly and NOT http://s.ly/api) and $apikey by the api key you get from the user panel. An important thing to note about this function is that it does not show the error message. It will only display the short url when it has been successfully shortened. Another thing to note is the name of the function. If you think this name has been taken by a plugin or something else (which may happen if you use a lot of plugins), then consider changing it to something unique e.g. make_this_url_short($url). Be creative :)
Add a shortcode feature
If you are an experienced WordPress user then chances are that you became addicted to shortcodes. These are built in to simplify and to speed writing of content. Registering a shortcode in WordPress is very easy to do. We will use the function above in combination with another simple function to register the shortcode and shorten the URL via the shortcode. We will again add the following codes in functions.php of the WordPress theme./* This function parses the shortcode content and send it to the main unction */ function shortcode_shorten_url($atts,$content){ return shorten_url($content); } // This code simply registers the shortcode "shorten" add_shortcode("shorten", "shortcode_shorten_url");The code above registers the shortcode [ shorten ]THE URL[ /shorten ]. Below is the full code you must add in your theme functions.php.
// Main Function function shorten_url($url){ $siteurl="THE URL TO YOUR SHORTENER"; // FOR Example http://gempixel.com/short $apikey="YOUR API KEY"; // You can get it from the user account if($apikey && $siteurl){ $short=@file_get_contents("$siteurl/api?api=$apikey&url=".strip_tags(trim($url))); $short=json_decode($short,TRUE); if(!$short["error"]){ return $short["short"]; } } } // Shortcode function function shortcode_shorten_url($atts,$content){ return shorten_url($content); } // Register Shortcode add_shortcode("shorten", "shortcode_shorten_url");To shorten a URL in your post, simply use [ shorten ]THEURL [ /shorten ]. To shorten things like the permalink of the post or page, see the example below.
Shortening Permalinks in your theme files
Now that you set up the function, you can freely use it anywhere you want. All you have to do is to call the main function and provide the URL. If for example you want to shorten the permalink of a post or a page you can use the following code in any part of your template.<?php echo shorten_url(get_permalink()) ?>