WooCommerce Create Post Comment Programmatically

Today’s discussion is aimed towards WooCommerce/Wordpress Create a Post Comment Programmatically. Here, we will discuss how to create a comment with a post. Oftentimes, we are faced with a need to add a comment to the posts. To add comments on any post, first of all, you need the post ID.

Below is an illustration for the same:

  $data = array(
        'comment_post_ID' => 50, // post id you want to add comment
        'comment_author' => 'Qaisar Satti', // author name
        'comment_author_email' => 'qaisarssatti@gmail.com', // author email
        'comment_author_url' => 'https://blog.qaisarsatti.com/',
        'comment_content' => 'Here is dummy comment',
        'comment_type' => 'comment', // add type of comment
        'comment_parent' => 0, // If you want to reply a comment then add parent comment Id
        'user_id' => 0, // add user id if you want to attach it with user
        'comment_author_IP' => '',
        'comment_agent' => '',
        'comment_date' => '2021-11-11 12:00:00',
        'comment_approved' => 0, // use 1 for approved
    );
     wp_insert_comment($data); // insert comment

Qaisar Satti

Hi, I'm Qaisar Satti! I've been a developer for over 20 years, and now I love sharing what I've learned through tutorials and guides. Whether you're working with Magento, PrestaShop, or WooCommerce, my goal is to make your development journey a bit easier and more fun. When I'm not coding or writing, you can find me exploring new tech trends and hanging out with the amazing developer community. Thanks for stopping by, and happy coding!