Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am in search of a snippet or function that will filter the embedded images inside a post to a custom size I have defined in functions.php.

Here is the scenario:

A client uploads and add any image size into the_content(). Sometimes he will upload very large images, and if i use CSS to resize the image it will not always work correctly. Is there a way to get the embedded images and replacing them with a custom size that I have already defined to fit the layout dynamically using the_content filter.

Using the below code, I get all the attachments I have inside the post, and can loop through it,

    $attachments = get_posts($args);
if ($attachments) {
    foreach ($attachments as $attachment) {
        .....
    }
}

Now, is it possible to take these image attachments, remove them from the_content, and replace them with a custom post thumbnail I've specified in functions.php.

EDIT

To make it more specific, here is what I have in mind, I am not a PHP geek, I know the code is not correct, but it is just a hint to what I want,

  function myCustomSize(){
        global $post;
        $args = array(
                            'post_type' => 'attachment',
                            'numberposts' => null,
                            'post_status' => null,
                            'post_parent' => $post->ID,
                            'exclude'     => get_post_thumbnail_id()
                        );
                        $attachments = get_posts($args);
                        if ($attachments) {
                            foreach ($attachments as $attachment) {
                              return wp_get_attachment_image( $attachment->ID, 'themerpro_post_thumb' );

                            }
                        }

        }
        add_filter('the_content', 'myCustomSize');

What this should do, is it will return a specified image (ie, themerpro_post_thumb), that is exactly what I want, but the issue with the above code is, it will display only one image from the post, it should return an array. The second issue is it will skip out the text inside the post (I mean paragraphs, etc...).

share|improve this question
    
So, you want to search the content of a post for incrusted images, check the size and if it is not correct replace it with the correct size. Right? –  cybmeta Jun 20 at 6:34
    
@cybmeta - exactly, that I want. Search the content for any attachments, and show the Custom size versions of those image in front end. –  Salih K Jun 20 at 6:42
    
With no offense, I think it much better if you teach to the editor to choose the correct image size for embedded images. In you code you are trying to get the attachments of the post from the database, you are not searching the content for images. Please, clarify if you want search the content for embedded images or display the files attached to the post. –  cybmeta Jun 20 at 16:29
    
@cybmeta - my code is not an ultimate one, i was just playing with a code i got from wp forum, but the good part is i can see i am getting the custom size image from content, i just want to show that. Actually I want to search the content for embedded images (ie, adding images inside content editor using add media button) and force to display custom size image(that is created when uploading image using add media button) rather displaying original size images in the post. –  Salih K Jun 21 at 3:57

1 Answer 1

You can use add_image_size function to add new image size and can use wp_get_attachment_image to retrieve those custom size image.

share|improve this answer
    
Please don't post links as an answer. You should add the relevant information/code from that links, links should only be used for further reading and credit to the website/author. Please edit your answer accordingly –  Pieter Goosen Jun 20 at 6:29
    
brthr I know those stuffs, but my issue is How to display my Custom sized Images in front end that comes inside the_content() function. is there a way to filter attachment image sizes that comes through the_content(). –  Salih K Jun 20 at 6:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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