ctovelox ctovelox - 1 year ago 88
jQuery Question

Replace attr of wordpress post images

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


backup images are in the folder:

example backup image:

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


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

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download