Create a WP test site with lots of users and posts
Posted on Sun 05 January 2020 in programming, IT, wordpress
The following WP-CLI command data create
creates a lot of test data into a WordPress site: 1000 new users, each with 50 posts. Each post gets a random date in the last year and a random post content of variable length filled using a lorem ipsum
generator.
<?php
WP_CLI::add_command( 'data create', 'create_test_data' );
function ppc_create_test_data() {
$user_n = 1000;
$posts_per_user = 50;
while( $user_n > 0 ) {
$user_id = wp_create_user( $user_n, 'password' );
if( ! is_wp_error( $user_id ) )
WP_CLI::line( "Created user ".$user_id );
for( $n = $posts_per_user; $n > 0; $n-- ) {
$post_id = wp_insert_post( array(
'post_author' => $user_id,
'post_date' => date( 'Y-m-d H:i:s', mt_rand(1663027200, 1669852799) ),
'post_content' => lorem(500),
'post_title' => $n,
) );
if( ! is_wp_error( $post_id ) )
WP_CLI::line( "Created post ".$post_id );
}
--$user_n;
}
}
function lorem($count = 1, $max = 20, $standard = true) {
// https://stackoverflow.com/questions/20633310/how-to-get-random-text-from-lorem-ipsum-in-php
$output = '';
if ($standard) {
$output = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, ' .
'sed do eiusmod tempor incididunt ut labore et dolore magna ' .
'aliqua.';
}
$pool = explode(
' ',
'a ab ad accusamus adipisci alias aliquam amet animi aperiam ' .
'architecto asperiores aspernatur assumenda at atque aut beatae ' .
'blanditiis cillum commodi consequatur corporis corrupti culpa ' .
'cum cupiditate debitis delectus deleniti deserunt dicta ' .
'dignissimos distinctio dolor ducimus duis ea eaque earum eius ' .
'eligendi enim eos error esse est eum eveniet ex excepteur ' .
'exercitationem expedita explicabo facere facilis fugiat harum ' .
'hic id illum impedit in incidunt ipsa iste itaque iure iusto ' .
'laborum laudantium libero magnam maiores maxime minim minus ' .
'modi molestiae mollitia nam natus necessitatibus nemo neque ' .
'nesciunt nihil nisi nobis non nostrum nulla numquam occaecati ' .
'odio officia omnis optio pariatur perferendis perspiciatis ' .
'placeat porro possimus praesentium proident quae quia quibus ' .
'quo ratione recusandae reiciendis rem repellat reprehenderit ' .
'repudiandae rerum saepe sapiente sequi similique sint soluta ' .
'suscipit tempora tenetur totam ut ullam unde vel veniam vero ' .
'vitae voluptas'
);
$max = ($max <= 3) ? 4 : $max;
$count = ($count < 1) ? 1 : (($count > 2147483646) ? 2147483646 : $count);
for ($i = 0, $add = ($count - (int) $standard); $i < $add; $i++) {
shuffle($pool);
$words = array_slice($pool, 0, mt_rand(3, $max));
$output .= ((! $standard && $i === 0) ? '' : ' ') . ucfirst(implode(' ', $words)) . '.';
}
return $output;
}
?>