0

The goal is to replace the word 'attachment' in attachment URLs to read another word, so vising an attachment would be like:

site.com/category/article-name/NEW-WORD/attachment-name/

Is this even possible in WordPress? I'm not sure if it would be better to completely replace it (so there is no 'attachment' URL) or just add a URL so that both /attachment/attachment-name and NEW-WORD/attachment-name would work (in order to avoid possible problems if WordPress updates something related to attachments).

WordPress helper Milardovich suggested this as one possible way to do it, but it didn't work for me (the /IMAGE/attachment-name URL just brings a 404, but nicely the attachment/attachment-name still works).

function wpd_attachment_link( $link, $post_id ){
    $post = get_post( $post_id );
    return home_url( '/images/' . $post->post_title );
}
add_filter( 'attachment_link', 'wpd_attachment_link', 20, 2 );

Adding to this Milo's code (below):

function wpd_rename_attachment_rewrites( $rules ){
    foreach( $rules as $key => $value ){
        $new_key = str_replace( 'attachment', 'IMAGE', $key );
        unset( $rules[ $key ] );
        $rules[ $new_key ] = $value;
    }
    return $rules;
}
add_filter( 'rewrite_rules_array', 'wpd_rename_attachment_rewrites' );

Now the code(s) has some effect but doesn't completely work because its not directing to the correct URL (it's removed the category/article-name/ from the attachment URL when you click on the link for an attachment page, so you get this:

Viewing attachment page for an image goes to:

site.com/IMAGE/image-name

… which is nothing there because its a different URL from the correct new attachment permalink, so its an Error404 on the front end when you visit that URL.

However, you can get to the correct URL by typing in:

site.com/category/article-name/IMAGE/image-name

… and brings up the correct attachment page.

So is there a way your code can be adjusted a bit so that when you click on the attachment for an image, it goes not to site.com/IMAGE/image-name but instead goes to site.com/category/article-name/IMAGE/image-name (ie it adds category/article-name to the URL direction)?

1 Answer 1

0

There are two parts to URLs in WordPress-

One part is the output of URLs for any given content. To change how these are output, you need to filter them. This is what your attachment_link filter does.

The other part is how WordPress determines what content requested URLs should load. This is the part you're missing- WordPress still expects these requests to contain attachment, not your new word.

To fix that, you can filter the rewrite rules that WordPress generates, and replace attachment with whatever unique thing you want:

function wpd_rename_attachment_rewrites( $rules ){
    foreach( $rules as $key => $value ){
        $new_key = str_replace( 'attachment', 'new-word', $key );
        unset( $rules[ $key ] );
        $rules[ $new_key ] = $value;
    }
    return $rules;
}
add_filter( 'rewrite_rules_array', 'wpd_rename_attachment_rewrites' );

EDIT-

The attachment_link function in your question ignores the original value of $link, and just returns a URL in a fixed pattern:

function wpd_attachment_link( $link, $post_id ){
    $post = get_post( $post_id );
    return home_url( '/images/' . $post->post_title );
}
add_filter( 'attachment_link', 'wpd_attachment_link', 20, 2 );

Change that to replace attachment within the value passed as $link instead, preserving the rest of the original URL:

function wpd_attachment_link( $link, $post_id ){
    return str_replace( '/attachment/', '/new-word/', $link );
}
add_filter( 'attachment_link', 'wpd_attachment_link', 20, 2 );
5
  • After placing both pieces of code and flushing permalinks, the following: when you click on an attachment, it directs to site.com/NEW-WORD/attachment-name/ which is a 404 simply because it's not the right address. When I type site.com/category/post-name/NEW-WORD/attachment-name/ in, I get to the correct page and its working. Why doesn't the attachment link have the category/post-name in it?
    – ordresser
    Sep 22, 2018 at 18:32
  • your attachment_link filter should just replace attachment with whatever word you want in its place, right now your filter completely overwrites whatever was passed to the function in $link
    – Milo
    Sep 23, 2018 at 1:13
  • I've updated my question
    – ordresser
    Sep 27, 2018 at 17:00
  • @ordresser see edit above
    – Milo
    Sep 28, 2018 at 3:29
  • I have tested this, and so far I think it works. Thanks! Nice.
    – ordresser
    Sep 28, 2018 at 20:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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