• Resolved pikashoo

    (@pikashoo)


    Hi,

    How should we use this ‘count_pro_percent’ => Helpful\Core\Helpers\Stats::get_pro($post_id, true), in shortcode?

    I tried many formats but can’t seem to get any to work.
    I just want to display the percentage of upvotes for the specific post.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Pixelbart

    (@pixelbart)

    @pikashoo

    Unfortunately, this is not so easy. You have to copy the template and adapt it to make it work.

    You copy this file: https://gist.github.com/pixelbart/3f81d45ec0fdbf5da4ca779ba6bcff33
    Into the folder: wp-content/themes/your-theme/helpful/helpful.php

    You can also help out with JavaScript if you like. Here’s an example, completely unoptimized, but it should work:

    jQuery('.helpful').each(function() {
        let helpful = jQuery(this);
        let proButton = helpful.find('.helpful-pro');
        let contraButton = helpful.find('.helpful-contra');
        let pro = parseInt(proButton.find('.helpful-counter').text());
        let contra = parseInt(contraButton.find('.helpful-counter').text());
        let total = pro + contra;
        let proPercent = Math.round((pro / total) * 100);
        let contraPercent = Math.round((contra / total) * 100);
    
        proButton.find('.helpful-counter').text(proPercent + '%');
        contraButton.find('.helpful-counter').text(contraPercent + '%');
    });

    If you’d rather put this in your functions.php, that works too:

    add_action('wp_footer', function() {
        ?>
    <script type="text/javascript">
    jQuery('.helpful').each(function() {
        let helpful = jQuery(this);
        let proButton = helpful.find('.helpful-pro');
        let contraButton = helpful.find('.helpful-contra');
        let pro = parseInt(proButton.find('.helpful-counter').text());
        let contra = parseInt(contraButton.find('.helpful-counter').text());
        let total = pro + contra;
        let proPercent = Math.round((pro / total) * 100);
        let contraPercent = Math.round((contra / total) * 100);
    
        proButton.find('.helpful-counter').text(proPercent + '%');
        contraButton.find('.helpful-counter').text(contraPercent + '%');
    });
    </script>
    <?php
    });

    Hopefully I could help you in some way. If I have misunderstood you, then write me briefly what exactly you have in mind.

    Greetings Kevin

    Thread Starter pikashoo

    (@pikashoo)

    Hi Kevin,

    Thanks alot for your reply.

    The code in my functions.php file works great, but it’s not really what I meant to do, my first post was a bit unclear.

    If I add the code to my functions.php, it changes the counters to a percentage instead of the vote count.

    What I want to do is use the vote count on the bottom of every post, but I would like to add something like: 80% of people found this post usefull in the post meta.
    I want to output the number 80 as a shortcode.

    I got everything working, but I just would the shortcode to output the number of votes in %

    Plugin Author Pixelbart

    (@pixelbart)

    @pikashoo

    Now I hope I have understood you. Here, this should work (functions.php):

    add_shortcode('pikashoo_helpful', function ($atts) {
        $defaults = [
            'type' => 'pro',
            'percentage' => true,
            'format_percentage' => '%d%%',
            'format' => '%d',
            'post_id' => null,
        ];
    
        $atts = shortcode_atts($defaults, $atts);
    
        extract($atts);
    
        $is_percentage = ($percentage === true);
    
        if ($post_id === null) {
            $post_id = get_the_ID();
        }
    
        if ($type === 'pro') {
            $value = Helpful\Core\Helpers\Stats::get_pro( $post_id, $is_percentage );
        } else {
            $value = Helpful\Core\Helpers\Stats::get_contra( $post_id, $is_percentage );
        }
    
        if ($is_percentage) {
            $content = sprintf($format, $value));
        } else {
            $content = sprintf($format_percentage, $value));
        }
    
        return apply_filters('pikashoo_helpful_content', $content, $atts);
    });

    default options
    [pikashoo_helpful type="pro" percentage="true" format_percentage="%d%%" format="%d" post_id="null"]

    pro percentage
    [pikashoo_helpful type="pro"]

    contra percentage
    [pikashoo_helpful type="contra"]

    It is best to show only one value at a time, as there can sometimes be differences due to rounding.

    Greetings Kevin

    Thread Starter pikashoo

    (@pikashoo)

    Hi Kevin,

    Working like a charms, thanks so much!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Count pro percent shortcode’ is closed to new replies.