Skip to:
Content
Pages
Categories
Search
Top
Bottom

PHP Functions for WordPress Post Voting


  • Aron Prins
    Keymaster

    @aronprins

    Hey everyone,

    While working on our WP Knowledgebase Plugin I wanted to add a feature that allowed a single post in a specific custom post type to display up or down vote buttons below the_content.

    Here’s the two functions we’re currently using on our WP Knowledgebase Plugin:

    /**
     * Adds a vote form after the_content
     */
    function wpkb_add_vote_form( $content ) {
      global $post;
      if ( is_singular( 'knowledgebase' ) && get_post_type() === 'knowledgebase' ) {
        $vote_count = get_post_meta( $post->ID, 'article_vote_count', true );
        if ( empty( $vote_count ) ) {
          $vote_count = 0;
        }
    
        $vote_form = '<form method="post">';
        $vote_form .= '<button type="submit" name="article_vote_type" value="upvote">?</button>&nbsp;';
        $vote_form .= '<button type="submit" name="article_vote_type" value="downvote">?</button>';
        $vote_form .= '</form>';
        $vote_form .= '<p>Vote count: ' . $vote_count . '</p>';
    
        // Check if the user has voted and prevent them from voting again
        $has_voted = false;
        $vote_cookie_name = 'knowledgebase_vote_' . $post->ID;
        if ( isset( $_COOKIE[ $vote_cookie_name ] ) ) {
          $has_voted = true;
        }
    
        if ( $has_voted ) {
          $vote_form = '<p>Vote count: ' . $vote_count . '</p>';
        }
    
        return $content . $vote_form;
      }
    
      return $content;
    }
    
    add_filter( 'the_content', 'wpkb_add_vote_form' );
    
    /**
     * Processes a vote if a up or down button is clicked.
     */
    function wpkb_process_vote() {
      if ( isset( $_POST['article_vote_type'] ) ) {
        add_action( 'wp', function() {
          $vote_type = sanitize_text_field( $_POST['article_vote_type'] );
          if ( $vote_type === 'upvote' || $vote_type === 'downvote' ) {
            $post_id = get_the_ID();
    
            $vote_count = get_post_meta( $post_id, 'article_vote_count', true );
            if ( empty( $vote_count ) ) {
              $vote_count = 0;
            }
    
            // Check if the user has voted and prevent them from voting again
            $has_voted = false;
            $vote_cookie_name = 'knowledgebase_vote_' . $post_id;
            if ( isset( $_COOKIE[ $vote_cookie_name ] ) ) {
              $has_voted = true;
            }
    
            if ( ! $has_voted ) {
              if ( $vote_type === 'upvote' ) {
                $vote_count++;
              } else {
                $vote_count--;
              }
    
              update_post_meta( $post_id, 'article_vote_count', $vote_count );
    
              // Set the vote cookie
              setcookie( $vote_cookie_name, $vote_type, time() + 3600, '/' );
            }
          }
        } );
      }
    }
    add_action( 'init', 'wpkb_process_vote' );

    Feel free to use them in your plugin (credit is always appreciated) or your theme!

    Cheers,
    Aron

    • This topic was modified 1 year, 10 months ago by Aron Prins.
    • This topic was modified 1 year, 10 months ago by Aron Prins.
  • You must be logged in to reply to this topic.
Skip to toolbar