Support

Account

Home Forums ACF PRO Save ACF repeater group injected into existing WP Job Manager submit job form

Solved

Save ACF repeater group injected into existing WP Job Manager submit job form

  • I’m injecting and ACF Pro field group into an existing WP Job Manager job submission form:

    
    /**
     * Inject ACF fields into route submission form.
     * This example uses the WP Job Manager Field Editor hook 
     * field to fire job_manager_field_actionhook_prefix_acf_repeater
     * 
     * @param array $field settings for field
     * @param string $key field key 
     */
    add_action( 'job_manager_field_actionhook_prefix_acf_repeater', 'prefix_acf_repeater', 10, 2 );
    function prefix_acf_repeater( $field, $key ) {
    	// We only want to add this on the front end. 
    	// The back end does not need custom display and saving.
    	if ( is_admin() ) {
    		return;
    	}
    
    	 // Insert ACF group field
    	 acf_form( [
    		 'new_post' => true,
    		 'post_id' => 'new_post', // Prevent old form values from being persistent
    		 'fields'=> [ 'field_5c9bf22960ed8'],
    		'return' => '', // No return value for WPJM compat. // https://support.advancedcustomfields.com/forums/users/jamesryder/replies/
    		'form' => false,
    		'honeypot' => false,
    	] );
    }
    

    I am not calling acf_form_head() because it conflicts with the WP Job Manager submission process. I am however, enqueueing acf_enqueue_scripts() so that the JS and CSS are loaded.

    
    add_action( 'init', 'prefix_acf_enqueue_scripts_manually', 1 );
    function prefix_acf_enqueue_scripts_manually() {
    	acf_enqueue_scripts();
    }
    

    I’ve created a callback function for save_post_job_listing (code at the end of this post) which allows me to look at $_POST and save my repeater fields, but this required building a complex custom save function which will break if the field structure is modified.

    Does ACF have a built in function that will allow me to manually trigger the saving of this group field? Or alternatively a more straight forward way of getting ACF and WPJM to work together?

    Here’s the custom save function:

    
    add_action( 'save_post_job_listing', 'prefix_save_group');
    function prefix_save_group( $post_id ) {
    	$fields = [];
    
    	$fields['days'] = [
    		'id'   => 'field_5c9bf22960ed8',
    		'name' => '_tsr_route_days'
    	];
    
    	$fields['stops'] = [
    		'id'   => 'field_5c9bf22962649',
    		'name' => 'stops'
    	];
    
    	$fields['stop_name'] = [
    		'id'   => 'field_5c9bf229635e9',
    		'name' => 'stop_name'
    	];
    
    	$fields['day_notes'] = [
    		'id'   => 'field_5c9bf25a6c3e0',
    		'name' => 'day_notes'
    	];	
    	
    	// Get array of days data from $_POST
    	$days_data = isset( $_POST['acf'][ $fields['days']['id'] ] )
    		? $_POST['acf'][ $fields['days']['id'] ]
    		: null;
    
    	// Bail if we have no data.	
    	if ( null === $days_data ) {
    		return;
    	}
    
    	// Day repeater (stores number of rows)
    	$days_data_count = count( $days_data );
    	$meta_key = $fields['days']['name'];
    	update_post_meta( $post_id, '_' . $meta_key, $fields['days']['id'] );
    	update_post_meta( $post_id, $meta_key,       $days_data_count );
    	
    	// Loop through days data and save it.
    	$day_counter = 0;
    	// Days
    	foreach ( $days_data as $day ) {
    		// Day
    		foreach ( $day as $day_field => $day_field_value ) {
    			// Day Stops
    			if ( $day_field === $fields['stops']['id'] ) {
    				// Number of stops
    				$stops = $day_field_value;
    				$stops_count = count( $stops );
    
    				$meta_key = $fields['days']['name'] . '_' .
    										$day_counter . '_' .
    										$fields['stops']['name'];
    
    				update_post_meta( $post_id, '_' . $meta_key, $day_field );
    				update_post_meta( $post_id, $meta_key,       $stops_count );
    				
    				$stop_counter = 0;
    				// Stop fields
    				foreach ( $stops as $stop ) {
    					// Stop field
    					foreach ( $stop as $stop_field => $stop_value ) {
    						// Stop Name
    						if ( $stop_field === $fields['stop_name']['id'] ) {
    
    							$meta_key = $fields['days']['name']  . '_' .
    													$day_counter . '_' .
    													$fields['stops']['name'] . '_' .
    													$stop_counter . '_' .
    													$fields['stop_name']['name'];
    
    							update_post_meta( $post_id, '_' . $meta_key, $stop_field );
    							update_post_meta( $post_id, $meta_key,       $stop_value );
    						}
    
    						// Stop Notes
    						if ( $stop_field === $fields['stop_notes']['id'] ) {
    
    							$meta_key = $fields['days']['name']  . '_' .
    													$day_counter . '_' .
    													$fields['stops']['name'] . '_' .
    													$stop_counter . '_' .
    													$fields['stop_notes']['name'];
    
    							update_post_meta( $post_id, '_' . $meta_key, $stop_field );
    							update_post_meta( $post_id, $meta_key,       $stop_value );
    						}
    					}
    					$stop_counter++;
    				} // Day stop
    			} // Day stops
    			
    			// Day Notes
    			if ( $day_field === $fields['day_notes']['id'] ) {
    
    				$meta_key = $fields['days']['name'] . '_' .
    										$day_counter . '_' .
    										$fields['day_notes']['name'];
    										
    				update_post_meta( $post_id, '_' . $meta_key, $day_field );
    				update_post_meta( $post_id, $meta_key,       $day_field_value );
    			}	// Day notes		
    		} // Day
    
    		$day_counter++;
    	} // Days
    }
    
  • Just following up…

    I figured out that triggering do_action( 'acf/save_post', $post_id ); is the key. This means that creating a custom save function is not necessary.

    
    add_filter( 'job_manager_update_job_data', 'my_acf_save_repeater_data', 10, 2 );
    function my_acf_save_repeater_data( $post_id, $route_data ) {
    	do_action( 'acf/save_post', $post_id );
    
    	return $route_data;
    }
    
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Save ACF repeater group injected into existing WP Job Manager submit job form’ is closed to new replies.