WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] wp_unschedule_event() and wp_clear_scheduled_hook() do not clear events (4 posts)

  1. umchal
    Member
    Posted 1 year ago #

    This is a simple plugin to test to see if events can be added and deleted. What is intended is that when the plugin is activated, it registers an event and when the plugin is deactivated, it unschedules it.

    The problems are:

    • it adds multiple events with the same name. (I expected only one event is added.)
    • it does not clear the event when the plugin is deactivated.

    So what would be the proper way to do it? Also the events I added for testing are staying forever. I'd like to remove them. How do I do it?

    I'm using the plugin called Cron View ( http://wordpress.org/extend/plugins/cron-view/) to see the registered events.

    /* Plugin Name: Testing Event Schedules */
    define("TESTEVENTNAME", 'Testing');
    
    if( !wp_next_scheduled( TESTEVENTNAME ) ) wp_schedule_event( time(), 'twicedaily', TESTEVENTNAME, array('sample parameter value.')); 	 
    
    // unschedule it when the plugin gets deactivated
    register_deactivation_hook(__FILE__, 'Clear_Events');
    function Clear_Events() {
      wp_unschedule_event(time(), TESTEVENTNAME, array('sample parameter value.'));	// does not unschedule the event
      // wp_unschedule_event(time(), TESTEVENTNAME);	// does not unschedule the event
      // wp_clear_scheduled_hook(TESTEVENTNAME);  // does not unschedule the event
    }
    
    add_action( TESTEVENTNAME
    			, array(new TestClass, 'Log')	// the method
    			, 10	// the priority. 10 is the default
    			, 1 	// the number of argument specified in the wp_schedule_event() function
    			);	
    
    class TestClass {
    	function Log($parameter) {
    		file_put_contents(dirname(__FILE__) . '/log.txt', 'parameter: ' . $parameter . PHP_EOL, FILE_APPEND);
    	}
    }
  2. umchal
    Member
    Posted 1 year ago #

    It seems wp_unschedule_event() and wp_clear_scheduled_hook() cannot delete events without the exact the same argument having passed when they were registered.

    I looked into the core and wrote a custom function for it.

    function WPUnscheduleEventsByName($strEventName) {
    
    	// this function removes registered WP Cron events by a specified event name.
    
    	$arrCronEvents = _get_cron_array();
    	foreach ($arrCronEvents as $nTimeStamp => $arrEvent)
    		if (isset($arrCronEvents[$nTimeStamp][$strEventName])) unset( $arrCronEvents[$nTimeStamp] );
    	_set_cron_array( $arrCronEvents );
    }

    The whole sample code.

    <?php
    /* Plugin Name: Testing Event Schedules */
    define("TESTEVENTNAME", 'Sample_Event');
    
    if( !wp_next_scheduled( TESTEVENTNAME ) ) wp_schedule_event( time(), 'twicedaily', TESTEVENTNAME, array('Hello world!'));
    register_deactivation_hook(__FILE__, 'Clear_Events');
    function Clear_Events() {
    	WPUnscheduleEventsByName(TESTEVENTNAME);
    }
    
    function WPUnscheduleEventsByName($strEventName) {
    
    	// this function removes registered WP Cron events by a specified event name.
    
    	$arrCronEvents = _get_cron_array();
    	foreach ($arrCronEvents as $nTimeStamp => $arrEvent)
    		if (isset($arrCronEvents[$nTimeStamp][$strEventName])) unset( $arrCronEvents[$nTimeStamp] );
    	_set_cron_array( $arrCronEvents );
    }	
    
    add_action( TESTEVENTNAME
    			, array(new TestClass, 'Log')	// the method
    			, 10	// the priority. 10 is the default
    			, 1 	// the number of argument specified in the wp_schedule_event() function
    			);	
    
    class TestClass {
    	function Log($parameter='') {
    		file_put_contents(dirname(__FILE__) . '/log.txt', 'parameter: ' . $parameter . PHP_EOL, FILE_APPEND);
    	}
    }

    However, it is still a mystery that two events are added when activating this plugin. I'd like to make it only a single event being added.

  3. bcworkz
    Member
    Posted 1 year ago #

    You're checking for a scheduled event and scheduling one if not yet scheduled every time the plugin file is loaded. I'm guessing there's some sort of time discrepancy thing happening, where the event is not fully scheduled before the file is loaded again.

    Try wrapping the if not scheduled statement in a function, then hooking the function to the 'wp' action. This should ensure an event is fully scheduled before the function is ever run again.

  4. umchal
    Member
    Posted 1 year ago #

    When I put wp_schedule_event() in register_activation_hook(), it started not creating multiple events. Thanks..

Topic Closed

This topic has been closed to new replies.

About this Topic