I'm trying to add an endpoint to unattached media items. This is what I hope to achieve:


...where foo is the endpoint I'm trying to add.

Here's my code:

add_action( 'init', function() {
    add_rewrite_endpoint( 'foo', EP_ATTACHMENT );
} );

add_action( 'template_redirect', function() {
    global $wp_query;
    if ( ! isset( $wp_query->query_vars['foo'] ) ) {
    include dirname( __FILE__ ) . '/my-template.php';
} );

This doesn't seem to work because when I visit example.com/attachment-slug/foo/ I get a 404.

How can I add an endpoint to unattached media?


EP_ATTACHMENT will insert rules for things attached to posts (and custom post types), but not unattached media.

You can make this work by adding it to EP_PAGES instead-

add_rewrite_endpoint( 'foo', EP_PAGES );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.