• Resolved rom4in

    (@rom4in)


    Hello

    I wrote a plugin that schedule a WP cron on activation.
    The hook calls a function that calls another function included.
    The function returns reports values and they are sent via email.

    The script works well when I press Execute in the CRON manager admin.
    The script works not so well when the cron job is run. The email does not have content of the function. Though I can see with error_log that the function is well executed.

    Anyone has a tip on how to investigate further?

    Here is the code simplified :

    <?php
    /*
    Plugin Name: MY PLUGIN
    */
    
    require_once(sprintf("%s/folder/import_common_functions.php", dirname(__FILE__)));				
    
    if(!class_exists('WP_My_Plugin'))
    {
        class WP_My_Plugin
        {
    
    ...
            /**
             * Activate the plugin
             */
            public static function activate()
            {
                // Plan the CRON scheduling
                wp_plugin_schedule();
            }
    
            /**
             * Deactivate the plugin
             */
            public static function deactivate()
            {
               // Remove CRON when disabling the plugin
               if (  wp_next_scheduled( 'wp_plugin_import' ) ) {
    			 		 wp_clear_scheduled_hook( 'wp_plugin_import' );
    		}
    
            } 
    
    			...
    
        }
    } 
    
    if(class_exists('WP_My_Plugin'))
    {
    ...
        // instantiate the plugin class
        $wp_plugin = new WP_My_Plugin();
    
        function wp_plugin_schedule() {
    
    	    // Scheduling of runiing batch import of films/screenings
    	    if (  !wp_next_scheduled( 'wp_plugin_import' ) ) {
    				wp_schedule_event( time(), 'hourly', 'wp_plugin_import' );
    		}
    
    		}		
    
    		add_action( 'wp_plugin_import', 'scheduled_import' );
    
    		function scheduled_import() {
    
    			$result = null;
    			$result = import_function(); // function included in import_common_functions.php
    
    			...
    
    		  $result_mail = wp_mail( $email,
    		  'Automatic email from '.get_bloginfo( 'name')." - ".date("Y-m-d h:i"), //email subject
    		  $email_content );
    
    			...
    
    		}
    
    }

    https://wordpress.org/plugins/advanced-cron-manager/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter rom4in

    (@rom4in)

    I think I found the issue (but not the root cause). print_r and var_dump functions do not work while the script is executed vi cron.
    Any idea why?

    Plugin Author Kuba Mikita

    (@kubitomakita)

    Hi,

    I suggest you saving your log into the file instead printing it out. Here’s very handful snippet to do that:

    file_put_contents( dirname( __FILE__ ) . '/log', print_r( $array, true ) . "\r\n\r\n", FILE_APPEND );

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Cron execution VS Execution from GUI’ is closed to new replies.