我正在处理一个插件并希望添加一个cron,所以我添加了以下代码,但它根本没有执行,请检查并在需要时为我更正。如果代码正确,那我该如何调试呢? 注意:acp海报是在文件的早期定义的。
add_action( 'wp_loaded', 'prefix_setup_schedule' );
function prefix_setup_schedule($record) {
if ( ! wp_next_scheduled( 'prefix_hourly_event' ) ) {
wp_schedule_event( time(), 'hourly', 'prefix_hourly_event',$record);
}
}
add_action( 'prefix_hourly_event', 'ACP_poster' );
这里是ACP_poster函数:
function ACP_poster($n){
global $wpdb,$api_key;
$row = $wpdb->get_results('SELECT a,b,c FROM tbname ORDER BY id DESC LIMIT 1',ARRAY_A);
$url = 'https://example.com';
$headers = array( 'Authorization' => $api_key );
$request = new WP_Http;
$result = $request->request( $url , array( 'method' => 'GET', 'headers' => $headers, 'sslverify' => false ) );
$data = new SimpleXMLElement($result['body']);
$attributes = $data->p->attributes();
foreach ($data->p[0] as $p)
{
// Sanitize data.
$pr = number_format((float)$p->pr, 2, '.', ' ');
$image = '<a href="'.$p->{'url2'}.'"><img src="'.$p->{'url3'} .'" style="float: right"/></a>';
$pd = $image.$p->description .'-- ='.$p->currency.' '.
' ,Retail Price ='.$p->{'retail-price'}.' , '.
$p->{'sale-price'}.'<a href="'.$p->{'url2'}.
'">...</a>';
$v = $c[$no]->child;
$ids = get_term_by('slug', $v, 'category');//wordpress function
$id = (int)$ids->term_taxonomy_id;
$p2 = array('post_title' => $p->name,
'post_content' => $pd,
'post_status' => 'publish',
'post_author' => 1,
'post_category' =>array($id));
$pr = wp_insert_post( $p2, $wp_error );
wp_reset_query(); // Restore global post data stomped by the_post().
}
}
}