Usually when we want to add an image to a post with a link we need to select "Link to: Media file". This is by default selected as none but the following filter would change default selection to "Media file".

add_action( 'after_setup_theme', 'default_attachment_display_settings' );
function default_attachment_display_settings() {
    update_option( 'image_default_link_type', 'file' );

The problem is that this only works for future inserted images, how do I do it for all images already inserted? Is there a filter which could overwrite this?

  • 2
    Once it's been done the filter has already ran, and the result is in the post content, WP won't go back and reprocess it like that. This filter just controls the UI – Tom J Nowell Jul 5 '18 at 9:05
  • I know, that is why I ask how to do it for everything with different code :) I am not able to find a solution – Ivan Topić Jul 5 '18 at 9:11
  • There isn't a filter as such. To add links to all these media files, you would need to (1) search your database for all images in posts, then (2) verify whether or not they're linked, then (3) only for unlinked images, add the links. You could probably do this with a series of queries and regexes, but it would be a run-once-and-update-content rather than a filter. – WebElaine Jul 5 '18 at 13:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.