All images from posts have no link set! How I can add to all posts link to attachment ? There is a filter I can inject on functions.php.


You can use the the_content filter to access the contents of the post before it is printed to the screen. The function that you use to filter it can either go two routes - using regex to parse the contents (not the "right" way but faster/dirtier) or an HTML parser. You should then be able to get to the src attribute and replace the <img/>.

function my_content_filter( $post_content ){
    // parse and modify the <imgs> in $post_content here
    return $post_content;
add_filter( 'the_content', 'my_content_filter' );

Keep in mind that if you go this route, the modified content's won't actually be stored in the database, so if something accesses $post->post_content without calling apply_filters( 'the_content', $post->post_content ) then your links won't be included. This will also put a relatively high load on your server to parse/modify the contents on every request, especially without a caching plugin in place. Programmatically accessing and updating the saved values might be a better choice if you want the updates to be ubiquitous.

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.