My attachments are currently rewritten from
http://localhost/?attachment_id=3 to
http://localhost/images/image-title using @Bainternet's answer here.

 $new_rules['images/(\d*)$'] = 'index.php?attachment_id=$matches[1]';  

However, wordpress still refers to the link as the default http://localhost/?attachment_id=3. Wordpress functions such as the_permalink, get_attachment_url, get_attachment_image_src, etc. all use the default format of http://localhost/?attachment_id=3.

I am able to access the image as intended if I manually type in the rewritten format http://localhost/images/image-title.

How can I get wordpress to use my custom rewrite for the permalink especially on the admin page?

EDIT: reworded question for specificity and correctness:
How do I override the_permalink to use the format /images/image-title instead of /?attachment_id=ID?
I can get image-title using $post->post_title.

EDIT #2:
For anyone reading this question in the future, I found it best to use $post->post_name for the link to insure uniqueness.
/images/post_title

  • 1
    that rule works with the attachment ID, how do you get the title from that? – Milo Apr 27 '14 at 16:59
up vote 6 down vote accepted
+50

Your rule works with the attachment ID, so I'm not sure how you're using the title, but the answer is almost identical in either case. The filter you want is attachment_link:

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 $post->post_title to $post->ID to put the ID in the URL instead of title.

  • Thanks. This will work with the attachment link function. Will it also work with get_attachment_link()? I will try it. I prefer to use the_permalink() which relies on get_attachment_link() but I don't want to lose some of the other functionality of get_attachment_link(). Really just trying to replace $link = home_url( '/?attachment_id=' . $post->ID ); with $link = home_url( '/images/' . $post->post_title ); – csi Apr 27 '14 at 17:23
  • All output of attachment URLs ultimately get run through that filter. If you want it applied in some cases and not others, you'll have to conditionally add/remove the filter. You could check if is_admin() first to have it only applied in admin screens, or add the filter in your template to only apply it for specific front end stuff. – Milo Apr 27 '14 at 17:32
  • Tried it. Worked like a charm. Thanks. Was trying to override the wrong function. Much appreciated. – csi Apr 27 '14 at 17:37
  • All works fine until I change to $post->ID. I don't have control over image names and spaces in images names gives 404 error. But when I change to $post->ID I also get 404 errors. Any idea on how to fix this? – AKNL Oct 15 '15 at 20:54

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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