Enhance your WordPress performance by disabling wp-cron.php

David Beroff

Well-known member
Registered
Joined
Jun 14, 2016
Messages
1,480
Points
63
WordPress uses the wp-cron.php file to set up auto posts in a schedule. By default, Wordpress sets to run the wp-cron.php file every time when the site has visited. With website has less visits, this process consumes server resources negligible, but with a large amount of traffic, this is a real problem when a significant amount of server resources are wasted.

So, we should disable wp-cron.php to increase the performance of the website. Here's the way:
Use the text editor to open the wp-config.php file and add to the bottom of the file.

Code:
define ('DISABLE_WP_CRON', 'true');
How to add as follows:

Code:
/** The Database Collate type. Don't change this if in doubt. */

define ('DB_COLLATE', '');

....

define ('DISABLE_WP_CRON', 'true');
Then save.

If you are using a PHP cache like Zend Opcache, you have to clear the php cache by restarting php.

Code:
service php-fpm restart
Hope it helps!
 

webalternative

Member
Registered
Joined
Mar 18, 2018
Messages
39
Points
8
Hi,

wp-cron.php is not just for "auto posts in a schedule"... It do way more than just this...

A LOT of plugins use wp-cron in order to work correctly. Auto-update and plugins/themes version check also rely on wp-cron.

If you disable wp-cron I highly suggest to setup a real cronjob that run wp-cron at least 1 time per hour.
 

David Beroff

Well-known member
Registered
Joined
Jun 14, 2016
Messages
1,480
Points
63
wp-cron.php is not just for "auto posts in a schedule"... It do way more than just this...
I check codes in wp-cron.php and there are no more things to do there
<?php
/**
* WordPress Cron Implementation for hosts, which do not offer CRON or for which
* the user has not set up a CRON job pointing to this file.
*
* The HTTP request to this file will not slow down the visitor who happens to
* visit when the cron job is needed to run.
*
* @package WordPress
*/

ignore_user_abort(true);

if ( !empty($_POST) || defined('DOING_AJAX') || defined('DOING_CRON') )
die();

/**
* Tell WordPress we are doing the CRON task.
*
* @var bool
*/
define('DOING_CRON', true);

if ( !defined('ABSPATH') ) {
/** Set up WordPress environment */
require_once( dirname( __FILE__ ) . '/wp-load.php' );
}

/**
* Retrieves the cron lock.
*
* Returns the uncached `doing_cron` transient.
*
* @ignore
* @since 3.3.0
*
* @return string|false Value of the `doing_cron` transient, 0|false otherwise.
*/
function _get_cron_lock() {
global $wpdb;

$value = 0;
if ( wp_using_ext_object_cache() ) {
/*
* Skip local cache and force re-fetch of doing_cron transient
* in case another process updated the cache.
*/
$value = wp_cache_get( 'doing_cron', 'transient', true );
} else {
$row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", '_transient_doing_cron' ) );
if ( is_object( $row ) )
$value = $row->option_value;
}

return $value;
}

if ( false === $crons = _get_cron_array() )
die();

$keys = array_keys( $crons );
$gmt_time = microtime( true );

if ( isset($keys[0]) && $keys[0] > $gmt_time )
die();


// The cron lock: a unix timestamp from when the cron was spawned.
$doing_cron_transient = get_transient( 'doing_cron' );

// Use global $doing_wp_cron lock otherwise use the GET lock. If no lock, trying grabbing a new lock.
if ( empty( $doing_wp_cron ) ) {
if ( empty( $_GET[ 'doing_wp_cron' ] ) ) {
// Called from external script/job. Try setting a lock.
if ( $doing_cron_transient && ( $doing_cron_transient + WP_CRON_LOCK_TIMEOUT > $gmt_time ) )
return;
$doing_cron_transient = $doing_wp_cron = sprintf( '%.22F', microtime( true ) );
set_transient( 'doing_cron', $doing_wp_cron );
} else {
$doing_wp_cron = $_GET[ 'doing_wp_cron' ];
}
}

/*
* The cron lock (a unix timestamp set when the cron was spawned),
* must match $doing_wp_cron (the "key").
*/
if ( $doing_cron_transient != $doing_wp_cron )
return;

