gdarko/wp-batch-processing

How exactly to use this plugin?

Closed this issue · 8 comments

Hi, I am sorry if you think you've given all the instructions in the plugin README.md or in this article; https://medium.com/@gdarko/processing-batch-data-in-wordpress-7cc5ae5720f0

But I am not able to figure out where exactly to place my own Batch class definition? I am placing it in my own custom plugin and it doesn't get recognized when I go under "Wordpress" > "Batches".

I am new to Wordpress let alone this plugin. Please help me understand how to implement this where exactly do I need to put my batch definition and will your plugin recognize my Batch?

My Plugins structure:
myplugin/myplugin.php

myplugin.php code:

if (class_exists('WP_Batch')) {
    class Update_Posts_Status extends WP_Batch {
        public $id = 'update_files_path';
        public $title = 'Updates files path';
        public function setup() {
            $posts = get_posts();
            foreach ($posts as $post) {
                    $this->push(new WP_Batch_Item($post->ID, ['post_id' => $post->ID]));
            }
        }
        public function process($item) {
            $post_id = $item->get_value('post_id');
            $post = get_post($post_id);
            $post_update = [
                'post_id' => $post_id,
                'post_status' => 'publish'
            ];
            wp_update_post( $post_update );
            return true;
        }
        public function finish() {
            //
        }
    }
    function wp_batch_processing_init() {
	$batch = new Update_Posts_Status();
	WP_Batch_Processor::get_instance()->register($batch);
    }
    add_action('wp_batch_processing_init', 'wp_batch_processing_init', 15, 1);
}

Thanks

Hi @PythonRage ,
Though I'm not the author I think the instructions given were enough. thanks to @gdarko
looking at your code everything seems to be ok. now if you just download and install this repo as a plugin in WordPress. you should see a batches menu in your WordPress dashboard. you can just open that. there you see your initialized batches. click on manage and then click on start. it must work.

That is the problem, I am not seeing anything under the Batches section. It is empty (see attached). As you can see Batches section is there because I've done the library installation part of course hence the Batches section is available but you can see its empty.

Also if remove the check if (class_exists('WP_Batch')) it throws this error.

Fatal error: Uncaught Error: Class 'WP_Batch' not found in sites/wordpress/wp-content/plugins/myplugin/myplugin.php:42
Screenshot 2023-09-28 at 6 41 50 PM

I realized something in your code when I take a deeper look.
the following part of your code is inside the class. put it out of class. this is for initializing the class and must not be inside it.

function wp_batch_processing_init() {
    $batch = new Update_Posts_Status();
    WP_Batch_Processor::get_instance()->register($batch);
  }
add_action('wp_batch_processing_init', 'wp_batch_processing_init', 15, 1);
Screenshot 2023-09-28 at 7 21 21 PM

That code is already outside the class you can see the class ends at line 64.

yes. my bad. have you tried to load the library through another method? if you are using composer then change the method to use the library as a regular plugin. or reverse. are you sure that you have correctly required the myplugin.php? try to echo or log something in this file to make sure it's loaded

Thanks @garousiamir , This "Hello World" page and the string "Hello World! 123" is coming from this myplugin.php

I am not using composer, installed the library manually putting it in wp-content/plugins/ and then activated it from WP plugins section.

This requiring thing you mentioned what needs to be required exactly where?

Screenshot 2023-09-28 at 7 34 07 PM

Thanks for your help, I figured this out I wasn't requiring wp-batch-processing/wp-batch-processing.php into my plugin. It is now working.

very glad to hear that.