Disable share buttons on certain categories

Shared Counts lets you specify where in a post the share buttons appear (before and/or after content). You can also exclude them on a specific post using the metabox when editing that post.

For even more control, you can use the locations filter to move or remove the share buttons programmatically. The code below removes the share buttons from any post in the “sponsored” category.

This code snippet goes in a core functionality plugin or Code Snippets.

/**
 * Remove Shared Counts from sponsored posts 
 *
 * @see https://sharedcountsplugin.com/2019/03/27/disable-share-buttons-on-certain-categories/
 * @author Bill Erickson
 */
function be_remove_shared_counts_from_sponsored_posts( $locations ) {
  if( is_single() && has_category( 'sponsored' ) ) {
    $locations['before']['hook'] = false;
    $locations['before']['filter'] = false;
    $locations['after']['hook'] = false;
    $locations['after']['filter'] = false;
  }
  
  return $locations;
}
add_filter( 'shared_counts_theme_locations', 'be_remove_shared_counts_from_sponsored_posts' );Code language: PHP (php)

Filters used:

Published by Bill Erickson

Bill Erickson is a freelance WordPress developer and a contributor 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.