Is the only way to add taxonomy terms via an admin panel?

Is it possible to register taxonomy terms programmatically from a plugin? I would like to add a custom taxonomy ‘geographical area’ and also prepopulate the taxonomy with a list of areas in the UK.

public function sample_taxonomy() {
    // create a new taxonomy
    register_taxonomy(
        'resellers',
        'wps-reseller',
        array(
            'label' => __( 'Geographical Areas' ),
            'rewrite' => array( 'slug' => 'area' ),
            'capabilities' => array(
                'assign_terms' => 'edit_guides',
                'edit_terms' => 'publish_guides'
            )
        )
    );
}

Also, how would I run this code only once as part of plugin activation? I am using the WordPress plugin boilerplate activation functions.

Answer

I think you are looking for wp_insert_term().

An example from the Codex:

$parent_term = term_exists( 'fruits', 'product' ); // array is returned if taxonomy is given
$parent_term_id = $parent_term['term_id']; // get numeric term id
wp_insert_term(
  'Apple', // the term 
  'product', // the taxonomy
  array(
    'description'=> 'A yummy apple.',
    'slug' => 'apple',
    'parent'=> $parent_term_id
  )
);

Attribution
Source : Link , Question Author : codecowboy , Answer Author : s_ha_dum

Leave a Comment