我正在创建一个wordpress插件,它从我的一个wordpress站点中获取一篇文章,然后将其发布到不同的wordpress站点。出于测试目的,我希望它随机抽取一篇文章,然后每隔30秒抽取一篇文章来创建一篇新文章。
如果没有计划的事件,下面的代码可以正常运行。换句话说,如果我将“create\\u blog\\u post”替换为“activate”,它将能够检测到其他博客帖子,并将创建一个帖子调用“this blog article ready exists”。然而,当我把它放在30秒计时器上时,什么都没有发生。
我尝试了其他方法,如按标题获取页面,并将与计划的事件一起运行。然而,它并没有检测到任何博客帖子,只有页面。
//activation of plugin
register_activation_hook( __FILE__, \'activate\' );
//deactivation of plugin
register_deactivation_hook( __FILE__, \'deactivate\' );
function bgs_add_cron_recurrence_interval( $schedules ) {
$schedules[\'every_30_seconds\'] = array(
\'interval\' => 30,
\'display\' => __( \'Every 30 Seconds\', \'textdomain\' )
);
return $schedules;
}
add_filter( \'cron_schedules\', \'bgs_add_cron_recurrence_interval\' );
function activate() {
if ( ! wp_next_scheduled( \'30_second_action\' ) ) {
wp_schedule_event( time(), \'every_30_seconds\', \'30_second_action\' );
}
}
add_action(\'30_second_action\', \'create_blog_post\');
function create_blog_post(){
$jsondata = file_get_contents(\'http://blog.hansenlighting.com/wp-json/wp/v2/posts?per_page=20\'); //pulls json data from hansenlighting blog
$json = json_decode($jsondata,true); //turns json data into something that php can understand
$ranNum = rand(1,10); //generates random number between 1-10
$blogTitle = $json[$ranNum][title][rendered];
$blogContent = $json[$ranNum][content][rendered];
global $wpdb;
if ( post_exists($blogTitle) ) {
$page = array( //creates post content
\'post_title\' => \'this blog article already exists\',
\'post_content\' => \'this is a test post\',
\'post_status\' => \'publish\',
\'post_author\' => 1,
\'post_type\' => \'post\',
);
wp_insert_post( $page ); //creates post
} else {
$page = array( //creates post content
\'post_title\' => $blogTitle,
\'post_content\' => $blogContent,
\'post_status\' => \'publish\',
\'post_author\' => 1,
\'post_type\' => \'post\',
);
wp_insert_post( $page ); //creates post
}
}
function deactivate(){
//wp_clear_scheduled_hook(\'my_hourly_event\');
flush_rewrite_rules(); //may not need this since its not a custom post type
}
function uninstall(){
}