andfinally
Forum Replies Created
-
Forum: Developing with WordPress
In reply to: wp cron is not running my functionThat’s a good description @michelts!
So the add_action with the callback should indeed be registered in for example the init hook. Or anything that is also executed / instantiated when wp-cron runs.
I’m not sure about how calling
add_action
in theinit
hook ensures the class whose method you’re calling gets instantiated? But it’s an important point: an instance of the class must exist at the time the scheduled job runs.In my case I was calling a static method, so I didn’t have that particular problem – but you’re right, it can be the cause of this issue in many cases.
- This reply was modified 3 years, 2 months ago by andfinally.
Forum: Developing with WordPress
In reply to: wp cron is not running my functionHi @lauraabraham, I’m not too familiar with the Code Snippets plugin, but it looks like it should work just as well as if you’d added the code to your
functions.php
. You might even be able to combine them in one snippet.Forum: Developing with WordPress
In reply to: wp cron is not running my functionIn my case I was able to get it working by jiggling things round a bit. I added an
init
action at the root level of a file which is included byfunctions.php
:add_action( 'init', function () { if ( ! has_action( 'import_scheduled_job' ) ) { add_action( 'import_scheduled_job', 'Products\do_import', 10, 2 ); } } );
And added the action handler at the root level of a file which contained my import class, which calls the class’s import method.
namespace Products; class Product_Importer { // Other functions, including one which calls create_async_job // when the user starts an import private function create_async_job( $file ) { if ( ! wp_next_scheduled( 'import_scheduled_job' ) ) { wp_schedule_single_event( time(), 'import_scheduled_job', array( 'file' => $file, 'attachment_id' => $this->id ) ); } } public static function import( $file, $attachment_id ) { // ... } } function do_import( $file, $attachment_id ) { Product_Importer::import( $file, $attachment_id ); }
Not very elegant, but it does the job for now. Hope this helps other people with the same problem. I’m still not sure why I was getting this, but judging by the fix, I suspect it’s something to do with the scope of classes and/or namespaces.
One gotcha that might catch other people: you pass an
$args
array of parameters when you callwp_schedule_single_event
, but when the action handler is called, those params are passed individually. So if your array has two elements, specify 2 arguments in youradd_action
call.- This reply was modified 3 years, 11 months ago by andfinally.
Forum: Developing with WordPress
In reply to: wp cron is not running my functionI have the same issue.
- I’ve deactivated the default cron spawning by setting
DISABLE_WP_CRON
to true - I’ve made a cron job on the host which calls
wp_cron.php?doing_wp_cron
every minute. - The event appears in the cron list using the WP Crontrol plugin, and apparently runs. But there is no output, either to error log or saving an option.
- I’m able to run the job via WP-CLI, which reports that it has successfully run. But again, the action handler hasn’t done anything.
I’m able to make this work, so maybe it’s something to do with the way I’m adding the action handler.
add_action( 'init', function () { if ( ! wp_next_scheduled( 'do_single_action' ) ) { wp_schedule_single_event( time(), 'do_single_action' ); } add_action( 'do_single_action', 'do_this_once' ); function do_this_once() { error_log( 'DO THIS ONCE' ); } } );
- This reply was modified 3 years, 11 months ago by andfinally.
- This reply was modified 3 years, 11 months ago by andfinally.
phillipmad’s solution didn’t do the trick for me, I’m seeing this issue on a Docker container on Sierra. I haven’t tried the “roll your own PHP” solution yet.
Forum: Plugins
In reply to: WordTube – baffled!!!!Ah now I get it! You need to upload your FLV under Tools > WordTube, not under Media > Add new. Then [media id=1] tag works perfectly.
Thanks very much dconnors!
Fred