How to Display Custom Data using a Shortcode

If you need to display custom reactions-related data, e.g. only the count of reactions for a custom object, you can create a custom shortcode for this purpose using DaReactions.

Step-by-Step Guide

1. Add the Custom Shortcode to Your Theme's `functions.php` File:

First, you need to add a function to your theme's functions.php file. This function will create a shortcode that retrieves and displays only the count of reactions without providing the interaction functionality.

function get_reactions_count( $attributes ) {
  if ( class_exists( '\DaReactions\Data' ) ) {
    global $post;

    // Extract attributes and set default values
    $attributes = shortcode_atts( array(
      'post_type' => isset( $post ) ? $post->post_type : '',
      'post_id'   => isset( $post ) ? $post->ID : 0,
    ), $attributes, 'reactions_count' );

    // Use shortcode parameters if provided, otherwise use global $post object
    $post_type = $attributes['post_type'];
    $post_id   = $attributes['post_id'];
    if ( $post_type && $post_id ) {
      $reactions = \DaReactions\Data::getReactionsForContent(
        $post_id,
        $post_type
      );

      if ( $reactions ) {
        $total_reactions = 0;
        $main_reaction   = '';
        $max_reactions   = 0;

        foreach ( $reactions as $reaction ) {
          $total_reactions += $reaction->total;
          if ( $reaction->total > $max_reactions ) {
            $max_reactions = $reaction->total;
            $main_reaction = $reaction->label;
          }
        }
        return sprintf(
          &#39;<ul><li>Total reactions: %s</li><li>Main reaction: %s</li></ul>&#39;,
          \DaReactions\Utils::formatBigNumber( $total_reactions ),
          $main_reaction
        );
      } else {
        return &#39;No reactions found for the specified post.&#39;;
      }
    } else {
      return &#39;No post specified and no global post available.&#39;;
    }
  }
  return &#39;Reaction class not found, is the plugin active?&#39;;
}
add_shortcode( &#39;reactions_count&#39;, &#39;get_reactions_count&#39; );

2. Use the Custom Shortcode:

You can use the [reactions_count] shortcode in your content by specifying the post_type and post_id parameters. For example:

 [reactions_count post_type="post" post_id="1"]

This will display the total number of reactions and the most common reaction for the specified post.

Example Output

Assuming you have a post with post_id=1 that has over 78,000 reactions, and the most common reaction is "Like", the shortcode will produce the following output:

  • Total reactions: 78K+
  • Main reaction: Like

Summary

By following this guide, you can create a custom shortcode to display only the count of reactions for any content type in your WordPress site using DaReactions. This allows you to disable reactions for specific users, such as the owner of the content, while still showing the overall engagement.

This tutorial was originally written to answer a question on wordpress.org.

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.

Still need help? Contact Us Contact Us