Wordpress cron job running more than once

I developed a plugin to read some XML files and publish their itens as post but for some reason, sometimes, it is duplicating posts. Basically that is what I have: // function one is scheduled (hourly) and unscheduled on plugin activation and deactivation hooks register_activation_hook(__FILE__, 'function_one_activation'); register_deactivation_hook(__FILE__, 'function_one_deactivate'); function function_one_activation() { if (!wp_next_scheduled('function_one_cron')) { wp_schedule_event(time(), 'hourly', 'function_one_cron'); } } function function_one_deactivate() { $timestamp = wp_next_scheduled('function_one_cron'); wp_unschedule_event($timestamp, 'function_one_cron'); } add_action('function_one_cron', 'one'); add_action('function_two_cron', 'two'); add_action('function_three_cron', 'three'); function one() { // download XML …
Category: Web

WP Cron as Fast as WordPress AJAX?

I have a question about performing a very long and resource intensive task within WordPress. Here is the scenario. I have a plugin that allows users to run a task that takes a very long time and once completed will return the result back to the user. The first thing I did was run the task via WordPress AJAX functions. This did not work since the site locked up to that user. Meaning that if the task was started then …
Category: Web

Cron schedule not updating after run

Cron-jobs are incredibly slow in my website. I created this small script, outside of the Wordpress environment, just to test the response time of WP Cron: <?php //Method which does a basic curl get request function get_data($url) { $ch = curl_init(); $timeout = 5; curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); $data = curl_exec($ch); //getinfo gets the data for the request $info = curl_getinfo($ch); //output the data to get more information. print_r($info); curl_close($ch); return $data; } get_data('http://www.example.com?wp-cron.php?doing_wp_cron=1486419273'); These …
Category: Web

Is there any background process that I can run from plugin without depending on page hits on a website without affecting page-load speed?

I am developing a WordPress plugin and wants to run a background task that calls an API and updates database table. Now, the API can only give results for 5 DB entries in one go and for 500 entries in my table, I have to make 100 API call. The API has allowed TPS quota of 1 and also in every 40 minutes, its old response expires which means I need to update my table if any entry is older …
Category: Web

Best place to keep files that are called by cron jobs?

I am interesting in setting up a Wordpress Website which needs to keep updating certain custom database tables after fetching data from RESTful Web Services, lets say once in a day. I can achieve this by having a script to make request to REST API, process data and then store it into required tables. However, I am not sure where to put this script file so cron can call it without any issues (security wise) Can I place these script …
Category: Web

Unzip file in functions.php, and add it to cron

I have a file in the root folder of my wordpress installation (myfolder, wp-content, wp-config, and all other). I want unzip a test.zip archive in my myfolder folder in root directory. I try this function on my theme functions.php : add_action( 'test_unzip', 'unzip_folder' ); function unzip_folder() { WP_Filesystem(); $destination = '../myfolder/'; $destination_path = $destination['path']; unzip_file( $destination_path.'/test.zip', $destination_path); } I add the action in cron to do it every 3 hours, and also to try my code. It's not work and …
Category: Web

WP Cron job timeout issues

I have to run a php function for around 10 minutes. Now I am sheduling the job as a wp-cron-job. But it is getting timed out after 30seconds which is the maximum execution time for php. How to get rid of this timeout issue?
Category: Web

WordPress cron running twice

