Support

Account

Home Forums Front-end Issues Use acf_form frontend only, not in backend

Solved

Use acf_form frontend only, not in backend

  • Hi.
    I would like to use the acf_form() function on the frontend only on WooCommerce single products. When submitting the data, I handle the data my self and save it to WooCommerce cart. I don’t not want the custom fields to be attached to the single product and do not want to display the custom fields in the single product edit page in the backend.
    How should I locate these fields when setting up these fields?
    Is that possible?

  • Create the field group, location is irrelevant.

    Set “Active” no “No” this will prevent the group from being shown in the admin and this is why the locations settings will not matter.

    Call acf_form() using the group key, example:

    
    acf_form(array('field_groups' => array('group_5f21b0552953a')));
    
  • Unfortunately, this did not work. The acf_form() still saves the data to the product it is embeded on.
    How can I remove the “Solved” tag?

  • You will likely need to create your own pre save post filter to tell ACF where to save the values https://www.advancedcustomfields.com/resources/acf-pre_save_post/

    and also figure out how to prevent ACF from doing the save. The best way to do this is to unset $_POST[‘acf’] inside of your pre save post filter.

  • Thanks again John! So with your help I put this together and now it works like I want.

    add_filter( 'acf/pre_save_post' , function( $post_id ) {
    
    	// check if this is to be a new post
    	if ( $post_id != 'new' ) {
    		return $post_id;
    	}
    	
    	if ( isset( $_POST['add-to-cart'] ) && isset( $_POST['product_id'] ) ) {
    		unset( $_POST );
    	}
    
    	// return the new ID
    	return $post_id;
    
    }, 10, 1 );
    
Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘Use acf_form frontend only, not in backend’ is closed to new replies.