Is there a way to change how WordPress names ‘attachment’ so that it could say something else in the address?
]]>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 );
You can check this thread on Stack Overflow for more info:
https://wordpress.stackexchange.com/questions/14924/pretty-urls-or-permalinks-for-attachments#answer-14931
After adding the function, I uploaded a new image, then added the image (as an attachment) to an article. When I click the image (which used to go to the site.com/category/articletitle/attachment/attachmentname/ image, it now goes to site.com/images/imagename, but there’s nothing on the page (displays a 404).
The good thing about this code, though, is it doesn’t remove the ‘attachment’ address from other images already on the site, which is good because that means it won’t ruin existing links that can still go to the /attachment/ URLs, because sometimes WordPress updates and it would be a mess if the change from /attachment/ caused a problem with an update. After placing the code, I can still go to the category/title/attachment/imagename/ on the new image and it works. (Only the new domain with /images/imagename doesn’t work/goes to 404.
]]>Then, after putting your code back in the functions.php and re-flushing permalinks again and clearing the cache again, the website didn’t break that time, but still, the site.com/images/imagename just shows a 404.
]]>without code (regular WordPress):
viewing an attachment page for an image goes to:
site.com/category/article-name/ATTACHMENT/image-name
… and there’s the image on the attachment page.
—————————
I’ve added a second piece of code by Milo:
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' );
Together, the code does 2 things: 1) change the way attachments are permalinked and 2) change the way they are reached. After adding the function and refreshing permalinks/cache:
Wiewing 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)?