Support

Account

Home Forums Backend Issues (wp-admin) WP User frontend pro not saving ACF values to Database

Solving

WP User frontend pro not saving ACF values to Database

  • Hello,

    I am using WP User frontend pro plugin to accept guest posts from the frontend form that contains several custom fields. Everything works fine after submitting the frontend form and even when we check the post from wp-admin, it shows all the correct custom field values correctly.

    But on the frontend guest post, it’s not showing custom field values until I resave the submitted guest post from the backend. When talked to ACF support, they said that the wp user frontend pro plugin populates the custom fields but does not save them to the database. They also said to write a custom function that triggers after WP user frontend submits/updates post from frontend and run save_post() ACF function.

    After discussing with the WP User frontend team, they gave the following functions triggers after post insert/update and can be used for custom actions –

    – wpuf_add_post_after_insert
    – wpuf_add_post_after_update

    Ref- https://wedevs.com/docs/wp-user-frontend-pro/developer-docs/wpuf_add_post_after_insert/

    So I wrote the following code –

    
    function save_to_database($post_id,$form_id){
    if($form_id == "195"){
    do_action( 'acf/save_post', $post_id );
    }
    }
    add_action( 'wpuf_add_post_after_insert', 'save_to_database' , 10 ,2 );
    add_action( 'wpuf_edit_post_after_update', 'save_to_database' , 10 , 2 );
    

    But this dint work. Can anyone help me correct the above code? Or provide me a better solution?

  • I hate to contradict ACF support, but it sounds like the field values are saved but the field key references for ACF are not being saved.

    I will need more information.

    How are you including the ACF fields in the form for the other plugin?

  • I do not have the plugin you mention so there isn’t any way I can see what that plugin is doing.

    At one time ACF would show fields if the same meta_key (i.e. field name) was used. This is no longer the case and it was changed not long ago for security reasons. ACF also requires a field key reference to be saved. The field key reference is recorded in the DB as "_{$field_name)". Without this value ACF will not show your field on the front end, but it will find and show the field value in the admin when editing.

    The reason that the function you added is not doing anything is because ACF is looking for an array in $_POST[‘acf’] made up of field_key => value pairs. In the case of the other plugin, the form input does not include this array.

    The solution is to create a action as suggested but instead of

    
    do_action( 'acf/save_post', $post_id );
    

    you will have to get each of the fields using get_post_meta() and then update each field using

    
    update_field($field_key, $value, $post_id);
    

    replacing $field_key with the correct field key that corresponds to each field name that you want to update.

  • Hello,

    I tried the following code but it dint work out.

    
    function perform_save_post($post_id , $form_id){
    	if($form_id == "195"){
    	$get_meta = get_post_meta($post_id, "acf-field_609ec6987bb8d");
    	update_field('acf-field_609ec6987bb8d', $get_meta, $post_id);
    	
    
    }
    }
    add_action( 'wpuf_add_post_after_insert', 'perform_save_post',10, 2 );
    add_action( 'wpuf_edit_post_after_update', 'perform_save_post',10,2 );
    

    Where did I go wrong?

  • You need to use the field name in get_post_meta() and the field key in update_field()

  • Hello,

    Thank you for your response.

    I tested the code on debug page and it successfully update the normal text field value. But this field which I am updating here is checkboxes.

    So the output of get_meta_data is a format like checked_val1 | checked_val2 | ..

    How do I use update_field on such data and update checked boxes??

    Thanks!

  • Does user front end save these values in the DB as multiple entries in the postmeta table or as a serialized array in a single db entry?

  • Just pass the value back in the same way. ACF expects an array of selected values. Would have been more complicated only if the other plugin added multiple db entries.

  • Hello,

    I tried the following code –

    
    
    $get_meta = get_post_meta('32', "payment_options", true);
    print_r($get_meta);
    echo "<br>";
    
    if(update_field('payment_options', $get_meta, '32')){
    	echo "Success";
    }
    else{
    	
    	echo "FAIL";
    }
    

    And the output is –

    Array ( [0] => paypal [1] => credit-card [2] => stripe )
    FAIL

    🙁

  • Look in the db and see if the meta key of “_payment_options” was created and if the value of this is the correct field key?

  • what is returned is not what you think it is in this case

    https://developer.wordpress.org/reference/functions/update_post_meta/

    Return

    int|bool Meta ID if the key didn’t exist, true on successful update, false on failure or if the value passed to the function is the same as the one that is already in the database.

    Since you are passing the same value that is already set it is returning false. This is what ACF returns. The purpose of what you are doing is to force ACF to set the field key reference.

  • Exactly! We are just trying to resave already saved values so they also get stored in the database. So what function shall we use here?

  • 
    
    $get_meta = get_post_meta('32', "payment_options", true);
    print_r($get_meta);
    echo "<br>";
    
    if(update_post_meta('32','acf-field_609ec6987bb8d',$get_meta)){
    	echo "Success";
    }
    else{
    	
    	echo "FAIL";
    }
    
    

    This again dint work :/

  • the first code you posted is probably working. The only way to know for sure is to look in the DB to see if the field key reference meta key was added.

    
    $get_meta = get_post_meta('32', "payment_options", true);
    print_r($get_meta);
    echo "<br>";
    
    // update field will always return false 
    // if the value used to update matches the value
    // in the database. 
    // The if will always be false even if the update succeeds.
    
    if(update_field('payment_options', $get_meta, '32')){
    	echo "Success";
    }
    else{
    	
    	echo "FAIL";
    }
    
Viewing 16 posts - 1 through 16 (of 16 total)

You must be logged in to reply to this topic.