PHP code example of alleyinteractive / wp-bulk-task
1. Go to this page and download the library: Download alleyinteractive/wp-bulk-task library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
alleyinteractive / wp-bulk-task example snippets
class My_Custom_CLI_Command extends WP_CLI_Command {
use Bulk_Task_Side_Effects;
/**
* Replace all instances of 'apple' with 'banana' in post content.
*
* ## OPTIONS
*
* [--dry-run]
* : If present, no updates will be made.
*
* [--rewind]
* : Resets the cursor so the next time the command is run it will start from the beginning.
*
* ## EXAMPLES
*
* # Bananaify links.
* $ wp my-custom-cli-command bananaify
*/
public function bananaify( $args, $assoc_args ) {
$bulk_task = new \Alley\WP_Bulk_Task\Bulk_Task(
'bananaify',
new \Alley\WP_Bulk_Task\Progress\PHP_CLI_Progress_Bar(
__( 'Bulk Task: remove_broken_links', 'my-textdomain' )
)
);
// Handle rewind requests.
if ( ! empty( $assoc_args['rewind'] ) ) {
$bulk_task->cursor->reset();
WP_CLI::log( __( 'Rewound the cursor. Run again without the --rewind flag to process posts.', 'my-textdomain' ) );
return;
}
$this->pause_side_effects();
// Set up and run the bulk task.
$dry_run = ! empty( $assoc_args['dry-run'] );
$bulk_task->run(
[
'post_status' => 'publish',
'post_type' => 'post',
'tax_query' => [
[
'field' => 'slug',
'taxonomy' => 'category',
'terms' => 'fruit',
],
],
],
function( $post ) use ( $dry_run ) {
if ( false !== strpos( $post->post_content, 'apple' ) ) {
$new_value = str_replace( 'apple', 'banana', $post->post_content );
if ( $dry_run ) {
WP_CLI::log( 'Old post_content: ' . $post->post_content );
WP_CLI::log( 'New post_content: ' . $new_value );
} else {
$post->post_content = $new_value;
wp_update_post( $post );
}
}
}
);
$this->resume_side_effects();
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.