Support

Account

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

Solved

register a taxonomy from an option page

  • Hi,
    I am using the Options page plugin to create on option page with a text field on it.
    when the field is loaded i use “acf/load_field” to reset the value to nothing.
    but when the value is saved i would like to use it to create a new taxonomy.

    i tried using ‘acf/save_post’ checking if the field value is not empty.
    and then use register_taxonomy. but apparently it should be called with the init action, so i tried calling the init action inside of the save_post action.

    any ideas on that isue?

    thanks

  • Hi @l.pirondini

    The register_taxonomy function should be placed inside your functions.php file and should run on every page load.

    I can understand what you have coded, but it won’t work with ACF.
    You will need to remove the acf/load_field filter, and the acf/save_post action code and change your logic to do this.

    1. Create a repeater field on the options pages with a text field for the taxonomy name
    2. Enter some data into this field on the options page
    3. Hook into the init action in your functions.php file and load the options page repeater field values.
    4. Loop through these values and run the register_taxonomy on each one

    Thanks
    E

  • 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;
    }
    	
    
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘register a taxonomy from an option page’ is closed to new replies.