We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 29181
    • 480 Posts
    We have migrated a ModX site from one URL to another using ModBak. I am extremely happy with how fast we managed this. Well done!

    Unfortunately, whilst carrying out some final checks yesterday I noticed (to my horror) that the content in the pages (for example images) still pointed to the old URL. This is because when the client used TinyMCE to create content, it automatically inserted the url in front of assets/images/image.jpg so they all look like http://www.olddomain.com/assets/images/image.jpg These obviously need updating to the new URL. We have over 200 pages so now to my question:

    How could I create a short script to loop through the content column in the modx_site_content table and do a find and replace?

    We have phpMyAdmin, and if anyone has done something like this before....a couple of pointers would be great!

    Thanks for any help,
    Taff
      Adrian Lawley: www.adrianlawley.com
      • 26178
      • 32 Posts
      There are two solutions to this problem:

      1. Just open the SQL file generated by modbak in some handy editor (Notepad++ is my advice) and use Search and Replace function
      2. Create module with something like this inside:

      $table = $modx->getFullTableName("site_content");
      $pages = $modx->db->select("id, content", $table);
      
      while ($page = $modx->db->getRow($pages))
      {
          $id = $page['id'];
      	$new_content = str_replace("http://www.some-old-url.com", "", $page['content']); // you'll have to change this row as you need
          ($modx->db->update("content = '$new_content'", $table, "id='$id'"));
      }
      
      echo "Done.";
      


      Tweak it and run it.

      Good luck!
        • 29181
        • 480 Posts
        Option 1 is just so cunning!

        Thanks very much for your time Sara
          Adrian Lawley: www.adrianlawley.com
          • 29181
          • 480 Posts
          Option 1 worked a treat....A 5 minute task as opposed to a day or more.

          I was a little worried about breaking something, but there was no need to be.

          Owe you one Sara!

          Taff
            Adrian Lawley: www.adrianlawley.com