How to Automatically Create Tiny URLs in WordPress

WPTurbo » Snippets » How to Automatically Create Tiny URLs in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Do you find long and cumbersome URLs cumbersome to share and remember? If so, you’re not alone. But don’t worry, there’s a simple solution: creating tiny URLs. By using a URL shortener, you can transform your lengthy URLs into compact and user-friendly ones. In this article, we’ll guide you through the process of automatically generating tiny URLs for your website, saving you time and making it easier for your audience to access and share your content.

					function wpturbo_create_tiny_url($url) {
    $api_url = 'https://api.shrtco.de/v2/shorten?url=' . urlencode($url);
    $response = wp_remote_get($api_url);

    if (is_wp_error($response)) {
        return $url;
    }

    $body = wp_remote_retrieve_body($response);
    $data = json_decode($body);

    if ($data && $data->ok) {
        return $data->result->full_short_link;
    } else {
        return $url;
    }
}

add_filter('the_permalink', 'wpturbo_create_tiny_url');
				

The code snippet above demonstrates how to create tiny URLs automatically in WordPress by utilizing a URL shortening API. This functionality can be useful when you want to display shorter and more concise URLs on your website.

The wpturbo_create_tiny_url() function takes a parameter $url, which represents the original URL that needs to be shortened. Inside the function, we construct the API endpoint by appending the original URL to the API base URL: 'https://api.shrtco.de/v2/shorten?url=' . urlencode($url).

We then use the wp_remote_get() function to make a HTTP GET request to the API endpoint and retrieve the response. This function ensures that the request is properly handled by WordPress, including handling SSL certificates and HTTP redirects.

Next, we check if the API request was successful by using the is_wp_error() function. If there was an error, we return the original URL as there was an issue with the API request.

If the API request was successful, we retrieve the response body using the wp_remote_retrieve_body() function. This body contains the JSON response from the API.

After retrieving the response body, we use the json_decode() function to decode it into a PHP object. This object contains the relevant information returned by the API, such as the full shortened URL.

We then check if the response object exists and if the ok property is true. If both conditions are met, we return the full shortened URL using $data->result->full_short_link.

If either the response object does not exist or the ok property is not true, we return the original URL since there was an issue with the API response.

Finally, we hook the wpturbo_create_tiny_url() function into the the_permalink filter. This filter allows us to modify the permalink before it is displayed on the website. By adding the function to this filter, we ensure that all permalinks on the website are automatically replaced with their corresponding shortened URLs.

With this code snippet in place, whenever a permalink is generated or displayed on your WordPress website, it will be automatically replaced with a shortened URL obtained from the specified API.

Register an account to save your snippets or go Pro to get more features.