• Resolved abelhoyos

    (@abelhoyos)


    I would like that only users registered as Subscribers can vote and see the results. It’s possible?

Viewing 12 replies - 1 through 12 (of 12 total)
  • Plugin Support Patrick – WPMU DEV Support

    (@wpmudevsupport12)

    Hi @abelhoyos

    I hope you are doing well.

    Could you please try this script:

    <?php
    
    add_filter( 'forminator_render_form_submit_markup', 'wpmudev_render_poll_submit_markup', 10, 4 );
    function wpmudev_render_poll_submit_markup( $html, $form_id, $post_id, $nonce ) {
    	if ( 3611 == $form_id && ! is_user_logged_in() ) {
    		$html = '<div class="forminator-poll-footer forminator-poll--actions"><p>You should be logged-in to submit your answer.</p></div>';
    	}
    
    	return $html;
    }

    Add it as a mu-plugin https://wpmudev.com/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-plugins replace the 3611 with your form ID, the ID can be found in the URL when editing the Poll.

    Let us know the result you got.
    Best Regards
    Patrick Freitas

    Thread Starter abelhoyos

    (@abelhoyos)

    Ooh, excellent. I was waiting for this answer for many hours. Will try it to see if it works or not. One moment and I’ll tell you.

    Thread Starter abelhoyos

    (@abelhoyos)

    That’s it, it works perfect, but I want you to only be able to vote once. That is, only 1 vote per registered User.

    Thread Starter abelhoyos

    (@abelhoyos)

    I tried with this code, but dont work for me 🙁

    <?php
    
    add_filter(
    	'forminator_render_form_markup',
    	function( $html, $form_fields, $form_type, $form_settings, $form_design, $render_id ) {
    		if ( is_user_logged_in() ) {
    			global $wpdb;
    			$user_id_field_name = 'hidden-1';
    			$user_id            = get_current_user_id();
    			$message            = __( 'You can submit this form only once' );
    
    			$user_entries = $wpdb->get_var(
    				$wpdb->prepare(
    					"SELECT COUNT(e.entry_id) FROM {$wpdb->prefix}frmt_form_entry as e
    					INNER JOIN {$wpdb->prefix}frmt_form_entry_meta as m ON e.entry_id=m.entry_id
    					WHERE m.meta_key=%s AND m.meta_value=%d 
    					LIMIT 1",
    					$user_id_field_name,
    					$user_id
    				)
    			);
    
    			if ( ! empty( $user_entries ) || 0 < $user_entries ) {
    				return $message;
    			}
    		}
    
    		return $html;
    	},
    	10,
    	6
    );
    Thread Starter abelhoyos

    (@abelhoyos)

    They should have this option enabled https://snipboard.io/xaPmKZ.jpg

    Thread Starter abelhoyos

    (@abelhoyos)

    And I want it to be like this for all the polls that I can create, because I want to create multiple polls.

    Plugin Support Williams – WPMU DEV Support

    (@wpmudev-support8)

    Hi @abelhoyos

    The code that you tried is for forms only.

    For polls, this should do the trick:

    <?php
    
    add_filter( 'forminator_poll_handle_form_user_can_vote', 'wmudev_check_user_submission_poll', 10, 2 );
    function wmudev_check_user_submission_poll( $user_can_vote, $module_id ) {
    
        if ( !is_user_logged_in() ) {     // only logged-in users can vote
            $user_can_vote = false;
            throw new Exception( esc_html__( 'You must be logged in to vote in this poll', 'forminator' ) );
            return $user_can_vote;
        }
    
        $user_id = get_current_user_id(); // get logged-in user's user_id
    
        $users_with_vote = get_option( $module_id.'_poll_users', array() );   // get list of those who have previously voted in this poll
        if ( ! empty( $users_with_vote ) ) {
            if ( in_array( $user_id, $users_with_vote ) ) {                   // if logged-in user is in that list...
                $user_can_vote = false;                                       //  they can't vote again
                throw new Exception( esc_html__( 'You have already submitted a vote to this poll', 'forminator' ) );
            }
        }
    
        return $user_can_vote;
    }
    
    add_action( 'forminator_poll_after_handle_submit', 'wpmudev_record_user_poll_submission', 10, 2 );
    add_action( 'forminator_poll_after_save_entry', 'wpmudev_record_user_poll_submission', 10, 2 );
    function wpmudev_record_user_poll_submission( $module_id, $response ) {
    
        if ( is_user_logged_in() ) {
            $user_id = get_current_user_id();                                 // get user_id
            $option = get_option( $module_id.'_poll_users', array() );        // get array of those who have already voted in this poll
            array_push( $option, $user_id );                                  // add user_id to the array
            $updated_option = array_unique( $option );                        // update the array
            update_option( $module_id.'_poll_users', $updated_option );
        }
    }

    It applies to all the polls and would only accept vote from user that did not voted in a given poll yet. Note: it will show the poll and let them choose option/press vote but the vote will not be registered and instead a message will be displayed: “You have already submitted a vote to this poll.

    You can change that message in this line of code:

    throw new Exception( esc_html__( 'You have already submitted a vote to this poll', 'forminator' ) );

    Kind regards,
    Adam

    Thread Starter abelhoyos

    (@abelhoyos)

    Is perfect. Thank you so much. I would like the results to be displayed for the people who voted. https://snipboard.io/D4etCx.jpg https://snipboard.io/LyfQOi.jpg

    Thread Starter abelhoyos

    (@abelhoyos)

    that is, the users who have already voted, I want them to always see the poll when they log in.

    Thread Starter abelhoyos

    (@abelhoyos)

    I am interested in buying a 1 year membership. In that case, is the support or chat immediate?

    Hi @abelhoyos,

    Hope this message finds you well and thanks for your interest in becoming a member.

    We notice that you have a membership and you already are getting assistance from our Support.

    Kind regards,
    Laura

    Plugin Support Kris – WPMU DEV Support

    (@wpmudevsupport13)

    Hi @abelhoyos

    We haven’t heard from you in a while, I’ll go and mark this thread as resolved. If you have any additional questions or require further help, please let us know!

    Kind Regards,
    Kris

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Vote in the Forminator survey, but only for previously registered Users.’ is closed to new replies.