EA Share Count – Update only on single posts

Any time you display the share counts using EA Share Count, it checks to see when that was last updated and, if too old, it adds that post to an update queue. Once the page has fully loaded, if there are any posts in the update queue it goes to SharedCount.com to get updated share counts.

If you’re displaying the share counts for lots of posts (ex: on an archive page) and you have a lot of traffic, you might have issues. Let’s say a user loads the homepage and 20 posts need updated share counts. Once the page loads, EA Share Count starts getting updated counts for those posts. But the user might not stick around long enough for all 20 posts to get updated share counts, or someone else might load the homepage and update the share counts as well.

The code below limits the share count updater to single posts. At most there will only ever be one post for which to retrieve updated share counts. This will reduce the request time and overall requests, since only people viewing the actual post will trigger an update (seeing an old post in search results won’t).

<?php
/**
* Only update share counts on single post
*
* @param array $update_queue
* @return array
*/
function be_update_share_count_on_single( $update_queue ) {
if( is_singular() && !empty( $update_queue ) )
$update_queue = array_key_exists( get_queried_object_id(), $update_queue ) ? array( get_queried_object_id() => $update_queue[get_queried_object_id()] ) : array();
else
$update_queue = array();
return $update_queue;
}
add_filter( 'ea_share_count_update_queue', 'be_update_share_count_on_single' );
view raw functions.php hosted with ❤ by GitHub

Bill Erickson

Bill Erickson is a freelance WordPress developer and a contributing developer to the Genesis framework. For the past 14 years he has worked with attorneys, publishers, corporations, and non-profits, building custom websites tailored to their needs and goals.

Ready to upgrade your website?

I build custom WordPress websites that look great and are easy to manage.

Let's Talk