Include Shared Counts in Display Posts Shortcode

Shared Counts is a powerful social sharing plugin, and Display Posts Shortcode is a tool for displaying dynamic queries for content without any code.

The following code allows you to add include_shared_counts="true" to the shortcode to display the share buttons.

You can also use the shared_counts_style parameter to change what button style is used. By default it uses whatever you have set in Settings > Shared Counts.

This adds the share buttons at the very top of the post. You can change this position by moving the $share variable in line 22, where the output is built.

/**
 * Include Shared Counts in Display Posts Shortcode 
 *
 * @author Bill Erickson 
 * @link https://www.billerickson.net/code/shared-counts-display-posts-shortcode/
 */
function be_shared_counts_dps( $output, $original_atts, $image, $title, $date, $excerpt, $inner_wrapper, $content, $class, $author, $category_display_text ) {
  // Check for include_shared_counts="true"
  if( ! ( isset( $original_atts['include_shared_counts'] ) && filter_var( $original_atts['include_shared_counts'], FILTER_VALIDATE_BOOLEAN ) )
    return $output;
    
  // Make sure Shared Counts is active 
  if( ! function_exists( 'shared_counts' ) )
    return $output;
  
  $style = !empty( $original_atts['shared_counts_style'] ) ? esc_attr( $original_atts['shared_counts_style'] ) : false;
  $share = shared_counts()->front->display( $location = 'dps', $echo = false, $style ); 
  $output = '<' . $inner_wrapper . ' class="' . implode( ' ', $class ) . '">' . $share . $image . $title . $date . $author . $category_display_text . $excerpt . $content . '</' . $inner_wrapper . '>';
  return $output;
}
add_action( 'display_posts_shortcode_output', 'be_shared_counts_dps', 10, 11 );

Bill Erickson

Bill Erickson is the co-founder and lead developer at CultivateWP, a WordPress agency focusing on high performance sites for web publishers.

About Me
Ready to upgrade your website?

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

Let's Talk