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...).