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

Hourly scheduled wp_cron job keeps getting rescheduled

Every time I refresh the page, my cron job update_backup_now_feed seems to have its timer reset. I'm using a plugin called "crontrol" that displays the time until next call, and every time I refresh it resets to 1 hour. The only time the schedule seems to fire off is when I avoid the site for over an hour and then refresh. Am I doing something wrong with my code below or is this a bug? I'm working on Multisite. I …
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

Scheduling an event inside plugin class is not working

wp_schedule_single_event is not firing. I wrote a function that is posting to external API, if it failed I schedule the same posting function to run 5 seconds later in cases of timeouts. Here's the simplified code of the function: class MyPlugin { public function post( $fields, $type ) { // Posting to external API logic, if it failed I schedule the same function to run in 5 seconds. wp_schedule_single_event( time() + 5, 'post', array( $fields, $type ) ); return; } …
Category: Web

Wordpress scheduled task is called but not executed

I created a plugin with a cron to update all posts of a certain type every 5 minutes. I installed WP Crontrol to check if the cron is registered correctly and everything seems to be okay. This is how I registered my cron: function custom_cron_interval( $schedules ) { $schedules['fiveminutes'] = array( 'interval' => 300, 'display' => __('Every five minutes') ); return $schedules; } add_filter( 'cron_schedules', 'custom_cron_interval' ); if ( ! wp_next_scheduled( 'recalculate_all_scores' ) ) { wp_schedule_event(time(), 'fiveminutes', 'recalculate_all_scores'); } It …
Category: Web

Schedule reminder at exact time

I have event website. I want send reminder about event exactly 2 hours before the event to subscribers. So how can schedule wordpress to call the function exactly at time. I know about wp_schedule and cron. But that doesn't helps me. Don't recommend plugins. Because I am developing custom plugin. So what is the correct way to do that. Example: if event start time is today 11:30 pm , I want to remind the event subscribers at exactly 9:30 pm.
Category: Web

Custom recurrence not working / Wp Cron event

I created a wp cron event with a custom interval of 5 minutes, but it is only executed hourly instead of every 5 minutes. (The callback is properly executed.) The DISABLE_WP_CRON constant is set to true, wp-cron.php is called via crontab every 5 minutes. (https://developer.wordpress.org/plugins/cron/hooking-into-the-system-task-scheduler/) No errors in debug.log (WP_DEBUG set to true). I created my plugin with the wordpress plugin boilderplate generator (https://wppb.me/). My code (in class Wp_Goldprice_Activator, function activate()): function fetch_metal_prices_recurrence( $schedules ) { $schedules['every_five_minutes'] = array( 'display' …
Category: Web

WP cron and update post meta

I try to create a scheduled cron job which runs every hour, Its my first time working with wp-cron. In the cron function, I want to update post meta values, if some conditions are met. I also tested the code of the cron function outside of the cron to see/print the results. The results looked ok, but when the cronjob runs no post gets updated. Iam using WP Crontrol to see the all available cronjobs. First I schedule an event …
Category: Web

Duplicate Cron Jobs Using wp_next_scheduled / wp_schedule_event

I'm working on a plugin to register and schedule various cron jobs. I am: using WP Crontrol (just to see all of my cron jobs and override them if need be) have a server cron job set up and have define('DISABLE_WP_CRON', true); in place When I went into 'Cron Events' within Crontrol I saw that some functions were added literally hundreds of times. Specifically, in 'Cron Events' the function is what's getting registered/listed, not the hook. A simplified version of …
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

When running WordPress Cron manually, it shows WP-Cron spawning is disabled. What is WP-Cron spawning?

I use Ubuntu 20.04 LEMP stack VPS. In this server, I run 13 WordPress sites. Every 10 minutes I have set up to run each site cron job with a 20 seconds interval. So every site does not execute cron jobs exact same time. I edited crontab using the following command. crontab -u www-data -e When I go to the WordPress cron event plugin page, it shows The DISABLE_WP_CRON constant is set to true. WP-Cron spawning is disabled. What is …
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

WP-Cron tasks scheduled but not running

I'm working on a particularly large WordPress multisite network, and have been struggling with WP-Cron. It seems that WP-Cron tasks are being scheduled, but not run. The WP-Cron tasks are not being run on either our staging or production environments, but locally, they run fine. The instances are all identical—multisite, with the same set of sites, the same theme, and the same plugins activated (and same versions of plugins). There are slight differences in the version of PHP on the …
Category: Web

How to execute add_action() function from custom plugin to Crontrol plugin or do_action()?

I created a custom plugin and I need to schedule add_action using Crontrol Plugin, the Setup class is from different php class. But the schedule action cant read the Crontrol even if I execute do_action('schedule') inside functions.php it is not working. class Scheduler { public $setup; public function __construct() { add_action('scheduler', [$this, 'etf_scheduler_func']); } public function etf_scheduler_func() { $this->setup = new Setup(); $this->setup->set_table(); } }
Category: Web

How to insert post from external php file?

I have this php file on the root directory, and i seen this example for inserting post from external php file but for some reason it doesn't work for me, wp_insert_post() always returns 0 So whats the problem ? and how i can fix it? I'm trying to build a Cron Job file to insert new posts from XML file In the end Thank you! CronJob.php: <?php require_once './wp-load.php'; $new_post = array( 'post_title' => 'My New Post', 'post_content' => 'Lorem …
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

Is doing_wp_cron a necessary query string when scheduling cron.php as a cron job?

I have seen various ways to schedule the WordPress cron so that it is triggered by the server. Some will run the php file directory from the crontab and others will use a wget and reference the url. I have noticed some sites suggesting this: wget -q -O - https://domain.com/wp-cron.php?doing_wp_cron >/dev/null 2>&1 What is the difference between this and just using wget -q -O - https://domain.com/wp-cron.php >/dev/null 2>&1
Category: Web

Delete all posts with a specific custom field using meta_query

I'm building a listings website using WooCommerce. A listing is either set to live or sold inside of a custom field key status Once a listing is sold then the custom field status is updated with sold. This allows the listing to be hidden from the website, yet still able to be viewed directly, just in case users still click a link and end up on the listing page. I am looking to automatically delete (or move to trash for …
Category: Web

About

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