simple-facebook-fan-count-wordpress-shortcode

An Easier (Depreciated) Way to Get Your Facebook Fan Count with WordPress Shortcode (or PHP)

While playing around with an app I stumbled upon the Facebook’s Rest Server API which I hadn’t dealt with before. While depreciated, it’s still working at the time of writing, and worthy of noting here as a reference. It points to a period in time when life was simple before the registration of apps and playing around with tokens and the like became a problematic norm.

We’ve already provided shortcode to obtain your page likes using the graph, which is the preferred and only supported method of obtaining your count. That said, your fan count can essentially be returned in two lines of PHP code. You’ll just have to substitute Flight’s FB page ID with your own.

We can quickly turn this into a shortcode function, or a function that can be used outside WordPress.

The Result

The result is as you would expect. Using the shortcode of [fbpglikes] the function will return Unavailable (Flight on FB). To get Internoetics’ poor Facebook count, use [fbpglikes id="158719717506226"]. The result: Unavailable.

We return a number format (with commas between thousands) by default. To disable this feature use n="0" or hardcode it into your function.

Hardcode your own Facebook page ID into the shortcode function so you won’t have to define it as an attribute in your shortcode.

The Shortcode Function

Copy and paste the WordPress function into your theme's functions.php file or, if you sensibly have one installed, your custom functions plugin.

If you require shortcode to work in a sidebar widget, you'll have to enable the functionality with a filter. If you're using our custom functions plugin, you'll have that feature enabled by default.

The PHP Function

The PHP function is very easy to use. It’s expected that you’ll provide a method of caching the data for a period of time to avoid making unnecessary requests to the API.

If you don’t want to return the count in number format, use n="0".

Download the Code

Download the WordPress shortcode function here and the PHP function here . Visit our other article with preferred code here.

Shortt URL for this post:

Leave a Reply

Your email address will not be published. Required fields are marked *