How to Get Facebook Fan Count Using get_transient and wp_remote_get in WordPress

Home » Snippets » How to Get Facebook Fan Count Using get_transient and wp_remote_get 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

Are you a WordPress user looking to display your Facebook fan count on your website? Adding a Facebook fan count to your site can be a great way to showcase your social media following and engage with your audience. In this article, we will show you how to use the get_transient function and wp_remote_get to retrieve and display your Facebook fan count on your WordPress site. With this method, you can easily keep track of your fan count without making unnecessary API calls, ensuring your site stays fast and efficient. Let’s dive in and learn how to add this feature to your website.

					function wpturbo_get_facebook_fan_count() {
    $transient_key = 'wpturbo_facebook_fan_count';

    $fan_count = get_transient($transient_key);

    if (false === $fan_count) {
        $url = 'https://graph.facebook.com/{your-facebook-page-id}?fields=fan_count&access_token={your-access-token}';
        $response = wp_remote_get($url);

        if (!is_wp_error($response)) {
            $body = wp_remote_retrieve_body($response);
            $data = json_decode($body, true);

            if (isset($data['fan_count'])) {
                $fan_count = intval($data['fan_count']);
                set_transient($transient_key, $fan_count, 60 * 60 * 24); // Cache the result for 24 hours
            }
        }
    }

    return $fan_count;
}
				

The code provided above illustrates how to retrieve the Facebook fan count for a specific Facebook page using the get_transient and wp_remote_get functions in WordPress.

First, we define a function called wpturbo_get_facebook_fan_count(). This function will be responsible for fetching and caching the Facebook fan count.

Inside the function, we specify a transient key variable, $transient_key, which will be used to store and fetch the fan count value from the WordPress transient cache.

Next, we retrieve the value of the fan count from the transient cache using the get_transient() function and store it in the $fan_count variable.

If the get_transient() function returns false, it means that the fan count value is not present in the cache or has expired. In this case, we proceed to fetch the fan count from the Facebook Graph API.

We construct the URL for the API request by replacing {your-facebook-page-id} with the ID of your Facebook page and {your-access-token} with your valid access token. This URL will retrieve the fan count field from the Facebook Graph API.

We use the wp_remote_get() function to send a GET request to the specified URL and retrieve the response. This function returns either a WP_Error object if there was an error or an array with the response details.

To handle the response, we first check if there were no errors using the is_wp_error() function. If there are no errors, we extract the body of the response using the wp_remote_retrieve_body() function.

The body of the response contains JSON-formatted data. We decode this data using the json_decode() function with the true parameter to convert it into an associative array.

Next, we check if the fan count field, $data['fan_count'], is present in the decoded data. If it is, we convert the value to an integer using intval() and store it in the $fan_count variable.

Finally, we set the transient cache using the set_transient() function, with the $transient_key, $fan_count, and a caching period of 24 hours (60 seconds 60 minutes 24 hours).

In the end, the function returns the fan count value, either fetched from the cache or retrieved from the Facebook Graph API.

By utilizing the get_transient() and wp_remote_get() functions, we can efficiently cache and retrieve the Facebook fan count, reducing the number of API requests and improving the performance of our WordPress website.

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