Wordpress Jetpack get number of Subscriber/followers

David Carr

Tutorials PHP & MySQL Wordpress

There's a great Wordpress plugin called Jetpack it comes bundled with some great options such as Site Statistics and allowing users to subscribe to your posts and comments to receive a notification via email.

Its super easy to setup, you will need a wordpress.com account this is free to setup too. Once its installed there's a widget that allows you to have a form for users to enter their email address to subscribe when enter an email is sent to the user for confirmation.

When the link in the email is click on you'll get a confirmation page:

As you can see its fast and straight forward to use, from the admin dashboard there's a new menu item called Jetpack under the menu there's an item called Site Stats from that page you can see how many subscribers you have and also see the individual email addresses.

For most users that's all you'll need. Some site such as this one display stats on the site such as Facebook fans, Twitter followers and email subscribers when using Jetpack there's no option to display the number of subscribers I've looked through the options and across many websites and was unable to find a way to show the number of subscribers.

I reluctantly started looking through Jetpacks code, I say reluctantly as I was hoping there was a ready made option available to use, so far I haven't found one.

Looking through the code I tried a few ways none of which worked until I came across the following method located in jetpack/modules/subscriptions.php

function fetch_subscriber_count() {
        $subs_count = get_transient( 'wpcom_subscribers_total' );

        if ( FALSE === $subs_count || 'failed' == $subs_count['status'] ) {
            Jetpack:: load_xml_rpc_client();

            $xml =& new Jetpack_IXR_Client( array(
                'user_id' => $GLOBALS['current_user']->ID
            ) );

            $xml->query( 'jetpack.fetchSubscriberCount' );

            if ( $xml->isError() ) { // if we get an error from .com, set the status to failed so that we will try again next time the data is requested
                $subs_count = array(
                    'status'  => 'failed',
                    'code'    => $xml->getErrorCode(),
                    'message' => $xml->getErrorMessage(),
                    'value'      => ( isset( $subs_count['value'] ) ) ? $subs_count['value'] : 0,
                );
            } else {
                $subs_count = array(
                    'status' => 'success',
                    'value'  => $xml->getResponse(),
                );
            }

            set_transient( 'wpcom_subscribers_total', $subs_count, 3600 ); // try to cache the result for at least 1 hour
        }

        return $subs_count;
    }

This method is what we need to get the number of subscribers, as its part of a class (Jetpack_Subscriptions_Widget) I wasen't sure how to call it from my theme so I decided to copy the method into a class in my theme I know is is not the best way but it works, if you know of a better way I'd love to hear from you.

In my theme functions.php file I have a class for creating widgets

class mytheme_Stats_Widget extends WP_Widget {

Inside the class I copied the method fetch_subscriber_count and removed anything that was not directly related to showing the number of subscribers.

function fetch_subscriber_count() {
        $subs_count = get_transient( 'wpcom_subscribers_total' );

        if ( FALSE === $subs_count || 'failed' == $subs_count['status'] ) {
            Jetpack:: load_xml_rpc_client();

            $xml =& new Jetpack_IXR_Client( array(
                'user_id' => $GLOBALS['current_user']->ID
            ) );

            $xml->query( 'jetpack.fetchSubscriberCount' );

            if ( !$xml->isError() ) { // if we get an error from .com, set the status to failed so that we will try again next time the data is requested
                $subs_count = array(
                    'status' => 'success',
                    'value'  => $xml->getResponse(),
                );
            }

            set_transient( 'wpcom_subscribers_total', $subs_count, -3600 ); // try to cache the result for at least 1 hour
        }

        return $subs_count;
    }

Next make a call to the function and save the result, then pass the value array into a number_format function. Then the number of subscribers is stored inside $subscribers_total so that can be used where I want to show the number of subscribers.

 $subs_fetch = $this->fetch_subscriber_count();
    $subscribers_total = number_format_i18n( $subs_fetch['value'] );

As I've said its not the best way of doing it, as it the Jetpack plugin is updated the method in my theme may also need to be updated.

Laravel Modules Your Logo Your Logo Your Logo

Become a sponsor

Help support the blog so that I can continue creating new content!

Sponsor

My Latest Book

Modular Laravel Book - Laravel: The Modular way

Learn how to build modular applications with Laravel Find out more

Subscribe to my newsletter

Subscribe and get my books and product announcements.

Fathom Analytics $10 discount on your first invoice using this link

© 2006 - 2024 DC Blog. All code MIT license. All rights reserved.