wp-background-processing icon indicating copy to clipboard operation
wp-background-processing copied to clipboard

How can I make it run from a script only instantiating WP through wp-load.php ?

Open Hmastery opened this issue 4 years ago • 4 comments

WP_Background_Process only works if the function executing it is located in the functions.php file it's is very frustrating.

I have Cron Jobs that I execute on my own, when I want them to rely on wordpress/woocommerce features I just use: require_once(__DIR__.'/../shop/wp-load.php'); Allowing me to use whatever is in functions.php or some core features of both WC and WP.

But in this specific case when I'm trying to execute the code extending WP_Background_Process it just won't work. The invoked function is executed but the content of task method will never be executed.

Has someone a fully working piece of code executed outside of functions.php which works? I just spent 12 hours straight on this, I think I'm going nut.

Hmastery avatar Dec 03 '20 23:12 Hmastery

I have been trying to set this exactly how it says in the docs and doesn't work at all..

It doesn't create any new table, It doesn't add into options table anything like cron or event I cannot see the queue in the list I don't know how to trigger the tasks manually to check if its works or not..

I am also using it outside of functions.php

ezequiel9 avatar Dec 03 '20 23:12 ezequiel9

I know!! This plugin uses AJAX to execute the script, then your class needs to be instantiated in somewhere in addition to your dispatch action!

ezequiel9 avatar Dec 04 '20 01:12 ezequiel9

Do you have any working code example with instruction over where to put the file and how to call the initiating function? :))

Le 4 déc. 2020 à 02:22, Ezequiel Fernandez [email protected] a écrit :

 I know!! This plugin uses AJAX to execute the script, then your class needs to be instantiated in somewhere in addition to your dispatch action!

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.

Hmastery avatar Dec 04 '20 09:12 Hmastery

Your class

class TaskCreateCustomer extends WP_Background_Process {

	/**
	 * @var string
	 */
	protected $action = 'task_create_customer';

	/**
	 * Task method
	 * @param mixed $customerID
	 * @return mixed
	 */
	protected function task( $customerID ) {
                $someWrapperAPI = new Wrapper();
		$success = $someWrapperAPI->createCustomer($customerID);
		if ($success) {
		        // maybe log success
                        return false; // FALSE removes the row from the queue in theory
                }
		// maybe log the error		
	}

	/**
	 * Complete
	 */
	protected function complete() {
		parent::complete();
		// Add other task on complete maybe.. 
	}

}

Instance your class somewhere to let WP know that you have an action ajax to register. Maybe in functions.php or some other global class in your project.

new TaskCreateCustomer();

Then you can use anywhere...

$task = new TaskCreateCustomer();
$task->push_to_queue( $customerID )->save()->dispatch();

ezequiel9 avatar Dec 04 '20 22:12 ezequiel9

Closing ancient issue. :smile:

ianmjones avatar Apr 11 '23 18:04 ianmjones