The get_edit_post_link WordPress PHP filter allows you to modify the post edit link.
Usage
add_filter('get_edit_post_link', 'your_custom_function', 10, 3);
function your_custom_function($link, $post_id, $context) {
// your custom code here
return $link;
}
Parameters
- $link (string): The edit link.
- $post_id (int): Post ID.
- $context (string): The link context. If set to ‘display’, then ampersands are encoded.
More information
See WordPress Developer Resources: get_edit_post_link
Examples
Change the edit post link for a custom post type
This code changes the edit post link for the custom post type ‘my_custom_post_type’ to a custom URL.
add_filter('get_edit_post_link', 'change_edit_post_link', 10, 3);
function change_edit_post_link($link, $post_id, $context) {
$post = get_post($post_id);
if ($post->post_type == 'my_custom_post_type') {
$link = 'https://example.com/custom_edit_url/' . $post_id;
}
return $link;
}
Add a query parameter to the edit post link
This code appends a query parameter ‘custom_param’ with the value ‘1’ to the edit post link.
add_filter('get_edit_post_link', 'add_query_param_to_edit_link', 10, 3);
function add_query_param_to_edit_link($link, $post_id, $context) {
$link = add_query_arg('custom_param', '1', $link);
return $link;
}
Modify the edit post link based on user role
This code changes the edit post link for authors to include an additional parameter ‘author_edit’ with the value ‘1’.
add_filter('get_edit_post_link', 'modify_link_for_authors', 10, 3);
function modify_link_for_authors($link, $post_id, $context) {
if (current_user_can('author')) {
$link = add_query_arg('author_edit', '1', $link);
}
return $link;
}
Change the edit post link to an external site
This code changes the edit post link to an external site URL based on a custom field ‘external_edit_url’.
add_filter('get_edit_post_link', 'change_to_external_edit_link', 10, 3);
function change_to_external_edit_link($link, $post_id, $context) {
$external_url = get_post_meta($post_id, 'external_edit_url', true);
if (!empty($external_url)) {
$link = $external_url;
}
return $link;
}
Remove the edit post link for certain posts
This code removes the edit post link for posts that have a custom field ‘disable_edit_link’ set to ‘1’.
add_filter('get_edit_post_link', 'remove_edit_link_for_certain_posts', 10, 3);
function remove_edit_link_for_certain_posts($link, $post_id, $context) {
$disable_edit_link = get_post_meta($post_id, 'disable_edit_link', true);
if ($disable_edit_link == '1') {
$link = '';
}
return $link;
}