• Hey friends,

    I created a plugin, that adds the ability to save form data to a custom post type. Looks like this:

    
    <?php
    
    function save_posted_data( $posted_data ) {
    	
    $form_id = $contact_form->id();
    if( $form_id == 1903 ) {
    
           $args = array(
             'post_type' => 'np-food',
             'post_status'=>'draft',
             'post_title'=>$posted_data['food-name'],
             'post_content'=>$posted_data['food-desc'],
           );
           $post_id = wp_insert_post($args);
    
           if(!is_wp_error($post_id)){
            if( isset($posted_data['food-name']) ){
            update_post_meta($post_id, 'food-name', $posted_data['food-name']);
            }
    // and so on
    return $posted_data;
    	   }
    	}  
     }
    add_filter( 'wpcf7_posted_data', 'save_posted_data' );
    
    ?>
    

    That works pretty good when using the form ID 1903. But I when I use any other form, sending the form via submit button ends up in a loop.

    How can I force CF7 only to save post data when a specific form is used and just react as normal, when another ID is used?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Just looking quickly, the only time you’re returning the post_data array is AFTER SUBMISSION IF NO ERROR.

    Without actually testing this, it could be as simple as moving the “return” line to do so every time your function is called, regardless:

    function save_posted_data( $posted_data ) {
    	
    	$form_id = $contact_form->id();
    	if( $form_id == 1903 ) {
    
    		$args = array(
    			'post_type' => 'np-food',
    			'post_status'=>'draft',
    			'post_title'=>$posted_data['food-name'],
    			'post_content'=>$posted_data['food-desc'],
    		);
    		$post_id = wp_insert_post($args);
    
    		if(!is_wp_error($post_id)){
    			if( isset($posted_data['food-name']) ){
    				update_post_meta($post_id, 'food-name', $posted_data['food-name']);
    			}
    			// and so on
    		}
    	}
    	return $posted_data;
    }
    add_filter( 'wpcf7_posted_data', 'save_posted_data' );

    Just had another thought, and probably a better way to do what you’re trying to.

    Only add your filter when you need it to execute. The default will execute the default way and only execute your action/function when it’s actually needed:

    function save_posted_data( $posted_data ) {
    	
    	$args = array(
    		'post_type' => 'np-food',
    		'post_status'=>'draft',
    		'post_title'=>$posted_data['food-name'],
    		'post_content'=>$posted_data['food-desc'],
    	);
    	$post_id = wp_insert_post($args);
    
    	if(!is_wp_error($post_id)){
    		if( isset($posted_data['food-name']) ){
    			update_post_meta($post_id, 'food-name', $posted_data['food-name']);
    		}
    		// and so on
    		return $posted_data;
    	}
    }
    
    $form_id = $contact_form->id();
    if( $form_id == 1903 ) {
    	add_filter( 'wpcf7_posted_data', 'save_posted_data' );
    }

    In the example above, I put the return line back in the save_posted_data function, but you may still want to move it outside of the “if no error” statement block. Depends on the behavior you’re looking for if there is/isn’t an error.

    Ignore my second reply…..I’ve actually tried code and it’s a no-go. I can’t figure out how to get the current submitted form ID outside of the perscribed hooks. Looks like the first option is your best bet.

    Thread Starter digiblogger

    (@digiblogger)

    Thank you very much.

    But Example 1) does not seem to work too.

    There is still the loop when submitting the form.

    Current Code:

    
    function save_posted_data( $posted_data ) {
    	
    	$form_id = $contact_form->id();
    	if( $form_id == 1903 ) {
    
    		$args = array(
    			'post_type' => 'np-food',
    			'post_status'=>'draft',
    			'post_title'=>$posted_data['food-name'],
    			'post_content'=>$posted_data['food-desc'],
    		);
    		$post_id = wp_insert_post($args);
    
    		if(!is_wp_error($post_id)){
    			if( isset($posted_data['food-name']) ){
    				update_post_meta($post_id, 'food-name', $posted_data['food-name']);
    			}
    			// and so on
    		}
    	}
    	return $posted_data;
    }
    add_filter( 'wpcf7_posted_data', 'save_posted_data' );
    • This reply was modified 4 years, 9 months ago by digiblogger.
    • This reply was modified 4 years, 9 months ago by digiblogger.
    Thread Starter digiblogger

    (@digiblogger)

    Is there no way to check the form ID before the post creation process is started?

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘CF/7 to custom post, but only for specific form’ is closed to new replies.