Support » Plugin: ACF Onyx Poll » Multiple polls as a shortcode

  • Resolved malenko123

    (@malenko123)


    Hi,

    Just testing your plugin – but I cannot seem to find any way to display mutiple polls on one page using a shortcode?

    Currently I’m using [onyx-poll]

    Can you please let me know how can I display multiple polls ( I need it strictly as a shortcode ). Thank you 🙂

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author andremacola

    (@andremacola)

    If you are using the Gutenberg editor you can use the ACF Onyx Poll Block under the blocks options.

    For shortcode, each poll has a unique ID and you can pass with the ID parameter. For example: [onyx-poll id=XX]

    So for multiple polls, just add multiple shortcodes like:

    [onyx-poll id=10]
    [onyx-poll id=11]
    [onyx-poll id=12]

    More information at FAQ area on plugin page.

    • This reply was modified 3 years, 8 months ago by andremacola.
    Thread Starter malenko123

    (@malenko123)

    Alright, thank you. That’ll do for now 🙂

    Anyway – one more question, is there any reason I cannot get the current user data inside your plugin?

    Is there any easy way to do this?

    Almost every time I get 0 for current ID.

    I’m trying to get the current user ID inside poll-api.php right before you post the results in the poll post.

    Plugin Author andremacola

    (@andremacola)

    I would need to see how you are doing this. Maybe it’s something related to WordPress REST API

    If you create an issue on Github with some example, I can check in my spare time

    Thread Starter malenko123

    (@malenko123)

    Hey,

    Actually haven’t inserted any big code – just tried getting the current user in function ‘vote’.

    /**
    	 * Compute vote
    	 * @param int $req['poll'] poll ID required
    	 * @param int $req['choice'] poll answer choice required
    	 * @todo: [$poll_choice - 1] is just a temp fix. maybe acf/settings/row_index_offset filter?
    	 */
    	public function vote($req) {
    	
    		// params vars
    		$poll_id       = $req['poll'];
    		$poll_choice   = (int) $req['choice'];
    		$poll_answers  = get_field($this->field['answers'], $poll_id);
    		$poll_total    = get_field($this->field['total'], $poll_id);
    		$poll_expired  = get_field($this->field['expired'], $poll_id);
    		$poll_limit    = get_field($this->field['limit_vote'], $poll_id);
    
    		// validate params from req
    		if (!is_numeric($poll_id) || !isset($poll_choice) || empty($poll_answers[$poll_choice - 1])) {
    			return new WP_Error('error', $this->message['invalid'], array('status' => 400));
    		}
    		if ($poll_expired || get_post_type($poll_id) != 'onyxpolls') {
    			return new WP_Error('error', $this->message['no_exist'], array('status' => 400));
    		}
    		if (isset($_COOKIE["onyx_poll_limit_$poll_id"]) && $poll_limit != 1) {
    			$response = array(
    				"code"     => "not_allowed",
    				"id"       => $poll_id,
    				"message"  => $this->message['no_allowed'],
    				"voted"    => false,
    				"data"     => ["status" => 200]
    			);
    		} else {
    			// update vote fields
    			$add_vote = array(
    				"votes" => $poll_answers[$poll_choice - 1]['votes']+1
    			);
    			$row   = update_row($this->field['answers'], $poll_choice, $add_vote, $poll_id);
    			$total = update_field($this->field['total'], $poll_total+1, $poll_id);
    
    			// set cookies
    			// limit = 1 (free vote)
    			// limit = 2 (per device/no expires)
    			$this->setcookie($poll_id, $poll_choice);
    
     
    	   		$user_id = get_current_user_id(); // RETURNS 0
    
    			// global $current_user;
    		 	//get_currentuserinfo();
    		   	//var_dump($current_user->ID);
    
    	   		/* when the POLL is answered get the current user ID in an repeater acf field which I've implemented in each post (poll) via acf */
    	   		/* I'm trying to LIMIT the submission to a USER - so not per defice - but poll per user */	   		
    			$field_key = "field_5f220c11c3fc1"; // repeater field
    			$value = array('field_5f220c36c3fc2' => $user_id);
    			add_row($field_key, $value, $poll_id);
    
    			// do_action('acf/save_post', $poll_id);		  
    			// wp_redirect(add_query_arg('updated', 'success', wp_get_referer()));
    
    			// return reponse
    			$response = array(
    				"code"     => ($row && $total) ? "success" : 'error',
    				"poll"     => $poll_id,
    				"choice"   => $poll_choice,
    				"message"  => ($row && $total) ? $this->message['success'] : $this->message['error'],
    				"voted"    => ($row && $total) ? true : false,
    				"data"     => ["status" => 200]
    			);
    		}
    
    		$response += $this->poll_data($poll_id);
    		return new WP_REST_Response($response, 200);
    	}

    Take a look at the comments. Tried to take the current user id with get_current_user_id() and it returns 0.

    What I’m actually trying to do is to limit the poll per USER.

    Thank you.

    Plugin Author andremacola

    (@andremacola)

    The WordPress REST API operates differently for user roles

    Please, see: https://stackoverflow.com/questions/55801175/get-current-user-inside-register-rest-route-method

    Thread Starter malenko123

    (@malenko123)

    Alright, thanks – I’ll give it a look.

    Anyhow any advice on how can I limit the poll to a user?

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Multiple polls as a shortcode’ is closed to new replies.