1

I am wondering how should i insert the category programmatically and it should insert only 1 time after the theme is activated.

I know up to here.

function example_insert_category() {
    wp_insert_term(
        'Science',
        'category',
        array(
          'description' => 'This is an Science category.',
          'slug'        => 'science'
        )
    );
}
add_action( 'after_setup_theme', 'example_insert_category' );

i don't know how to insert children etc. I want to insert category structured like this:

Science
--Grade 11
----Science
----Management
----Humanities
--Grade 12
----Science
----Management
----Humanities

MIT
--Grade 15
----Science
----Management
----Humanities
--Grade 16
----Science
----Management
----Humanities

Is it possible to do programmatically?

Thanks

1 Answer 1

1

Keep track of the return value of wp_insert_term and use it to build your structure. A successful return value will be an array with a term_id key that you can pass into wp_insert_term's $args array as parent.

$parent = wp_insert_term('Science', 'category'); // I'll leave out `$args` here
if (is_wp_error($parent)) {
  // insert didn't work! return from your function or error or whatever
  return;
}

// grade 11 category has "Sience" as its parent
$grade11 = wp_insert_term('Grade 11', 'category', ['parent' => $parent['term_id']]);
if (is_wp_error($grade11)) {
  // same as above, something went wrong
  return;
}

// Management has the parent "Grade 11"
wp_insert_term('Management', 'category', ['parent' => $grade11['term_id']]);

As far as only inserting one time, you can hook into after_switch_theme and do your stuff. You'll probably want to look up your term first to make sure it hasn't been inserted, or set an option with the inserted term ID on success.

add_action('after_switch_theme', 'wpse206511_switch');
function wpse206511_switch()
{
    if (get_option('wpse206511_term_id')) {
        // already done, bail
        return;
    }

    // category insert code from above

    // set the term ID in an option that you can check later
    // if/when the theme gets activated/deactivated again
    add_option('wpse206511_term_id', $parent['term_id']);
}
6
  • Yes thanks. But suppose i have more category like Science should i repeat the code from begining?
    – stlawrance
    Commented Oct 25, 2015 at 14:37
  • Yep. You you just need to walk down the tree for each category. Start with the toplevel parent, insert next level parents, insert the child categories. Commented Oct 25, 2015 at 14:38
  • yea and in second part of code should i manually insert category id in option ?
    – stlawrance
    Commented Oct 25, 2015 at 14:40
  • If you have more than one category you can track each ID or just track a boolean value to mark that you inserted things. add_option('wpse206511_inserted_terms', true) for instance. Your call. Commented Oct 25, 2015 at 14:45
  • yes using your idea i did it . And successfully got inserted. But want to know is it good if we have large number of categories. I have even created a paste bin . Is it good to do like this? pastebin.com/rJ1QFgxF
    – stlawrance
    Commented Oct 25, 2015 at 15:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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