Using WordPress ‘edit_post_link’ PHP filter

The edit_post_link WordPress PHP filter allows you to modify the post edit link anchor tag.

Usage

add_filter('edit_post_link', 'your_custom_function', 10, 3);

function your_custom_function($link, $post_id, $text) {
    // your custom code here
    return $link;
}

Parameters

  • $link (string) – The anchor tag for the edit link.
  • $post_id (int) – The post ID.
  • $text (string) – The anchor text.

More information

See WordPress Developer Resources: edit_post_link

Examples

Change the edit link text to “Edit this post” for all posts.

add_filter('edit_post_link', 'change_edit_link_text', 10, 3);

function change_edit_link_text($link, $post_id, $text) {
    $text = 'Edit this post';
    $link = '<a class="post-edit-link" href="' . get_edit_post_link($post_id) . '">' . $text . '</a>';
    return $link;
}

Add a custom class “my-custom-class” to the edit link.

add_filter('edit_post_link', 'add_custom_class', 10, 3);

function add_custom_class($link, $post_id, $text) {
    $link = str_replace('class="', 'class="my-custom-class ', $link);
    return $link;
}

Hide the edit link for specific post types

Hide the edit link for a custom post type called “my_custom_post_type”.

add_filter('edit_post_link', 'hide_edit_link_for_custom_post_type', 10, 3);

function hide_edit_link_for_custom_post_type($link, $post_id, $text) {
    if (get_post_type($post_id) == 'my_custom_post_type') {
        return '';
    }
    return $link;
}

Change the edit link URL to a custom URL.

add_filter('edit_post_link', 'change_edit_link_url', 10, 3);

function change_edit_link_url($link, $post_id, $text) {
    $custom_url = 'https://example.com/custom-url/';
    $link = '<a class="post-edit-link" href="' . $custom_url . '">' . $text . '</a>';
    return $link;
}

Show the edit link only to logged-in users.

add_filter('edit_post_link', 'show_edit_link_for_logged_in_users', 10, 3);

function show_edit_link_for_logged_in_users($link, $post_id, $text) {
    if (is_user_logged_in()) {
        return $link;
    }
    return '';
}