Support

Account

Home Forums ACF PRO Add field to add_term lightbox in taxonomy field ? Reply To: Add field to add_term lightbox in taxonomy field ?

  • I found a solution, but I’m sure we can do it cleaner.

    In My theme:

    ## First I add somes helpers

    
    function zk_acf_get_field_groups_by_tax($taxonomy){
        $groups = acf_get_field_groups();
        $filtered_groups = [];
        foreach($groups as $key => $group):
            foreach($group['location'] as $location):
                foreach($location as $location_item){
                    if(
                        $location_item['param'] == 'taxonomy' && 
                        $location_item['operator'] == '==' &&
                        $location_item['value'] == $taxonomy
                    ){
                        $filtered_groups[] = $group;
                    }
                }
            endforeach;
        endforeach;
        return $filtered_groups;
    }
    
    function zk_acf_get_fields_by_tax($taxonomy){
        $groups = zk_acf_get_field_groups_by_tax($taxonomy);
        $fields = [];
        foreach($groups as $group){
            $group_fields = acf_get_fields($group['key']);
            if(is_array($group_fields)){
                $fields  = array_merge($fields, $group_fields);
            }
        }
        return $fields;
    }
    

    ## Then I copy class-acf-field-taxonomy.php in my theme and modify ajax_add_term function

    
    function ajax_add_term() {
    		// vars
    		$args = wp_parse_args($_POST, array(
    			'nonce'				=> '',
    			'field_key'			=> '',
    			'term_name'			=> '',
    			'term_parent'		=> ''
    		));
    		
    		// verify nonce
    		if( !acf_verify_ajax() ) {
    			die();
    		}		
    		
    		// load field
    		$field = acf_get_field( $args['field_key'] );
    		if( !$field ) {
    			die();
    		}
    		
    		// vars
    		$taxonomy_obj = get_taxonomy($field['taxonomy']);
    		$taxonomy_label = $taxonomy_obj->labels->singular_name;
    		$custom_fields = zk_acf_get_fields_by_tax($field['taxonomy']);
    
    		// validate cap
    		// note: this situation should never occur due to condition of the add new button
    		if( !current_user_can( $taxonomy_obj->cap->manage_terms) ) {
    			wp_send_json_error(array(
    				'error'	=> sprintf( __('User unable to add new %s', 'acf'), $taxonomy_label )
    			));
    		}
    		
    		// save?
    		if( $args['term_name'] ) {
    			
    			// exists
    			if( term_exists($args['term_name'], $field['taxonomy'], $args['term_parent']) ) {
    				wp_send_json_error(array(
    					'error'	=> sprintf( __('%s already exists', 'acf'), $taxonomy_label )
    				));
    			}
    			
    			// vars
    			$extra = array();
    			if( $args['term_parent'] ) {
    				$extra['parent'] = (int) $args['term_parent'];
    			}
    			
    			// insert
    			$data = wp_insert_term( $args['term_name'], $field['taxonomy'], $extra );
                
                
                
                
    			// error
    			if( is_wp_error($data) ) {
    				wp_send_json_error(array(
    					'error'	=> $data->get_error_message()
    				));
    			}
    			
    			// load term
    			$term = get_term($data['term_id']);
    			foreach($custom_fields as $custom_field){
                    $value = $_POST[$custom_field['name']];
                    if($value){
                        update_field($custom_field['name'], $value, $term);
                    }
                }
                
    			// prepend ancenstors count to term name
    			$prefix = '';
    			$ancestors = get_ancestors( $term->term_id, $term->taxonomy );
    			if( !empty($ancestors) ) {
    				$prefix = str_repeat('- ', count($ancestors));
    			}
    		
    			// success
    			wp_send_json_success(array(
    				'message'		=> sprintf( __('%s added', 'acf'), $taxonomy_label ),
    				'term_id'		=> $term->term_id,
    				'term_name'		=> $term->name,
    				'term_label'	=> $prefix . $term->name,
    				'term_parent'	=> $term->parent
    			));
    				
    		}
    		
    		?><form method="post"><?php
    		
    		acf_render_field_wrap(array(
    			'label'			=> __('Name', 'acf'),
    			'name'			=> 'term_name',
    			'type'			=> 'text'
            ));
            
            foreach($custom_fields as $field){
               acf_render_field_wrap($field); 
                ?>
                    <script>
                        acf.add_filter('prepare_for_ajax', function (data) {
                            data['<?php echo $field['name']; ?>'] = jQuery('#acf-<?php echo $field['key']; ?>').val();
                            console.log('data',data);
                            return data;
                        });
                    </script>
                <?php
            }
           
    
    		if( is_taxonomy_hierarchical( $field['taxonomy'] ) ) {
    			
    			$choices = array();
    			$response = $this->get_ajax_query($args);
    			
    			if( $response ) {
    				
    				foreach( $response['results'] as $v ) { 
    					
    					$choices[ $v['id'] ] = $v['text'];
    					
    				}
    				
    			}
    			
    			acf_render_field_wrap(array(
    				'label'			=> __('Parent', 'acf'),
    				'name'			=> 'term_parent',
    				'type'			=> 'select',
    				'allow_null'	=> 1,
    				'ui'			=> 0,
    				'choices'		=> $choices
    			));
    			
    		}
    		
    		
    		?><p class="acf-submit">
    			<button class="acf-submit-button button button-primary" type="submit"><?php _e("Add", 'acf'); ?></button>
    		</p>
    		</form><?php
    		
    		
    		// die
    		die;	
    		
    	}