foreach ( $crons as $timestamp => $cronhooks ) {
if ( $timestamp > $gmt_time )
break;

foreach ( $cronhooks as $hook => $keys ) {

foreach ( $keys as $k => $v ) {

$schedule = $v['schedule'];

if ( $schedule != false ) {
$new_args = array($timestamp, $schedule, $hook, $v['args']);
call_user_func_array('wp_reschedule_event', $new_args);
}

wp_unschedule_event( $timestamp, $hook, $v['args'] );

/**
* Fires scheduled events.
*
* @ignore
* @since 2.1.0
*
* @param string $hook Name of the hook that was scheduled to be fired.
* @param array $args The arguments to be passed to the hook.
*/
do_action_ref_array( $hook, $v['args'] );

// If the hook ran too long and another cron process stole the lock, quit.
if ( _get_cron_lock() != $doing_wp_cron )
return;
}
}
}

if ( _get_cron_lock() == $doing_wp_cron )
delete_transient( 'doing_cron' );

die();
A LOT of plugins use wp-cron in order to work correctly. Auto-update and plugins/themes version check also rely on wp-cron.
You are right on this, if plugins or themes do jobs with wp-cron.php then we should not disable wp-cron.php in this case.

If you disable wp-cron I highly suggest to setup a real cronjob that run wp-cron at least 1 time per hour.
It is the way I am thinking about. :)
 

VirtuBox

Well-known member
Registered
Joined
May 3, 2016
Messages
1,622
Points
83
VirtuBox
You can disable wp-cron on all your wordpress sites, but you have to add a cronjob on your server to run wp-cron with the time interval of your choice :
Here some examples of cronjob you can use :
Bash:
# with curl - php-fpm will handle the request
*/10 * * * * curl http://votresite.tld/wp-cron.php?doing_wp_cron > /dev/null 2>&1
# directly with the cli - php-cli will handle the request (no timeout)
*/10 * * * * cd /var/www/votresite.tld/htdocs; php /var/www/votresite.tld/htdocs/wp-cron.php > /dev/null 2>&1
Additionaly to avoid issues with post scheduling, you can add
PHP:
define( 'ALTERNATE_WP_CRON',    'true' );
in your wp-config.php, as well as a timeout to make sure wp-cron will not impact your website performance
PHP:
define( 'WP_CRON_LOCK_TIMEOUT', 60 );
 

vpatella

New member
Registered
Joined
Apr 12, 2018
Messages
4
Points
1
I'm interested in what this actually does for the service that improves performances? What does the file actually do for the user when it is enabled?
 

SenseiSteve

Web Hosting Sensei
Hosting Provider
Registered
Joined
Nov 11, 2015
Messages
273
Points
28
I'm curious how you would define a large amount of traffic? Any testing to determine the breaking point?
 

kibe

New member
Registered
Joined
Aug 30, 2017
Messages
3
Points
1

Mujkanovic

Well-known member
Collaborate
Registered
Joined
Apr 24, 2016
Messages
424
Points
18
If WordPress users have no experience with setting up cron jobs on servers, you can do it with WP Cron plugin:
https://wordpress.org/plugins/easycron/.
This plugin hasn’t been tested with the latest 3 major releases of WordPress. It may no longer be maintained or supported and may have compatibility issues when used with more recent versions of WordPress.
Last updated: 3 years ago
I am not sure it will work correctly with latest version of Wordpress.
 

kibe

New member
Registered
Joined
Aug 30, 2017
Messages
3
Points
1
kibe
Hi,
I tested this Cron Plugin, it is working for the latest version of WP.
Maybe you can use the Easycron directly, there is a tutorial here, hope help you set cron job.
 
Older Threads
Replies
13
Views
5,212
Replies
3
Views
1,856
Replies
6
Views
4,503
Replies
3
Views
1,854
Newer Threads
Replies
12
Views
3,974
Replies
2
Views
1,148
Replies
2
Views
1,935
Replies
2
Views
2,767
Recommended Threads
Replies
7
Views
3,228
Replies
1
Views
1,296

Latest Hosting OffersNew Reviews

Sponsors

Tag Cloud

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top