Support » Requests and Feedback » Problem with cron and file_put_contents

  • Hi
    i have problem with code in my plugin. I have function with rand number and add to counter. I want to run it every day in Cron. If I run it manually in Cron manager thats all works fine, but if cron running this code i have problem becouse working only code rand number and add to old counter and save (update_option) but cod with open json file and change counter value and save this file on server dosn’t work :/

    This is fragment cron and my function.

    register_activation_hook(__FILE__, 'my_activation');
    register_deactivation_hook(__FILE__, 'my_deactivation'); // Zaczep do funkcji wyłączajacej crone
     
    function my_activation() {
        if (! wp_next_scheduled ( 'makitech_dzienny_licznik_hook2' )) {
        wp_schedule_event(time(), 'daily', 'makitech_dzienny_licznik_hook2');
        }
    }
     
    add_action('makitech_dzienny_licznik_hook2', 'do_this_hourly');
     
    function do_this_hourly() {
        losuj_dzienny_licznik2();
    }		
    		function my_deactivation() {
    			wp_clear_scheduled_hook('makitech_dzienny_licznik_hook2');
    
    		}		
    
    
    
    function losuj_dzienny_licznik2 () {
    	
    	$options = get_option( 'boj_myplugin_options' );
    	$licznik = $options['text_string'];
    	$min = $options['min_val'];
    	$max = $options['max_val'];
    	
    	
    	$nowa_wartosc = $licznik+ rand($min,$max);
    	$options['text_string'] = $nowa_wartosc;
    	
    	$path = get_home_path() ;
    	
    	$strJsonFileContents = file_get_contents($path."licznik.json");
    	
        $data = json_decode($strJsonFileContents, true);
    	
    	$data['licznik'] = "{$nowa_wartosc}";
    	
    	$newJsonString = json_encode($data);
        file_put_contents($path."licznik.json", $newJsonString);
    	
    	
    	 update_option('boj_myplugin_options', $options);
    	 
    
    }
    
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.