How to Modify The Share Counters Value Appearing on Your Site?

Article sections

    since 4.1

    Easy Social Share Buttons for WordPress has filter that you can use to modify the generated share counters on screen before render. Using this filter you can generate your own values of share counters. To do this you need to call essb4_get_cached_counters for example in your theme functions.php file.

    apply_filters('essb4_get_cached_counters', $cached_counters);

     

    Parameters:

    $cached_counters (array) containing current share counter values by social networks (total share counter value is also present as key)

    Using this filter you can easy create a fully functional replacement of share counters for each social network using the click over share buttons. Your share buttons will increase their values with each button click and indicate over front. This will also allow to setup initial share counter of each social network. Those values are stored inside post meta fields with prefix essb_pc_<network>.

    if (!function_exists('essb_cache_counters_parse')) {
    	function essb_cache_counters_parse($cached_counters = array()) {
    		global $post;
    		
    		if (!isset($post)) return $cached_counters;
    		
    		$post_id = $post->ID;
    		
    		$cumulative_total = 0;
    		foreach ($cached_counters as $network => $shares) {
    			
    			if ($network == 'total') continue;
    			
    			$shares = get_post_meta($post_id, 'essb_pc_'.$network, true);
    			
    			$cached_counters[$network] = $shares;
    			$cumulative_total += intval($shares);
    		}
    		
    		$cached_counters['total'] = $cumulative_total;
    	
    		return $cached_counters;
    	}
    	
    	add_filter('essb4_get_cached_counters', 'essb_cache_counters_parse');
    }

     

    Was this article helpful?
    YesNo
    in Developers
    Was this article helpful?
    YesNo