Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Can either pop this into a plugin and activate it to make the command available. Or install as a package via these instructions: https://github.com/wp-cli/wp-cli/wiki/Community-Packages
<?php
if ( !defined( 'WP_CLI' ) ) return;
/**
* Comment Generate
*/
class Comment_Generate extends WP_CLI_Command {
/**
* ## OPTIONS
*
* [--limit=<num>]
* : Number of most-recent posts to add comments to. -1 for all. Default 5
*
* [--count=<num>]
* : Number of comments to add per-post. Default 1
*
* ## EXAMPLES
*
* wp comment generate --limit=10 --count=2
*
*/
function __invoke( $args, $assoc_args ) {
$assoc_args = wp_parse_args( $assoc_args, array(
'limit' => 5,
'count' => 1,
) );
$ppp = $assoc_args['limit'];
$count = $assoc_args['count'];
$posts = get_posts("posts_per_page=$ppp");
$ids = wp_list_pluck( $posts, 'ID' );
foreach ( $ids as $id ) {
for ( $i=1; $i <= $count; $i++ ) {
wp_insert_comment( array(
'comment_author' => $this->one_of( array( 'Mr.', 'Mrs.', 'Ms.', 'Dr.' ) ) .' Author',
'comment_author_email' => 'Author@example.com',
'comment_content' => 'Lorem ipsum..',
'comment_post_ID' => $id,
) );
}
}
}
function one_of( $array ) {
shuffle( $array );
return array_pop( $array );
}
}
WP_CLI::add_command( 'comment generate', 'Comment_Generate' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment