Support

Account

Home Forums Add-ons Options Page register a taxonomy from an option page Reply To: register a taxonomy from an option page

  • Hi, I changed a bit my taxonomy architecture.
    The website i am working on sales shoes so I made a “Brand” category with “Nike, Adidas and so on…” the shoes models “air max, air force…” are saved as a children categories of the brand they belong to.

    That way i no longer need to register_taxonomy if i want to add a new brand from my option page, i just have to insert a new term inside on the root of my “Brand” category.

    Here is how i did it:

    // run AFTER ACF saves the $_POST['fields'] data
    add_action('acf/save_post', 'my_acf_save_option_page', 20);	
    
    function my_acf_save_option_page( $post_id ){
        
        //getting the value of my text box
        $brand_to_add = get_field_object('field_52d6f9449e688', 'option');
         
        //Checking if the value is not empty (this also check if it exists 
        //so when i am saving a post and not the option page the 
        //wp_insert_term is not called
        if( !empty($brand_to_add['value'])){ 
            //inserting the desired value inside the "marque" category 
            //(marque = brand in french)
            wp_insert_term( $brand_to_add['value'], 'marque' );
    
        }//end if statement 
    }

    also when the option page is loaded the text field value is set to an empty string so we can add a new category

    
    // Reset new brand field
    add_filter('acf/load_field/key=field_52d6f9449e688', 'reset_new_brand');	
    
    function reset_new_brand( $field ){
        // reset choices
        $field['value'] = '';
        
        // Important: return the field
        return $field;
    }