Skip to content

Instantly share code, notes, and snippets.

@aprea
Last active August 29, 2015 13:56
Show Gist options
  • Save aprea/9344578 to your computer and use it in GitHub Desktop.
Save aprea/9344578 to your computer and use it in GitHub Desktop.
A small WordPress plugin to create a bunch of dummy comments for a specified post
<?php
/*
Plugin Name: Basic Dummy Comments
Version: 0.1
Plugin URI: https://github.com/aprea
Description: Creates a bunch of dummy comments
Author: Chris Aprea
Author URI: https://gist.github.com/aprea/9344578
License: GPL v3
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
$paragraphs = array(
'Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts.',
'Separated they live in Bookmarksgrove right at the coast of the Semantics, a large language ocean.',
'A small river named Duden flows by their place and supplies it with the necessary regelialia.',
'It is a paradisematic country, in which roasted parts of sentences fly into your mouth.',
'Even the all-powerful Pointing has no control about the blind texts it is an almost unorthographic life One day however a small line of blind text by the name of Lorem Ipsum decided to leave for the far World of Grammar.',
'The Big Oxmox advised her not to do so, because there were thousands of bad Commas, wild Question Marks and devious Semikoli, but the Little Blind Text didn’t listen.',
'She packed her seven versalia, put her initial into the belt and made herself on the way.'
);
$paragraph_count = count( $paragraphs ) - 1;
if ( isset( $_GET['dummy-comments'] ) ) {
$users = get_users();
$user_count = count( $users );
$comment_ids = array();
for ( $i = 0; $i < 40; $i++ ) {
$rand = mt_rand( 0, $user_count );
// Guest user
if( $rand == $user_count ) {
$user = new stdClass();
$user->ID = 0;
$user->user_email = '';
$user->user_url = 'http://wordpress.org';
$user->display_name = 'Guest';
}
else { // Regular user
$user = $users[$rand];
}
$content = (array) array_rand( $paragraphs, mt_rand( 1, $paragraph_count ) );
$content = array_values( array_intersect_key( $paragraphs, array_flip( $content ) ) );
shuffle( $content );
$content = '<p>' . implode( '</p><p>', $content ) . '</p>';
$comment_parent = 0;
// 75% chance to be a reply comment, 25% to be a regular comment
if( 1 == mt_rand( 0, 3 ) && ! empty( $comment_ids ) ) {
$comment_parent = array_rand( $comment_ids, 1 );
$comment_parent = $comment_ids[$comment_parent];
}
$comment = array(
'comment_post_ID' => 1,
'comment_author' => $user->display_name,
'comment_author_email' => $user->user_email,
'comment_author_url' => $user->user_url,
'comment_content' => $content,
'comment_parent' => $comment_parent,
'user_id' => $user->ID,
);
$id = wp_insert_comment( $comment );
$comment_ids[] = $id;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment