I am working on WordPress plugin and I have added support for wp cron there. See the code below:

function __construct(){

            //Cron Job 
            add_action('init', array($this,'g_order_sync'));
            add_action('ga_order_syn', array($this,'sync_order'));


function my_cron_schedules($schedules){
                $schedules["10sec"] = array(
                    'interval' => 10,
                    'display' => __('Once every 5 minutes'));

            return $schedules;

public function g_order_sync(){

        if( !wp_next_scheduled('ga_order_syn') ) {  
            wp_schedule_event( time(), '10sec', 'ga_order_syn' );  

public function sync_order(){
        $content = "some text here";
        $fp = fopen($_SERVER['DOCUMENT_ROOT']. "/myText.txt","wb");


When I print cron using print_r( _get_cron_array() );, it shows me that my cron(ga_order_syn) is scheduled every 10 second but sync_order() function does not create file at DOCUMENT_ROOT. If I add a call to the wp_mail() function, it does not send me an mail.

What is issue with my code? Why is it not working?

  • Is this a portion of a PHP class?
    – jdm2112
    Commented Nov 16, 2016 at 20:47

2 Answers 2


Are you constructing the class properly? Is the file writable? Do you need the interval or can you add the scheduled item if it doesn't exist.

This example is working, and sets an event 10 seconds into the future.

wp_schedule_event( time() + 10, null, 'ga_order_syn' );

class CronTest {

    function __construct() {
        add_action( 'init', array ( $this, 'g_order_sync' ) );
        add_action( 'ga_order_syn', array ( $this, 'sync_order' ) );

    // init

    public function g_order_sync() {
        if ( ! wp_next_scheduled( 'ga_order_syn' ) ) {
            wp_schedule_event(  time() + 10, null, 'ga_order_syn' );

    // cron job

    public function sync_order() {
        $content = time() . ": some text here";
        $this->_write_content ($content);

    // write content

    private function _write_content( $content = '') {
        $path =  $_SERVER[ 'DOCUMENT_ROOT' ] . "/myText.txt";
        if( is_writable($path)) {
            $original = file_get_contents($path);
            $original .= PHP_EOL . $content;
            $fp = fopen( $path, "wb" );
            fwrite( $fp, $original );
            fclose( $fp );
        } else {
            // log error here

// must initialize the cron class
$cron_test = new CronTest();


First of all you need to understand something about WP Cron. WP Cron does not work like server Cron.

The cron action will trigger when someone visits your WordPress site, if the scheduled time has passed.

So in your case, you have set the time has 10 seconds. You need really high traffic site to work your cron as you expected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.