Shopping Cart (0)

Making a more useful & informative admin bar

1 Posted February 14, 2012 by Rockstar Frog Categories: Code Snippets

Love it or hate it, the WordPress Admin Bar is here to stay. With a bit of creative coding, you can change it from being a nuisance to a helpful tool. Today, we’re going to cover how to make your site’s admin bar more socially informative by adding some statistics on your social platforms.

Here’s what we’ll be coding:

In short, we’re going to request 3 bits of data to give us variables that hold our Twitter follower count, our Facebook Fans and our RSS Subscribers. With those 3 variables stored, we’ll then create faux menu items that display each count.

Firstly, we setup our function and hook it into the admin_bar_menu WordPress hook.

function social_toolbar($admin_bar){
    // our code's going to go here
}
add_action('admin_bar_menu', 'social_toolbar', 100);

Now that we’re hooked into the admin bar, let’s start by saving the usernames / ID’s of the 3 accounts we’re going to use: (this goes within our social_toolbar() function)

    $twitterUsername = "twitter";
    $facebookPageID = "22934684677";
    $feedburnerID = "TechCrunch";

Replace the username’s / ID’s in the above code to your own accounts.

Note: There’s a bit of a trick to retrieve the ID of a Facebook Fan page. It’s quite simple though, don’t worry. The above example is the ID of the fan page for The Big Bang Theory. The URL to that fan page is http://www.facebook.com/TheBigBangTheory. In order to find out the ID of that page, just replace “www” in the URL to “graph”, IE: http://graph.facebook.com/TheBigBangTheory. That information contains the ID of the fan page.

Now we’re going to setup a variable that will ensure this code is dynamic and won’t conflict with any other functions. This variable is used to create the new admin bar menu and submenu’s so we’re going to create a variable that is a lowercase, one-word version of your site’s name.

    $theme = get_bloginfo('name');
    $theme = str_replace(' ', '', $theme);
    $theme = strtolower($theme);

You’ll notice this variable being used a bit later.

Now we’re going retrieve the counts for our Twitter Followers, Facebook Fans and RSS Subscribers.

    // get your twitter followers count

        $twitter = file_get_contents('http://twitter.com/users/show/'.$twitterUsername.'.xml'); // customize this with your own twitter username
        $begin = ''; $end = '';
        $page = $twitter;
        $parts = explode($begin,$page);
        $page = $parts[1];
        $parts = explode($end,$page);
        $twitterFollowers = $parts[0];
       
    // get your facebook fan count
   
        $xml = @simplexml_load_file("http://api.facebook.com/restserver.php?method=facebook.fql.query&query=SELECT%20fan_count%20FROM%20page%20WHERE%20page_id=".$facebookPageID."") or die ("a lot");
        $facebookFans = $xml->page->fan_count;

    // get your RSS subscriber count
   
        $stringVal="";
        $url="http://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=";  
        $url= $url. $feedburnerID;
        $ch = curl_init();  
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
        curl_setopt($ch, CURLOPT_URL, $url);  
        $data = curl_exec($ch);  
       
        curl_close($ch);  
        $xml = new SimpleXMLElement($data);
        $feedcount = $xml->feed->entry['circulation'];

We’ve now got the 3 count variables stored in $twitterFollowers, $facebookFans and $feedcount.

Now we get to the actual admin bar menu items. Let’s create the top-level item that’ll house our dropdown.

    $admin_bar->add_menu( array(
        'id'    => $theme.'-social',
        'title' => 'Social',
        'href'  => '#'
    ));

* Note the usage of the $theme variable.

With our main menu item in place, we’ll now add the 3 sub items which echo out our 3 count variables we created.

    $admin_bar->add_menu( array(
        'id'    => $theme.'-social-facebook',
        'parent' => $theme.'-social',
        'title' => 'Facebook ('.$facebookFans.')',
        'href'  => '#',
        'meta'  => array(
            'class' => $theme.'-social-facebook'
        ),
    ));
    $admin_bar->add_menu( array(
        'id'    => $theme.'-social-twitter',
        'parent' => $theme.'-social',
        'title' => 'Twitter ('.$twitterFollowers.')',
        'href'  => '#',
        'meta'  => array(
            'class' => $theme.'-social-twitter'
        ),
    ));
    $admin_bar->add_menu( array(
        'id'    => $theme.'-social-rss',
        'parent' => $theme.'-social',
        'title' => 'RSS ('.$feedcount.')',
        'href'  => '#',
        'meta'  => array(
            'class' => $theme.'-social-rss'
        ),
    ));

Download the final file here and then require or include it from your functions.php file in order to get it working.

And that’s that. Your admin bar is now socially aware and will give you a quick overview of your follower status.

Do you have any other suggestions for cool follower counts that could be included here?

Comments

26/02/2012 9:07 pm
Guru Frog

That’s an incredibly useful addition to the WordPress Admin Bar, we need someone to compile it into a quick and easy plugin!

Reply

Leave a Comment

TimThumb not displaying images? Let’s fix that!

If you've noticed that a number of images aren't displaying correctly ... Read more

George has something new to show you

Hello tadpoles! We're as happy and excited as a dog with two tails, to tell you about the latest theme from the team around the pond. It's called Cohesion, and we like to think of it as "a socially-engaged business theme". Read more

Adding a Tweet Box to your site

Both Windows and Mac are integrating social media platforms more and m... Read more

Valentines Giveaway: Win a copy of our new gorgeous theme – Occasions

Love is in the air, and lucky for you frogs aren't immune to Cupid's m... Read more

Creating a useful 404 page – for both your visitors and yourself

Having a creative 404 page is always fun. Site developers end up spend... Read more

eFrog News Categories

Browse our themes