Halloween Sale 15% off Ends in See the plans
PHP

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.

Don’t have WPIDE yet? Install it on your site

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

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.