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.
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 3 years, 1 month ago by
digiblogger.
-
This reply was modified 3 years, 1 month ago by
digiblogger.
Is there no way to check the form ID before the post creation process is started?