0

i want edit this code to auto increment not just +2 for each user's click and instead increment number would be random ( + 2 , + 4 , ... )

if( $should_count && ( ( isset( $views_options['use_ajax'] ) && (int) $views_options['use_ajax'] === 0 ) || ( !defined( 'WP_CACHE' ) || !WP_CACHE ) ) ) {
                update_post_meta( $id, 'views', $post_views + 2 );
                do_action( 'postviews_increment_views', $post_views + 2 );

my full wp postview code bellow:

### Function: Calculate Post Views
add_action( 'wp_head', 'process_postviews' );
function process_postviews() {
    global $user_ID, $post;
    if ( is_int( $post ) ) {
        $post = get_post( $post );
    }
    if ( ! wp_is_post_revision( $post ) && ! is_preview() ) {
        if ( is_single() || is_page() ) {
            $id = (int) $post->ID;
            $views_options = get_option( 'views_options' );
            if ( !$post_views = get_post_meta( $post->ID, 'views', true ) ) {
                $post_views = 0;
            }
            $should_count = false;
            switch( (int) $views_options['count'] ) {
                case 0:
                    $should_count = true;
                    break;
                case 1:
                    if( empty( $_COOKIE[ USER_COOKIE ] ) && (int) $user_ID === 0 ) {
                        $should_count = true;
                    }
                    break;
                case 2:
                    if( (int) $user_ID > 0 ) {
                        $should_count = true;
                    }
                    break;
            }
            if ( isset( $views_options['exclude_bots'] ) && (int) $views_options['exclude_bots'] === 1 ) {
                $bots = array(
                    'Google Bot' => 'google'
                    , 'MSN' => 'msnbot'
                    , 'Alex' => 'ia_archiver'
                    , 'Lycos' => 'lycos'
                    , 'Ask Jeeves' => 'jeeves'
                    , 'Altavista' => 'scooter'
                    , 'AllTheWeb' => 'fast-webcrawler'
                    , 'Inktomi' => 'slurp@inktomi'
                    , 'Turnitin.com' => 'turnitinbot'
                    , 'Technorati' => 'technorati'
                    , 'Yahoo' => 'yahoo'
                    , 'Findexa' => 'findexa'
                    , 'NextLinks' => 'findlinks'
                    , 'Gais' => 'gaisbo'
                    , 'WiseNut' => 'zyborg'
                    , 'WhoisSource' => 'surveybot'
                    , 'Bloglines' => 'bloglines'
                    , 'BlogSearch' => 'blogsearch'
                    , 'PubSub' => 'pubsub'
                    , 'Syndic8' => 'syndic8'
                    , 'RadioUserland' => 'userland'
                    , 'Gigabot' => 'gigabot'
                    , 'Become.com' => 'become.com'
                    , 'Baidu' => 'baiduspider'
                    , 'so.com' => '360spider'
                    , 'Sogou' => 'spider'
                    , 'soso.com' => 'sosospider'
                    , 'Yandex' => 'yandex'
                );
                $useragent = isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : '';
                foreach ( $bots as $name => $lookfor ) {
                    if ( ! empty( $useragent ) && ( false !== stripos( $useragent, $lookfor ) ) ) {
                        $should_count = false;
                        break;
                    }
                }
            }
            $should_count = apply_filters( 'postviews_should_count', $should_count, $id );
            if( $should_count && ( ( isset( $views_options['use_ajax'] ) && (int) $views_options['use_ajax'] === 0 ) || ( !defined( 'WP_CACHE' ) || !WP_CACHE ) ) ) {
                update_post_meta( $id, 'views', $post_views + 2 );
                do_action( 'postviews_increment_views', $post_views + 2 );
            }
        }
    }
}
3
  • So you just need to pick a random number, save it in a variable so you use the same value twice, and then swap the + 2s for + that variable? Which bit do you need help with?
    – Rup
    Commented Nov 24, 2020 at 23:27
  • And why? Why not +1? That's the actual number of views you've got.
    – Rup
    Commented Nov 24, 2020 at 23:27
  • @Rup yes i want to change codes from constant increment to random increamen. pleas tell me codes i need to replace. tnx
    – samanta
    Commented Nov 29, 2020 at 22:38

1 Answer 1

0

To randomly add 1-5 views, you can change the if ( $should_count block at the bottom to:

if( $should_count && ( ( isset( $views_options['use_ajax'] ) && (int) $views_options['use_ajax'] === 0 ) || ( !defined( 'WP_CACHE' ) || !WP_CACHE ) ) ) {
    $random_increment = rand( 1, 5 );
    update_post_meta( $id, 'views', $post_views + $random_increment );
    do_action( 'postviews_increment_views', $post_views + $random_increment );
}

I'm still not sure why you'd want to do this though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.