0

I'm writing a plugin for adding a Custom Post called"Dog". The core of the plugin is strictly based on this one https://github.com/RescueThemes/Rescue-Animal-Custom-Posts

I prefer to add predefined sexes after the plugin activation, I would like to add "Male", "Female", "Unapplicable" from the start.

So, based on other resources I wrote that in the same .php plugin file

Consider that I changed the taxonomy name from "Genders" to "Sexes" from the original plugin.

add_action( 'init', 'create_dog_taxonomies', 0 );

function create_dog_taxonomies() {

// Sex taxonomy
    $labels = array( 
    'name' => _x( 'Sexes', 'shelter-dogs' ),
    //etc.
);

    $args = array( 
        'labels' => $labels,
        'public' => true,
        'show_in_nav_menus' => true,
        'show_ui' => true,
        'show_tagcloud' => true,
        'hierarchical' => false,
        'rewrite' => true,
        'query_var' => true
    );

    register_taxonomy( 'dog_sex', array('shelter_dogs'), $args );
}

add_action('init', 'add_sexes', 100);

function add_sexes()
{
    wp_insert_term(
      'Male', // the term 
      'Sexes', // the taxonomy
      array(
        'slug' => 'Male'
      )
    );
}

I get the Sexes taxonomy, but no predefined values. Where's the problem?

3

Maybe try wp_insert_term once the plugin has been enabled: https://codex.wordpress.org/Function_Reference/wp_insert_term

I.e.

wp_insert_term(
  'Male', // the term 
  'dog_sex', // the taxonomy
  array(
    'slug' => 'male',
  )
);
  • where should I put it? – lucgenti Jun 8 '16 at 21:29
  • 1
    It needs to be called after the taxonomy has been registered (and only called once). Also your taxonomy in the example above is actually dog_sex therefore in my example sexes needs to be updated to dog_sex – emily Jun 8 '16 at 21:45
  • Oh my god. That was the problem: I shouldn't put the name label, but the actual string I use for registering the taxonomy, dog_sex. Thank you, now it adds terms successfully! What's the way you suggest to run it once but integrate it in a good fashion inside a plugin? Via the plugin_activation hook? – lucgenti Jun 9 '16 at 14:51
  • Seems like I have another problem: terms are in the database and are visible from the Taxonomy menu "Sex", but they're not visible on the metabox that appears when adding a custom post. But that happens only if the taxonomy is not hierarchical. – lucgenti Jun 9 '16 at 15:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.