Change the Permalink for wordpress attachment

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.

Leave a Comment