Skip to content

Preserve HTTP Counts

Jared Atchison edited this page Jul 19, 2018 · 4 revisions

This is a feature in Shared Counts. When enabled (screenshot), this setting will preserve all share counts associated to "http" URLs.

This is useful (and should only be used) when an existing site switches to SSL. This will change the URL schema from http to https. When the URL changes, the previous counts are not tracked.

Preserving HTTP counts will tell Shared Counts to fetch the share counts for both versions of posts; both http and https. The counts displayed will be to total of both URLs.

Note: Turning on this feature will double the number of API calls needed to update the counts, since we need to check two versions of the URL (https and http).

Lastly, we provide filter that can be used to only check/preserve HTTP counts for posts that were published before SSL was implemented. An example of using this filter is below.

/**
 * Don't preserve share counts for posts that are published after the date
 * SSL was implemented (Sep 1 2017 in the example below).
 *
 * @param bool $preserve
 * @param int $id Post ID.
 *
 * @return bool
 */
function shared_counts_maybe_preserve_http( $preserve, $id ) {
	
	// Published date.
	$post_date = get_the_date( 'U', $id );

	// Date SSL was implemented.
	$ssl_date = strtotime( 'September 1, 2017' );

	// If this post has been published after we implemented SSL, don't
	// track and lookup HTTP share counts.
	if ( $ssl_date > $post_date ) {
		return false;
	}

	// This will return true by default.
	return $preserve;
}
add_filter( 'shared_counts_preserve_http', 'shared_counts_maybe_preserve_http', 10, 2 );
Clone this wiki locally