I’ve been trying to figure out why my plugin stopped working and have yet to figure out why.. Basically the plugin is supposed to create a Cron job through wordpress’ scheduled events hook and then weekly parse through a json feed to update the posts. When I initially set it up it was working wonderfully, and now it seems to not even work when I activate the plugin, which it should do at the very least. The activation and de-activation of the cron job seems to work fine.
This code snippet creates the weekly cron activity functionality.
add_filter( 'cron_schedules', 'cron_add_weekly' );
function cron_add_weekly( $schedules ) {
// Adds once weekly to the existing schedules.
$schedules['weekly'] = array(
'interval' => 604800,
'display' => __( 'Once Weekly' )
);
return $schedules;
}
The below code creates the cron job on activation, which is working.
register_activation_hook( __FILE__, 'startupx_activation' );
function startupx_activation() {
wp_schedule_event( time(), 'weekly', 'startupx_weekly_event_hook' );
}
add_action( 'wp', 'startupx_setup_schedule' );
function startupx_setup_schedule() {
if ( ! wp_next_scheduled( 'startupx_weekly_event_hook' ) ) {
wp_schedule_event( time(), 'weekly', 'startupx_weekly_event_hook' );
}
}
I then have created a function that does all the parsing/updating all the posts and inserting any new ones. I do the update in the event that the feed has changed for a particular episode. I figure it’s easier to just update it rather than do a check to see if it needs to be updated or not. Below is a little bit of that code.
add_action( 'startupx_weekly_event_hook', 'insert_episodes' );
function insert_episodes() {
//load the data from the RSS
$feed = "http://www.seanzulu.com/startupx?format=json";
$json = json_decode(curl_get_contents($feed)); //curl_get_contents calls another function to get the feed data via cURL
$episode_list = $json->items; //set where the episode data lives in the object for easier use
//parse through each episode for data
foreach($episode_list as $episode) {
//set the post array to have the necessary data per http://codex.wordpress.org/Function_Reference/wp_insert_post
if(get_page_by_title($episode->title,OBJECT,'post') != NULL) {
//update post
wp_update_post($post);
} else {
//insert post
$dateCreated = date('Y-m-d H:i:s', $epoch/1000);
$post['post_date'] = $dateCreated; //only set the date on creation
wp_insert_post($post);
}
}
}
I have removed some of the code as I didn’t think it was necessary to the problem that I’m having. I can add it in if need be. It was basically just to parse through the data and set it so that the $post
had the right values matching to the indexes per the codex. After this function I have a function that deactivates the weekly even schedule hook.
register_deactivation_hook( __FILE__, 'startupx_deactivation' );
function startupx_deactivation() {
wp_clear_scheduled_hook( 'startupx_weekly_event_hook' );
}
Like I said I’m not entirely sure why it’s breaking and what’s causing it to not be able to run the insert_episodes()
function when even activating the plugin. I’m guessing it’s something to do with how the hook is setup, but it was working before and without me changing the code at all it broke. I looked at the documentation for the different functions to see if maybe something had changed there, but I didn’t find anything useful. I’m at a loss of what I should try next so any insight on what I might be able to do to allow it to run would be greatly appreciated. If you see areas that could be better optimized by all means let me know! If you have questions let me know and I’ll be more than happy to answer. Thanks in advance for any and all help! If you want me to upload more code then let me know!
Update 1:
I think it may have been because I had an undeclared variable in $epoch
. I am unsure if this has fixed the issue or if it’s just fixing a different issue that I was having.
The first thing you can do is install “WP Crontrol” (https://wordpress.org/plugins/wp-crontrol/). It will let run your cron job on-demand so you can test properly. The second thing you might want to do is install “BlackBox Debug Bar” (https://wordpress.org/plugins/blackbox-debug-bar/) so you can see how your operations are being performed (like the SQL queries, etc.).
You can also do some basic logging that might help you find the problem. The easiest way that I’ve found is to dump “logged events” into transients so I can grab them after, for example, a cron run. You can dump whatever you want into the log.
For inside the insert_episodes() function…
Then you can add a log viewer like so…
Hopefully, that should help you narrow down the problem. But I would say the first thing to do is check the date / time on the two servers. Nothing causes weird, out-of-the-blue issues like time synchronization.