• Hello,

    I’m sorry, I speak a little English.

    My custom theme:

    ~/wp-content/themes/mycustomtheme/functions.php

    function mct_task() {
    	include( get_template_directory() . '/inc/cron.php' );
    }
    add_action( 'mct_hook', 'mct_task' );
    //
    function mytheme_setup_options() {
    	if( ! wp_next_scheduled( 'mct_hook' ) ) {
    		wp_schedule_event( time(), 'hourly', 'mct_hook' );
    	} else {}
    }
    add_action( 'after_switch_theme', 'mytheme_setup_options' );

    ~/wp-content/themes/mycustomtheme/inc/cron.php

    $category_id = wp_create_category( 'Super' );
    // $my_post = array( 'post_category'=> array( $category_id ) );
    wp_insert_post( $my_post );

    ~/error_log

    PHP Fatal error: Uncaught Error: Call to undefined function wp_create_category() in...

    Why? What is the problem?

    I would like use category at wp_insert_post with wp-cron. If the category exists, use the category ID, if not exists, create new category.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter otto2021

    (@otto2021)

    PHP Fatal error: Uncaught Error: Call to undefined function wp_insert_category() in...

    No comment. I was disappointed in WordPress.

    Thread Starter otto2021

    (@otto2021)

    Bad idea:

    wp_create_category();
    wp_insert_category();

    Good idea:

    wp_insert_term( 'Super', 'category' );

    Moderator bcworkz

    (@bcworkz)

    The so called “bad” are essentially wrappers of the “good”. They are all interrelated. Any of them should work for you.

    Typically when we get undefined function errors on standard WP functions, it’s because the WP environment was not correctly initiated for the request. cron.php needs to be included or required from your theme’s functions.php. Apparently something is directly requesting cron.php without initializing WP.

    The way to use wp-cron is to schedule an event, which defines a certain action hook that will fire on schedule. You add callback functions to this hook to get code to execute. There are examples in the user notes near the bottom of the docs page:
    https://developer.ww.wp.xz.cn/reference/functions/wp_schedule_event/

    From what I remember wp_create_category() has two arguments and both are required as this:
    wp_create_category(int/string $cat_name, int $parent)
    Kian William

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Call to undefined function wp_create_category()’ is closed to new replies.