Filter Content for Attachment and Modify it (5 posts)

  1. salihkulangara
    Posted 2 years ago #

    I want to do this, just search the content for any attachments, and if attachments, usually the embedded images inside post, show the Custom size versions(that i have defined in functions.php) of those image in front end. I think we want to do a filter on, the_content, wp_get_attachment_image , etc.... . I am not sure about that, So if there anyone who can help me??

  2. bitwit
    Posted 2 years ago #


    Check out example #2 in this document. I think it might be what you need.


  3. salihkulangara
    Posted 2 years ago #

    Hai bitwit thanks for your replay,
    but Sorry, I dont want that stuff, What I want is - My client says He doest care about cropping the image before embedding to his blog post, What he do is add new post-> open the editor add title, inside the post add some text add a image, that may be 1800x1000(he is the hero), ok so it really mess up layout structure, using css not a good idea here, because some times image will be distorted, so what i am looking for is Is there a way to show the uploaded images(whatever size it may be) in custom size( that i have predefined in functions.php) in front end.

    simply - search for any embedded images inside post and display those images in our custom size not the actual image size editor uploads.

    I think a filter on the_content() will make the trick, but no idea on how to do it

  4. bcworkz
    Posted 2 years ago #

    Using 'the_content' filter could work, but it is fraught with problems because the resized image names are stored by their pixel dimensions, not the registered size name. Depending on the aspect ratio, the dimensions and file name will change. You must also alter the <img> tag height and width attributes accordingly as well.

    Even if you are able to successfully alter the content via code to reference the desired image size, the whole approach is inherently fragile, it will take much testing to ensure the code is crash proof. I think a better approach is to first remove any undesirable image sizes so the client cannot really choose the wrong size. This still leaves the full size image as an option. The full size option can be removed by overriding the media library code or by modifying the resulting HTML with jQuery.

    That resolves the issue for new posts. For existing posts, either ignore the issue or create a one time script to permanently modify the DB data much in the same way you would have done with filtering 'the_content'.

  5. salihkulangara
    Posted 2 years ago #

    Thanks @bckworz, I think I have to do more search on this topic, and as of now i didnt get any particular solution, So I think i have to stick with this css rule ---

    img.size-full, img.size-large, .wp-post-image, .post-thumbnail img {
    height: auto;
    max-width: 100%;

Topic Closed

This topic has been closed to new replies.

About this Topic