abbas derafshi
abbas derafshi

Reputation: 287

how can reply wordpress comment programmatically

in WORDPRESS, when need to add comment use this function:

wp_insert_comment();

but, after add the comment, I need to add reply to comment programmatically, there is way for this?

Upvotes: 0

Views: 480

Answers (2)

Jack K
Jack K

Reputation: 51

The WP_Comment Object has a parent property on it. Give the ID of the comment you're replying to, as the parent ID for this comment.

wp_update_comment(['comment_ID' => {{REPLYING_COMMENT_ID}}, 'comment_parent' => {{REPLYING_TO_COMMENT_ID}}, 'comment_content' => 'My new comment.']);

We can use the wp_update_comment() function to update a comment array with all the data we need.

https://developer.wordpress.org/reference/functions/wp_update_comment/

Upvotes: 1

Josh.Millard
Josh.Millard

Reputation: 23

/**
 * Fires immediately after a comment is inserted into the database.
 *
 * @since 2.8.0
 *
 * @param int        $id      The comment ID.
 * @param WP_Comment $comment Comment object.
 */
do_action( 'wp_insert_comment', $id, $comment );

You could hook into wp_insert_comment and just ignore a specific user of the comment id if their role is administrator. For example;

If( author of comment id user privileges != 'administrator' ) {
//insert reply
}

Upvotes: 0

Related Questions