Using WordPress ‘edit_comment()’ PHP function

The edit_comment() WordPress PHP function updates a comment with values provided in $_POST.

Usage

The edit_comment() function is typically used when you need to update an existing comment. It takes the $_POST variable and updates the relevant comment in the database. Here’s a simple example:

// Assuming you have an existing comment with ID 1
$_POST['comment_ID'] = '1';
$_POST['comment_content'] = 'This is an updated comment content.';
edit_comment();

In this example, the comment with ID 1 is updated with the new content specified in the $_POST variable.

Parameters

The edit_comment() function does not accept any parameters directly. Instead, it uses the global $_POST variable to get the necessary data. The $_POST variable should contain:

  • comment_ID: (integer) The ID of the comment you want to update.
  • comment_content: (string) The new content for the comment.

More information

See WordPress Developer Resources: edit_comment()

The edit_comment() function is included in WordPress since version 1.5. As of the last update of this guide, it is not deprecated.

Examples

Update a comment’s content

In this example, we’re updating the content of the comment with ID 1.

$_POST['comment_ID'] = '1';
$_POST['comment_content'] = 'Updated comment content.';
edit_comment();

Update a comment’s author

Here, we’re changing the author of the comment with ID 2.

$_POST['comment_ID'] = '2';
$_POST['comment_author'] = 'New Author Name';
edit_comment();

Update a comment’s author email

In this example, we’re updating the author’s email of the comment with ID 3.

$_POST['comment_ID'] = '3';
$_POST['comment_author_email'] = '[email protected]';
edit_comment();

Update a comment’s website

This time, we’re updating the author’s website for the comment with ID 4.

$_POST['comment_ID'] = '4';
$_POST['comment_author_url'] = 'https://www.newwebsite.com';
edit_comment();

Update multiple fields of a comment

Finally, we’re updating multiple fields for the comment with ID 5.

$_POST['comment_ID'] = '5';
$_POST['comment_content'] = 'Updated comment content.';
$_POST['comment_author'] = 'New Author Name';
$_POST['comment_author_email'] = '[email protected]';
edit_comment();

Remember to sanitize and validate all user inputs to prevent any potential security risks.