Using WordPress ‘comment_edit_redirect’ PHP filter

The comment_edit_redirect WordPress PHP filter allows you to modify the URI a user is redirected to after editing a comment in the admin area.

Usage

add_filter('comment_edit_redirect', 'your_custom_function', 10, 2);

function your_custom_function($location, $comment_id) {
    // your custom code here
    return $location;
}

Parameters

  • $location (string) – The URI the user will be redirected to.
  • $comment_id (int) – The ID of the comment being edited.

More information

See WordPress Developer Resources: comment_edit_redirect

Examples

Redirect to the post page after editing a comment

Redirect the user to the post page where the comment was made after editing the comment.

add_filter('comment_edit_redirect', 'redirect_to_post_page', 10, 2);

function redirect_to_post_page($location, $comment_id) {
    $comment = get_comment($comment_id);
    return get_permalink($comment->comment_post_ID);
}

Redirect to a custom admin page

Redirect the user to a custom admin page after editing a comment.

add_filter('comment_edit_redirect', 'redirect_to_custom_admin_page', 10, 2);

function redirect_to_custom_admin_page($location, $comment_id) {
    return admin_url('your-custom-admin-page.php');
}

Redirect to the comments page with a custom message

Redirect the user to the comments page and display a custom message after editing a comment.

add_filter('comment_edit_redirect', 'redirect_with_custom_message', 10, 2);

function redirect_with_custom_message($location, $comment_id) {
    return add_query_arg('message', 'custom', $location);
}

Redirect to the user’s profile page

Redirect the user to their own profile page after editing a comment.

add_filter('comment_edit_redirect', 'redirect_to_user_profile', 10, 2);

function redirect_to_user_profile($location, $comment_id) {
    return admin_url('profile.php');
}

Redirect to a custom URL based on the comment’s post category

Redirect the user to a custom URL depending on the category of the post the comment was made on.

add_filter('comment_edit_redirect', 'redirect_based_on_category', 10, 2);

function redirect_based_on_category($location, $comment_id) {
    $comment = get_comment($comment_id);
    $post_categories = wp_get_post_categories($comment->comment_post_ID);

    // Check the first category
    $category = get_category($post_categories[0]);

    if ($category->name === 'Category 1') {
        return 'https://example.com/category-1/';
    } elseif ($category->name === 'Category 2') {
        return 'https://example.com/category-2/';
    }

    return $location;
}