Skip to content

Instantly share code, notes, and snippets.

@garciasanchezdani
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save garciasanchezdani/8ddf43db29137001a4f2 to your computer and use it in GitHub Desktop.
Save garciasanchezdani/8ddf43db29137001a4f2 to your computer and use it in GitHub Desktop.
Script which insert comments to a random post of wordpress - by using the core wordpress comments
function addComment(){
global $wpdb;
//get a random post
$args=array('post_type'=>'post', 'orderby'=>'rand', 'posts_per_page'=>'1');
$mypost=new WP_Query($args);
while ($mypost->have_posts()) : $mypost->the_post();
$mypost_id = $post->ID;
endwhile;
wp_reset_postdata();
$comentarios = array(); //custom array of comments to insert...please fill in with your values
$nombres = array(); //custom array of names to insert...please fill in with your values
$randIP = "".mt_rand(0,255).".".mt_rand(0,255).".".mt_rand(0,255).".".mt_rand(0,255); //random ip
//one comment
$comentario_aleatorio = $comentarios[array_rand($comentarios)]; //get a random value of the comments array
$nombre_aleatorio = $nombres[array_rand($nombres)]; //get a random value of the names array
//insert in the table
$time = current_time('mysql');
$data = array(
'comment_post_ID' => $mypost_id,
'comment_author' => $nombre_aleatorio,
'comment_author_email' => 'admin@admin.com',
'comment_author_url' => 'http://',
'comment_content' => $comentario_aleatorio,
'comment_type' => '',
'comment_parent' => 0,
'user_id' => 0,
'comment_author_IP' => $randIP,
'comment_agent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10 (.NET CLR 3.5.30729)',
'comment_date' => $time,
'comment_approved' => 1,
);
wp_insert_comment( $data );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment