vanpariyar / wp-post-views

This is the simple plugin that counts the views of the your wordpress website
https://wordpress.org/plugins/wp-post-views/
GNU General Public License v3.0
8 stars 4 forks source link

How to translate the output of counter ? #14

Open vanpariyar opened 2 years ago

vanpariyar commented 2 years ago

Thanks for this awesome and simple plugin. I want to translate/str_replace of the output of the counter. How can I do that ? For example, I want to replace “198” with “१९८”. (Latin to Devanagari)

vanpariyar commented 2 years ago

Thanks for this awesome and simple plugin. I want to translate/str_replace of the output of the counter. How can I do that ? For example, I want to replace “198” with “१९८”. (Latin to Devanagari)

I fiddled around the code and settings near about 3 hours, and finally found the solution to this question. This needs the plugin to be edited. I am sharing my modifications to help anyone wanting to the same thing.

Modify includes/shortcode.php file in this way :

<?php

add_action( 'init', 'wppv_add_custom_shortcode' );
function wppv_add_custom_shortcode() {
    /**
     * @param $post which is Post id ( Optional )
     * @author of shortcode Ronak Vanpariya.
     * @desc Get Post Count For the Blog.
     */

    function wppv_current_post_view_callback($atts = array() , $content = ''){ 
        $meta_key         = 'entry_views';
        $view_post_meta   = get_post_meta(get_the_ID(), $meta_key, true);
        $view_post_meta = str_replace('1', '१', $view_post_meta);
        $view_post_meta = str_replace('2', '२', $view_post_meta);
        $view_post_meta = str_replace('3', '३', $view_post_meta);
        $view_post_meta = str_replace('4', '४', $view_post_meta);
        $view_post_meta = str_replace('5', '५', $view_post_meta);
        $view_post_meta = str_replace('6', '६', $view_post_meta);
        $view_post_meta = str_replace('7', '७', $view_post_meta);
        $view_post_meta = str_replace('8', '८', $view_post_meta);
        $view_post_meta = str_replace('9', '९', $view_post_meta);
        $view_post_meta = str_replace('0', '०', $view_post_meta); 
        return $view_post_meta;
    }
    if(!shortcode_exists( 'WPPV-TOTAL-VIEWS' )){
        add_shortcode( 'WPPV-TOTAL-VIEWS', 'wppv_current_post_view_callback' );
    }   
}

Expand After the line $view_post_meta = get_post_meta(get_the_ID(), $meta_key, true); I entered the str_replace lines, and finally return $view_post_meta;

You can replace the Devanagari Numerals to any script ones.