• I really like having a share counter on my blogposts. I noticed that it actually encourages visitors to share the content themselves. Because there are no WordPress sharecount plugins out there that I actually find satisfying (most of them make way to much calls), I wrote the code myself.

    It works perfect, but still slows down my site. So I would rather it caches and refreshes once per hour or so. I don’t know how to manage this though … Any ideas?

    This is what I put in the themes function file:

    class shareCount {
    private $url,$timeout;
    function __construct($url,$timeout=10) {
    $this->url=rawurlencode($url);
    $this->timeout=$timeout;
    }
    
    function get_tweets() {
    $json_string = $this->file_get_contents_curl('https://urls.api.twitter.com/1/urls/count.json?url=' . $this->url);
    $json = json_decode($json_string, true);
    return isset($json['count'])?intval($json['count']):0;
    }
    
    function get_fb() {
    $json_string = $this->file_get_contents_curl('https://api.facebook.com/restserver.php?method=links.getStats&format=json&urls='.$this->url);
    $json = json_decode($json_string, true);
    return isset($json[0]['total_count'])?intval($json[0]['total_count']):0;
    }
    
    private function file_get_contents_curl($url){
    $ch=curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    curl_setopt($ch, CURLOPT_FAILONERROR, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
    $cont = curl_exec($ch);
    if(curl_error($ch))
        {
            die(curl_error($ch));
        }
            return $cont;
        }
    }

    And this is what I use in single.php:

    <!-- Begin mod: Add share counter -->
    <span class="share-count">
        <?php
        $obj=new shareCount(get_permalink( $post->ID ));
        echo $obj->get_tweets() + $obj->get_fb();
        ?>
    </span>
    <span class="share-text">
        keer gedeeld
    </span>
    <!-- End mod: Add share counter -->

    Then I also add some css.

Viewing 8 replies - 1 through 8 (of 8 total)
  • Use the Transient API!

    Thread Starter Bitmaniacs

    (@bitmaniacs)

    Hmmm, looks good, but this is new to me, how does this actually work?

    Thread Starter Bitmaniacs

    (@bitmaniacs)

    Or … Is there maybe an option to lazy load the social counter?

    Moderator bcworkz

    (@bcworkz)

    You could lazy load, that would help with page load speed, but you’re still making way more cURL requests than is necessary, assuming a reasonable traffic load.

    Transients can be part of the solution, but the missing key is you need to schedule the count requests. Once you get the counts, they can be stored in a transient that expires at a time somewhat longer than the schedule interval.

    Your pages will load faster and you will be a better ‘netizen’ by not overly taxing third party resources.

    Thread Starter Bitmaniacs

    (@bitmaniacs)

    Thanks bcworkz!

    This sounds like a great solution … But beyond my endless limitations. Do you by any chance de freelance work?

    Moderator bcworkz

    (@bcworkz)

    Do I do freelance work? Not through this site, it’s actually against the rules here to even solicit work, much less accept it. Consider hiring someone from WordPress Jobs or Code Poet. You will not find me there though (sorry), I’m busy enough as it is, but thanks for the offer.

    Should be pretty easy to get this setup with transients. When a transient is expired it will return false so every time that it expires you could call the update from the API.

    In your single.php it would look something like this:

    <!-- Begin mod: Add share counter -->
    <span class="share-count">
        <?php
        $social_count = get_transient( 'social_' . $post->ID );
    
        if( $social_count == false ){
            //transient expired update it
            $obj=new shareCount(get_permalink( $post->ID ));
            $social_count =  $obj->get_tweets() + $obj->get_fb();
    
            set_transient( 'social_' . $post->ID, $social_count, 60 * 60 * 24 );
        }
    
        echo $social_count;
    
        ?>
    </span>
    <span class="share-text">
        keer gedeeld
    </span>
    <!-- End mod: Add share counter -->
    Thread Starter Bitmaniacs

    (@bitmaniacs)

    Thanks bcworkz & thanks a million Simalam!

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Caching custom social share count in WordPress’ is closed to new replies.