Using WordPress ‘get_edit_comment_link’ PHP filter

The get_edit_comment_link WordPress PHP filter allows you to modify the comment edit link.

Usage

add_filter('get_edit_comment_link', 'your_custom_function', 10, 1);
function your_custom_function($location) {
    // your custom code here
    return $location;
}

Parameters

  • $location (string) – The URL for the comment edit link.

More information

See WordPress Developer Resources: get_edit_comment_link

Examples

This example adds a custom query parameter named extra_param to the comment edit link.

add_filter('get_edit_comment_link', 'add_extra_param', 10, 1);
function add_extra_param($location) {
    $location = add_query_arg('extra_param', 'value', $location);
    return $location;
}

This example changes the comment edit link to open in a new tab by adding the target="_blank" attribute.

add_filter('get_edit_comment_link', 'open_edit_link_in_new_tab', 10, 1);
function open_edit_link_in_new_tab($location) {
    $location = str_replace('">', '" target="_blank">', $location);
    return $location;
}

This example changes the text of the comment edit link to “Edit this comment”.

add_filter('get_edit_comment_link', 'change_edit_link_text', 10, 1);
function change_edit_link_text($location) {
    $location = preg_replace('/>(.*?)</', '>Edit this comment<', $location);
    return $location;
}

This example removes the edit comment link for users with the ‘subscriber’ role.

add_filter('get_edit_comment_link', 'remove_edit_link_for_subscribers', 10, 1);
function remove_edit_link_for_subscribers($location) {
    if (current_user_can('subscriber')) {
        return '';
    }
    return $location;
}

This example adds a custom CSS class named custom-edit-link to the edit comment link.

add_filter('get_edit_comment_link', 'add_custom_css_class', 10, 1);
function add_custom_css_class($location) {
    $location = str_replace('<a ', '<a class="custom-edit-link" ', $location);
    return $location;
}