Skip to content

Instantly share code, notes, and snippets.

@trepmal
Created July 3, 2014 18:15
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trepmal/5aef5b0e80d5d9744481 to your computer and use it in GitHub Desktop.
Save trepmal/5aef5b0e80d5d9744481 to your computer and use it in GitHub Desktop.
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