• Resolved jbweb1

    (@jbweb1)


    Hello,
    I’d like to know how can I get the status of a vote by a user:
    I wanted to create a function who give mycred points to user who voted on a topic and points to user who voted (I already have that part).
    The only thing I struggle with is how can I detect if user already voted ? Because when I use bbp_voting_voted it doesn’t make the difference between already voted and not voted.
    Glad you can help me

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

    (@natekinkead)

    I forgot to update the FAQ section since I added a 4th parameter for $identifier which should help you because it will be a user ID if it’s numeric…

    add_action( 'bbp_voting_voted', 'my_function_on_bbp_vote', 10, 4 );
    function my_function_on_bbp_vote( $post_id, $direction, $score, $identifier ) {
        // Do something
        // $post_id will be the ID of the topic or reply that was voted on
        // $direction will be 1 or -1 representing the upvote or downvote
        // $score will be the new total score
        // $identifier will be either a numeric user ID or an IP address of a non-logged-in visitor
    }
    Thread Starter jbweb1

    (@jbweb1)

    Thank you for your quick reply.
    But how can I know if a user already voted ? Even with the identifier, I still can’t know that test. Or may it exists a function has_user_voted($user_id) ?
    To help you viewing what I’m trying to do, here’s my code:

    add_action( 'bbp_voting_voted', 'my_function_on_bbp_vote', 10, 3 );
    function my_function_on_bbp_vote( $post_id, $direction, $score ) {
    	
    	mycred_add( 'reference', get_current_user_id(), 1, 'Point for voting!' );
    	if (1 == 1) { // here's goes the test where I need to check if user has already voted
    		if ($direction == 1) { // if upvote:
    			mycred_add( 'reference', bbp_get_topic_author_id($post_id), 5, 'Points for asking questions!' );
    		} 
    		else { // if downvote:
    			mycred_add( 'reference', bbp_get_topic_author_id($post_id), -2, 'Points for asking non relevant questions!' );
    		}
    	}
    }

    When I use that code, it give me points when I vote but also when I “unvote” and I don’t want to give points when “unvote”

    • This reply was modified 4 years, 2 months ago by jbweb1.
    Plugin Author natekinkead

    (@natekinkead)

    Oh, I get it. I fixed it in version 2.0.3. bbp_voting_voted hook now will pass $direction = 0 when the user removed their vote.

    Thread Starter jbweb1

    (@jbweb1)

    It works:
    For the next people who try to do something similar, I write you the code I have.

    add_action( 'bbp_voting_voted', 'my_function_on_bbp_vote', 10, 3 );
    function my_function_on_bbp_vote( $post_id, $direction, $score ) {
    
    	if ($direction != 0) { // if user not voted on a topic
    		mycred_add( 'reference', get_current_user_id(), 1, 'Point for voting!' );
    		if ($direction == 1) { // if upvote:
    			mycred_add( 'reference', bbp_get_topic_author_id($post_id), 5, 'Points for asking questions!' );
    		} 
    		if ($direction == -1) { // if downvote:
    			mycred_add( 'reference', bbp_get_topic_author_id($post_id), -2, 'Points for asking non relevant questions!' );
    		}
    	}
    }
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘See if user already voted’ is closed to new replies.