I have a website with products (shoes), i have some shoes models registered as taxonomy on the function.php file like so:
function add_custom_taxonomies() {
register_taxonomy('mod-nike', 'pompitup_product', array(
// Hierarchical taxonomy (like categories)
'hierarchical' => false,
// This array of options controls the labels displayed in the WordPress Admin UI
'labels' => array(
'name' => _x( 'modeles Nike', 'taxonomy general name' ),
'menu_name' => __( 'Modeles Nike' ),
),
));
}
add_action( 'init', 'add_custom_taxonomies', 0 );
this is working as it should.
my problem is that i have a few custom taxonomies created this way but the user should be able to add more on the backend, with the help of an option page.
so i tried to hook on "acf/save_post" like so:
function my_acf_save_post( $post_id ){
//some logic that take have to do with custom post....
function register_brand_tax(){
$brand_to_add = get_field_object('field_52d6f9449e688', 'option');
//in the variable $brand_to_add I get the field object from the option pages
if( !empty($brand_to_add['value'])){
//then I instert the term on the "brand category" (this works when outisde of a function)
wp_insert_term(
$brand_to_add['value'], // the term
'marque' //the tax
);
register_taxonomy('mod-' . $brand_to_add['value'], 'pompitup_product',
array(
'hierarchical' => false,
'labels' => array(
'name' => _x( 'modeles ' . $brand_to_add['value'], 'taxonomy general name' ),
'menu_name' => __( 'Modeles ' . $brand_to_add['value'] ),
),
));
}
}
}
add_action( 'init', 'register_brand_tax', 20 );
add_action('acf/save_post', 'my_acf_save_post', 1);
the wp_insert_term is working but apparently the register_taxonomy must be called by an 'init' action that's why i tried to call it inside of the 'acf/save_post' but diden't had any luck so far, is there any way even outside of ACF to let my user register a new taxonomy from the admin backend?