Add Snippet To Project
Are you looking for a way to easily retrieve and display the subscriber count from your Feedburner feed? Feedburner is a popular service that allows you to manage and track your RSS subscriptions. In this article, we’ll show you how to use the WordPress functions get_transient()
and wp_remote_get()
to fetch the subscriber count from Feedburner and store it as a transient. This will not only speed up your website’s performance but also ensure that you always have the latest subscriber count without making unnecessary API calls. Stay tuned to learn how to implement this feature on your WordPress site.
function wpturbo_get_feedburner_count() {
// Set the transient name and duration
$transient_name = 'wpturbo_feedburner_count';
$transient_duration = 3600; // 1 hour
// Get the transient value if it exists
$count = get_transient($transient_name);
// If the transient value is false, fetch the count from Feedburner
if ($count === false) {
// API URL to retrieve the Feedburner count
$url = 'https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=YOUR_FEEDBURNER_URI';
// Send GET request to the Feedburner API
$response = wp_remote_get($url);
// If successful response, extract the count from the response body
if (is_array($response) && !is_wp_error($response)) {
$body = wp_remote_retrieve_body($response);
$data = json_decode($body);
$count = $data->feed->entry[0]->circulation;
} else {
$count = 'N/A';
}
// Set the transient value with the count and duration
set_transient($transient_name, $count, $transient_duration);
}
return $count;
}
The code snippet provided demonstrates how to retrieve the feed count from Feedburner using the get_transient()
and wp_remote_get()
functions in WordPress.
First, we define two variables: $transient_name
and $transient_duration
. These variables are used to store the name and duration of the transient, respectively.
The get_transient()
function is called to check if a transient value with the specified name ($transient_name
) already exists. If it does, the value is stored in the $count
variable. If not, the code inside the if
block is executed.
Inside the if
block, we define the URL to retrieve the feed count from the Feedburner API. Note that you need to replace YOUR_FEEDBURNER_URI
with your actual Feedburner URI.
The wp_remote_get()
function is then used to send a GET request to the Feedburner API and retrieve the count. The response is stored in the $response
variable.
Next, we check if the response is a successful array and not a WP_Error object using the is_array()
and is_wp_error()
functions. If it is, we extract the count from the response body using the wp_remote_retrieve_body()
function and assign it to the $count
variable.
If the response is not successful, we assign the value ‘N/A’ to the $count
variable.
Finally, the set_transient()
function is called to set the transient value with the count and duration. The $transient_name
, $count
, and $transient_duration
variables are passed as arguments to this function.
The last step is to return the value of $count
.
By using transients, we can store the feed count for a specified duration (1 hour in this case). This ensures that the count is retrieved from the Feedburner API only once within the specified duration, reducing unnecessary API calls and improving performance.