Edit Links on Posts in Blog and Archive Lists
Adds an Edit link under each post on blog, archive and search pages for logged-in users who are allowed to edit that post. Works with classic themes and Query Loop blocks.
Opens WPIDE on
Code
// Link text. Screen readers also hear the post title.
$wpide_listing_edit_text = 'Edit';
$wpide_listing_edit_link = function ( $post_id ) use ( $wpide_listing_edit_text ) {
$url = get_edit_post_link( $post_id, 'raw' );
if ( ! $url || ! current_user_can( 'edit_post', $post_id ) ) {
return '';
}
$label = $wpide_listing_edit_text . ' “' . wp_strip_all_tags( get_the_title( $post_id ) ) . '”';
return '<p class="wpide-edit-link"><a class="post-edit-link" href="' . esc_url( $url ) . '" aria-label="' . esc_attr( $label ) . '">' . esc_html( $wpide_listing_edit_text ) . '</a></p>';
};
// Classic themes: after the excerpt or content of each post on blog, archive and search pages.
$wpide_listing_edit_append = function ( $content ) use ( $wpide_listing_edit_link ) {
if ( is_admin() || is_feed() || is_singular() || wp_is_block_theme() || ! in_the_loop() || doing_filter( 'get_the_excerpt' ) ) {
return $content;
}
return $content . $wpide_listing_edit_link( get_the_ID() );
};
add_filter( 'the_excerpt', $wpide_listing_edit_append, 20 );
add_filter( 'the_content', $wpide_listing_edit_append, 20 );
// Block themes: after the Post Title block inside any Query Loop.
add_filter( 'render_block_core/post-title', function ( $content, $block, $instance = null ) use ( $wpide_listing_edit_link ) {
if ( is_admin() || ! $instance instanceof WP_Block || empty( $instance->context['postId'] ) || ! isset( $instance->context['queryId'] ) ) {
return $content;
}
return $content . $wpide_listing_edit_link( (int) $instance->context['postId'] );
}, 10, 3 );
Notes
Visitors never see the link. In block themes it appears after the Post Title block of each item in a Query Loop. Some classic themes already print an edit link in the post footer; if yours does, you will see both.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.