Developer

Last Updated November 19, 2021

How to use Hero.link with your WordPress site Manually 

If you interested to Not to use our WordPress Plugin and set up the API your WordPress wbsite manually here is the step by step guide   , Also 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 your API of Hero.link 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 thefunctions.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.

function shorten_url($url){

    $siteurl="db.gl"; 
    $apikey="YOUR API KEY"; // You can get it from the user account

    if($apikey && $siteurl){
        $short=@file_get_contents("db.gl/api?api=$apikey&url=".strip_tags(trim($url)));
        $short=json_decode($url,TRUE);
        if(!$short["error"]){
             return $short["short"];
        }
    }
}

Make sure to replace  $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. I 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="db.gl"; 
    $apikey="YOUR API KEY"; // You can get it from the user account

    if($apikey && $siteurl){
        $short=@file_get_contents("db.gl/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.

Conclusion

Now that you know how to use Hero.link and it's awesome features, go ahead and have fun :)