ctovelox ctovelox - 1 month ago 11
jQuery Question

Replace attr of wordpress post images

I moved a wordpress site, but I have many posts with the following path for images:

http://www.example.com/media/76897/tips-for-keeping-your-chips-from-getting-crushed-header.jpg


backup images are in the folder:
wp-content/uploads/blog/assets/


example backup image:
http://www.example.com/wp-content/uploads/blog/assets/76897/tips-for-keeping-your-chips-from-getting-crushed-header.jpg


I tried this place with jquery, but did not succeed.

Jquery

$('.wsContentAndSideBar .wsContent img').attr('src', function (i, src) {
return 'http://www.example.com/wp-content/uploads/blog/assets/76897/' + src.split('/')[1];
});

Answer

I'd recommend using the Wordpress plugin Search Regex. Be sure to back up your database before you commit any replacements. enter image description here

If you really want to use jquery here's code that'll work for you:

$(document).ready(function() {
      $('.wsContentAndSideBar .wsContent img').each(function() {
          var oldsrc = $(this).attr('src');
          $(this).attr('src', oldsrc.replace('http://www.example.com/media/', 'http://www.example.com/wp-content/uploads/blog/assets/'));
          });
      });

jsfiddle example