Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Automatically Change All WordPress

  • SOLVED

I'm sure this isn't possible but I thought I'd ask.

I have a blog with over 400 posts. Each post has multiple images that were inserted using the "Full Size" option at a width of 960px.

I have since changed the max width in the media settings to be 700px and have run the Regenerate Thumbnails plugin.

So, now I have "Large" thumbnails of all the images at a max 700px wide.

Is there a way to automatically change every instance of the "Full Size" images in every post to instead use the "Large" thumbnail?

CLARIFICATIONS:

- No, I cannot just change the width of the post to accomodate 960px since the new post width is 700px.

- They're embedded images in the posts, not "featured images".

- If it's not possible, any ideas as to the best way of doing things since editing over 400 posts directly is out of the question?

Thanks.

Answers (2)

2011-12-08

John Cotton answers:

Are these embedded images - ie img elements in the html of the post? Or are you talking about post thumbnails (aka featured images)?


Jason Manheim comments:

Sorry, yes, they're embedded images.

If it were "featured images" that would be easy. ;)


John Cotton comments:

Could you do a search and replace in the database? If the images have gone from one fixed size to another, the file names should be consistent and some REPLACE sql could do it.

UPDATE wp_posts SET post_content = REPLACE( 'xxx', 'yyy', post_content) WHERE post_type = 'post'

That sort of thing.

If the file names are not consistent, then you'd need something that regex'ed each post content for the image src names, and then replaced by identifying the new filename in the uploads directory.

Too late in the day for me to write that now, but I might have a look tomorrow if the SQL idea (which is much easier) doesn't work.


Jason Manheim comments:

The widths of the images are all 960px but the heights are all different. So, it wouould be going from 960px to 700px wide.

The heights would be based on some sort of aspect ratio...

960x550 would become 700x501
960x700 ... 700x510
960x342 ... 700x249
...etc...etc...etc...

As for the names, they should be consistant just appended with the new sizes since thats what WP does. So...

http://website.com/wp-content/uploads/2011/10/VF_0304b.jpg

would become...

http://website.com/wp-content/uploads/2011/10/VF_0304b-700x465.jpg

That all makes sence to me but I have no idea how to write a SQL command to achieve all that.

If you could sometime tomorrow I'd be grateful and please let me know how long it takes so I can increase the prize if necessary.


John Cotton comments:

<blockquote>The widths of the images are all 960px but the heights are all different. </blockquote>

I thought you might say that...

The SQL route is a non-starter - quicker to do something in PHP. Same principle - go through each post and replace, but with a regex to pull out the values, do the maths and then replace.

If no-one else has a go tonight, I'll have a look some time tomorrow. It's certainly doable, just not 2 minutes of code...


John Cotton comments:

PS - are they all jpegs? ie is what we're looking for '960x???.jpg' ?


Jason Manheim comments:

They're not appended with 960x???.jpg since they were inserted using the "Full Size" option but yes, they're all .jpg.

This is for a client and it may be better to just manually change the last 10 or so posts and call it a day. I'll see what she wants.

In the meantime I'm going to award you the $15 for talking me through this so don't bother with writing the script for now. If it is indeed something the client wants to do, do you mind if I contact you directly to pay you to write the script?


John Cotton comments:

<blockquote> If it is indeed something the client wants to do, do you mind if I contact you directly to pay you to write the script</blockquote>
Of course. You can contact me through me profile page on here.


Jason Manheim comments:

Thanks, John.

2011-12-08

Francisco Javier Carazo Gil answers:

Jason,

You could always try to make it possible directly with JavaScript, but it's not the best way.

The best way should include: using ImageMagick or any other app or library in server to resize all images and change width/height in img tag.

I'm going to look the first and then look the second one. I will write in minutes.


Jason Manheim comments:

Just saw this. Thanks, Francisco, but don't worry about it. I have the answer I need.