0

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.

0

The core functions needs to be called within the WordPress environment only. The cron.php is out of it thus resulting in that Call to undefined function error. Easiest way to the solution of your problem is to move the contents on that file in the mct_task() function.

4
  • The wp_insert_post is work in cron.php file (without wp_create_category function) and another WP functions is okay.
    – jozs2021
    Jul 31 at 9:27
  • I try the ~/wp-content/themes/mycustomtheme/functions.php file, the mct_task function, but the error: PHP Fatal error: Uncaught Error: Call to undefined function wp_create_category() in...
    – jozs2021
    Jul 31 at 10:13
  • I try the ~/wp-content/themes/mycustomtheme/functions.php file, in first line, but the error: There has been a critical error on this website. Please check your site admin email inbox for instructions. Learn more about troubleshooting WordPress.
    – jozs2021
    Jul 31 at 10:13
  • PHP Fatal error: Uncaught Error: Call to undefined function wp_insert_category() in...
    – jozs2021
    Jul 31 at 15:32

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.