Custom code for Forminator API

Need help with the custom code for Forminator API.

  • Konstantinos Xenos
    • Rubber Duck Debugger

    Hi Daniel ,

    I've taken a look at the code snippet you provided us and the issue is with the $entry_meta array. The entry_meta table isn't populated is because the $entry_meta is missing it's parent array().

    The code you provided is as follows:

    $entry_meta =
    array(
    	'name' => 'answer-3',
    	'value' => 'Right'
    );

    And the correct code should be:

    $entry_meta = array(
    	array(
    		'name'  => 'answer-3',
    		'value' => 'Right'
    	),
    );

    Note that the each "answer" is an array within the _meta array(). Since this was missing Forminator couldn't properly read the answers given and populate the tables.

    The updated entry meta should be populating both tables and you will have the results as expected.

    As an extra note make sure that the add_poll() is run only once as each time it will create a new poll with a new ID as well so it will result in multiple polls with only 1 entry.

    Tell me if you need any further help with this !

    Regards,
    Konstantinos

  • Daniel
    • New Recruit

    Konstantinos,
    UPDATE: So when I manually create a post it will work great. When I do the feed import where it creates a post from the feed it will create a poll submission but will not show the result when I click the submission. Maybe the below code will help shed some light:

    Thanks for your follow up on this. I added the code you have above, when I look at the poll it says there was 1 submission, but when I click the submission link it shows there were no votes. I'm going to post all my code below to see if anything jumps out at you.

    function do_on_publish($id){
    	global $wpdb;
    	$rowcount_parent = $wpdb->get_var("SELECT post_parent FROM wp_posts WHERE post_id = $id");
    	//$rowcount_parent = $wpdb->num_rows;
    	//echo 'rowcount_parent = ' . $rowcount_parent;
    
    	if($rowcount_parent == 0){
    		$updated_id = $id;
    	}
    	else{
    		$updated_id = $rowcount_parent;/*$wpdb->get_var("SELECT post_parent FROM wp_posts WHERE post_id = $id");*/
    	}
    	echo 'updated id = ' . $updated_id . '<br>';
    
    	$wpdb->get_results("SELECT * FROM poll_post_link WHERE post_id = $updated_id");
        $rowcount = $wpdb->num_rows;
    	echo 'row count: ' . $rowcount . '<br>';
    	if($rowcount < 1){
    			  $poll_answers = array(
    				array(
    				  "title" => "Left",
    				  "element_id" => "answer-1"
    				),
    				array(
    				  "title" => "Neutral",
    				  "element_id" => "answer-2"
    				),
    				array(
    				  "title" => "Right",
    				  "element_id" => "answer-3"
    				),
    			  );
    			  $poll_settings = array(
    				"poll-question" => "How Would You Rate This Article?",
    				"results-behav" => "show_after",
    				"results-style" => "bar",
    				"enable-ajax" => true
    			  );
    			  $poll_id = Forminator_API::add_poll( "Political Bias", $poll_answers, $poll_settings );
    
    			echo ' poll id = ' . $poll_id . '<br>';
    
    			$wprss_ur_id = $wpdb -> get_var("SELECT meta_value FROM wp_postmeta WHERE post_id = $updated_id AND meta_key = 'wprss_ftp_feed_source'");
    			echo 'wprss_ftp_feed_source = ' . $wprss_ur_id . '<br>';
    			$wprss_url = $wpdb -> get_var("SELECT meta_value FROM wp_postmeta WHERE post_id = $wprss_ur_id AND meta_key = 'wprss_url'");
    			echo 'wprss url = ' . $wprss_url . '<br>';
    			$rss_lean = $wpdb -> get_var("SELECT initial_lean FROM rss_feed_list WHERE url = '$wprss_url'");
    			echo ' rss_lean = ' . $rss_lean . '<br>';
     			$guid = $wpdb -> get_var("SELECT guid FROM wp_posts WHERE ID = '$updated_id'");
    
    			if ($rss_lean == 'Left'){
    				echo 'Left';
    				$entry_meta = array(
    					array(
    							'name' => 'answer-1',
    							'value' => 'Left'
    						),
    					);
    
    				Forminator_API::add_poll_entry( $poll_id, $entry_meta );
    				echo ' the left works <br>';
    			}
    			else if ($rss_lean == 'Right'){
    				echo 'Right';
    				$entry_meta =array(
    					array(
    							'name' => 'answer-3',
    							'value' => 'Right'
    						),
    					);
    
    				$update_poll = Forminator_API::add_poll_entry( $poll_id, $entry_meta );
    				echo ' the right works <br> updated poll: ' . $update_poll . '<br>';
    			}
    			else if ($rss_lean == 'Neutral'){
    				echo 'Neutral';
    				$entry_meta = array(
    					array(
    							'name' => 'answer-2',
    							'value' => 'Neutral'
    						),
    					);
    
    				Forminator_API::add_poll_entry( $poll_id, $entry_meta );
    				echo ' the neutral works ';
    			}
    			else{
    				echo 'Error';
    				$entry_meta = array(
    						array(
    								'name' => 'Error',
    								'value' => 'Error'
    							)
    					);
    				Forminator_API::add_poll_entry( $poll_id, $entry_meta );
    			}
    
    			$poll_data = array(
    					'id' => NULL,
    					'poll_id' => $poll_id,
    					'post_id' => $updated_id,
    					'url' => $guid
    			);
    
    			//$pid = $wpdb -> get_var("SELECT poll_id FROM poll_post_link WHERE post_id = '$post_id'");
    			//echo 'poll id: ' . $poll_id . ' post_id: ' . $id . ' url: ' . $guid;
    
    		  $wpdb->insert('poll_post_link',$poll_data);
    
    		$test_data = array(
    					'id' => NULL,
    					'wprss_ur_id' => $wprss_ur_id,
    					'wprss_url' => $wprss_url,
    					'rss_lean' => $rss_lean
    			);
    		$wpdb->insert('test',$test_data);
    	}
    
    }
    add_action( 'publish_post', 'do_on_publish' );
  • Predrag Dubajic
    • Support

    Hi Daniel,

    Apologies for the long delay here, we were a bit short staffed for couple of days and we're doing our best to get everything back to normal.

    I have pinged Kostas again about this and hopefully he will be able to figure it out soon.
    We appreciate your patience so far.

    Best regards,
    Predrag

  • Konstantinos Xenos
    • Rubber Duck Debugger

    Hey Daniel ,

    I'm not entirely sure what you're trying to achieve here as I'm missing a lot of data from your tables to see where the issue might exactly be.

    As an example right at the start of your code you have

    $rowcount_parent = $wpdb->get_var("SELECT post_parent FROM wp_posts WHERE post_id = $id");

    that seems wrong as the WHERE is on post_id, on a default WP table it should be ID instead as post_id doesn't exist and it should already be producing an error on your debug.log. Except if you have added an extra column with an actual "post_id" name.

    Since I'm also missing the extra tables "rss_feed_list", "poll_post_link" I can't easily follow and debug the code at the moment to tell you where it could be going wrong exactly.

    Could you tell me if you're using an extra plugin or are those custom tables by you? Just so I can replicate and understand what's the concept here with all these extra $wpdb calls :slight_smile: .

    Regards,
    Konstantinos

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.