• Resolved mativve

    (@mativve)


    Hi! I use shortcode to add custom reactions. Unfortunately I must disable reactions for owner of this object (in this case the image) and I want to show only count of reactions. There is any way to get only visual counts of reactions without functionality? Or there is only way to do it by custom query to wordpress database?

    Unfortunately official website don’t work and any documentation don’t exist ??

    Thanks for any advices ??

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Daniele Alessandra

    (@danielealessandra)

    Hello Mativve, thank you for using DaReactions.

    As you know, the plugin already have a shortcode to display the reactions widget, I suggest creating a new shortcode to handle your current needs.

    The following snippet can be added to you theme’s functions.php file or in any other place that your WordPress instance is using.

    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(
              '<ul><li>Total reactions: %s</li><li>Main reaction: %s</li></ul>',
              \DaReactions\Utils::formatBigNumber( $total_reactions ),
              $main_reaction
            );
          } else {
            return 'No reactions found for the specified post.';
          }
        } else {
          return 'No post specified and no global post available.';
        }
      }
    
      return 'Reaction class not found, is the plugin active?';
    }
    
    add_shortcode( 'reactions_count', 'get_reactions_count' );

    Previous shortcode can be used to display only simple count data:

    [reactions_count post_type=post post_id=1]

    Result:

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

    Assuming that you have a post with id=1 with more than 78 thousands reactions and that the most common reaction for this content is labeled “Like”.

    I am moving my documentation on HelpScout, it is still a work in progress, but you can access docs here: https://da-plugins.helpscoutdocs.com/

    Thread Starter mativve

    (@mativve)

    @danielealessandra Thank you for fast response! It’s work! I really appriciated!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to get reaction from custom object’ is closed to new replies.