I have the following code that creates a CSV and then sends an email: function generate_and_send_csv() { // Removed CSV code for ease of reading wp_mail('[email protected]', 'New Data Files', 'You have received new data files.', $headers, $filepath); } //Create the cron event if (!wp_next_scheduled('generate_send_csv')) { wp_schedule_event('1488934800', 'daily', 'generate_send_csv'); } // Run it add_action('generate_send_csv', 'generate_and_send_csv'); I then have the following cron set up in cPanel: php /home/user/public_html/wp-cron.php After installing the Cronjob Scheduler by chrispage1 plugin, I can see the plugin is …
Category: Web

How to make sure WP-CRON job loops through all posts?

I am using WP-Crontrol and have some CRON jobs running daily to check the age of the posts. If the post is 2 days older, they will be deleted. This works but for some reason, only some of the three days and older posts get deleted. There are still some that remain published. It must be because there are a lot of posts on the site around 3000. Is there a way to make my CRON job more efficient? Here …
Category: Web

Auto Delete Users (auto_delete_users)

I need some help cause I couldn't find any solution for my problem and haven't the knowledge to solve the problem on my own. What I'm trying to do: I want to delte every WordPress user (subscriber) which is older than X days. For testing purposes I needed short intervals, in production the users should be deleted after 6 months. So each subscriber should be deleted after 6 months, after their registration. What I've tried for testing purposes: function wcs_auto_delete_users() …
Category: Web

Does wp-cron runs all tasks scheduled at same time together or one after other?

I am developing a plugin and want to add a cron task(let's say cron_task_test) but have a doubt. In case 15 tasks(they may be scheduled by multiple plugins or the user himself) are scheduled to run at a time along with my task(cron_task_test), will wp-cron pick them one by one i.e. pick another task after first has finished or if the first task has taken more than a minute? Or does all will be fired one after another In short, …
Category: Web

Can/should we delete wordpress cron jobs with no action?

I find myself during a website creation where I try several plugins and delete them afterwards. However, some plugins do not clean their cronjobs and are left to be executed with no action. My question is this: Should we clean up/delete leftover crons with no action? If we should, what is the best way to do it? Is manual the only way to go (ex. WP Crontrol plugin)? Bonus: If we should not, why not? It would be handy to …
Category: Web

Scheduled Posts and wp-cron - Why don't scheduled posts publish if too old?

I notice that the documentation for wp_schedule_single_event mentions the following:- The action will fire off when someone visits your WordPress site, if the schedule time has passed. However this does not seem to be true for scheduled posts. I notice that some posts can be missed if wp-cron is not run for a while (not sure of exact timeframe). The post then has the label 'Missed Schedule' in the admin panel. My questions are:- Is this just for scheduled posts …
Category: Web

execute function in wordpress plugin using crontab

So, I want to run a function on my plugin from crontab. For the example, I want to run it every day at 1:10am function my_cronjob_action () { //this function is in my plugin // code to execute on cron run syslog(LOG_DEBUG, 'executed by unix cron'); } add_action('my_cronjob_action', 'my_cronjob_action'); I've disabled wp-cron by adding define( 'DISABLE_WP_CRON', true ); into wp-config.php what's the next step so the function can be executed from crontab? Thank you before.
Topic: cron Wordpress
Category: Web

WordPress: Cron locking and Queue

I have some CPU intensive scripts that I don't want to run concurrently. Every now and again the scripts will overlap and crash the server. Already at 2GB and the mysql container still crashes. I have a crontab setup to hit example.com/cron.php?do_cron=randstr every 15 minutes. Is there any way to place a lock and only execute one cron task at a time? When it's done, the next one would then run. I know this can be accomplished using PHP's file …
Category: Web

Best Way to Enter Maintenance Mode Programmatically

I need the site to be in maintenance mode while I complete a cron job within a plugin. I have solved this by creating a ".maintenance" file in my plugin directory and using the following code in my cron job: $plugin_dir = ABSPATH . 'wp-content/plugins/my_plugin/'; $base_dir = ABSPATH; $static_maintenance_file = $plugin_dir . '.maintenance'; $new_maintenance_file = $base_dir . '.maintenance'; copy( $static_maintenance_file, $new_maintenance_file ); // do the business end of the cron job unlink( $new_maintenance_file ); It works as designed. However, it …
Category: Web

Most efficient way to trigger wp-cron through system cron.

I have recently decided it would be best if i disable the built in wordpress cron function and move to the system cron which will activate the wordpress cron every 15 minutes. I am wondering which way is best and what the differences are performance wise etc? The two ways I have seen this done are: curl http://example.com/wp-cron.php?doing_wp_cron > /dev/null 2>&1 and cd /var/www/example.com/htdocs; php /var/www/example.com/htdocs/wp-cron.php?doing_wp_cron > /dev/null 2>&1 Which way is better, and what are the benefits of a …
Category: Web

How to run wp_insert_post() & wpdb on the background?

I have created a code to fetch data from a remote API then echo certain data & import those data into a post type using wp_insert_post() & post meta** using wpdb->query(). Here's my current code: $imdbid = $_GET['id']; $string = file_get_contents("http://www.omdbapi.com/?i=".$imdbid."&apikey=API_KEY_HEREKEYVALUE"); $json = json_decode($string, true); if( $json['Response'] === "True" ){ //METADATA $dataID = $json['imdbID']; $dataTitle = $json['Title']; $dataYear = $json['Year']; $dbdata = array( 'post_title' => $dataID, 'post_status' => 'publish', 'post_type' => 'datadb', 'post_author' => 1 ); if( $dbid = wp_insert_post( …
Category: Web

Running WP Cron on specific local time

The scenario is to run insert_post function every midnight (00:00) of local time. Must run daily on weekday. function add_daily_task(){ if((date('l', time()) != 'Saturday') || (date('l', time()) != 'Sunday')){ // insert post } } if(!wp_next_scheduled( 'add_daily_task_schedule')){ wp_schedule_event(time(), 'daily', 'add_daily_task_schedule'); // how should change time() to meet my local schedule? } add_action('add_daily_task_schedule', 'add_daily_task');
Category: Web

Set up a WP Cron scheduled event to update calculated ACF field

I have 3 ACF fields set up on a custom post type: Number Value A - automatically updated every minute Number Value B - automatically updated every minute (at the same time as Value A) Number Value C - a calculated ACF field, dividing Value A by Value B, x 100 to give a percentage Number Value C only recalculates when I go into each individual custom post and click update. I need a solution whereby Value C is recalculated/updated …
Category: Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.