How can I create parent category or child category using html and assign it to parent category if exists otherwise create new category as parent and assign it to my custom post type.

http://prntscr.com/gnwnic http://prntscr.com/gnwoc7

wp_create_category( $cat_name, $parent );

using function above getting fetal error

function create_cat_form_submit(){

$cat_name = $_POST['cat_name'];

if(!empty($cat_name) || $cat_name !== ''){

    wp_create_category( $cat_name, '' );


Fatal error: Uncaught Error: Call to undefined function wp_create_category() in E:\xampp\htdocs\post_view_counter\wp-content\plugins\tasks\tasks.php:146 Stack trace: #0 

Since wp_create_category() is part of the admin API you can only rely it's defined if is_admin() === true. Instead use wp_insert_term(). Check the examples in the codex.

  • thanks Zlatev, lovely answer, if you please provide me example as per my code situation, that will be great, if there is any hook involve please also mention that thanks – mohsin Sep 21 '17 at 13:33

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.