The problem is that you’re only hooking the cron action on admin_init
, which doesn’t run when wp-cron.php
is called. So the function will never run.
So this function shouldn’t be inside wpcp_activate()
:
add_action( 'wpcp_cron_hook', 'wpcp_cron_do' );
Also, register_activation_hook()
shouldn’t be inside the admin_init
hook either.
So your code should look like this:
function wpcp_make_post( $post_title, $post_content, $post_type ) {
$post_id = 0;
if ( post_type_exists( $post_type ) ) :
wp_set_current_user( 1 );
$to_post = array(
'post_type' => $post_type,
'post_status' => 'draft',
'post_author' => 1,
'post_title' => $post_title,
'post_content' => $post_content,
);
$post_id = wp_insert_post( $to_post );
endif;
return $post_id;
}
function wpcp_cron_activation() {
if ( ! wp_next_scheduled( 'wpcp_cron_hook' ) ) :
wp_schedule_event( time() , 'hourly', 'wpcp_cron_hook' ); // = daily
endif;
}
register_activation_hook( __FILE__, 'wpcp_cron_activation' );
function wpcp_cron_do() {
$post_title="This should be the draft title";
$post_content="All HERE";
wpcp_make_post( $post_title, $post_content, 'post' );
}
add_action( 'wpcp_cron_hook', 'wpcp_cron_do' );