Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wordpress replacing localhost urls inside base encoded strings saved by theme, pluigns etc

Tags:

wordpress

For hosting wordpress site from localhost to live server require to change all the localhost urls to the live domain urls. Done and site also loads fine but none of the media items are showing as they are inside base encoded strings.

BUT How to replace urls inside the base encoded strings saved by the theme and other plugins?

In my case i am using BeTheme and visual composer which contains the actual page content and saved base encoded in the db. They contains the hardcoded full urls of a lot of media items.

I took the db dump in a .sql file and find and replaced all localhost to live domain url... But this is where i stuck!

like image 307
forsimb Avatar asked Jun 04 '15 03:06

forsimb


2 Answers

I encountered the same problem and found a way to do this. My example is for wp_postmeta table and some theme from Muffin

Requirements:

  • some search-replace plugin (e.g https://wordpress.org/plugins/search-and-replace/ )
  • MySQL version that has from_base64() function (5.6.1 and higher )
  • (probably optional) some meta_key to identify which rows to decode, because decoding base64 for huge table makes db servers sad .

Steps:

  1. Backup your database(!!!)

  2. Create a table with same layout as wp_postmeta:

    CREATE TABLE wp_base64_dec LIKE wp_postmeta
    
  3. Insert into new table base64_decoded values selected from wp-postmeta

    INSERT INTO wp_base64_dec(`meta_id`, `post_id`,`meta_key`,`meta_value`)
    SELECT `wp_postmeta`.`meta_id` AS `meta_id`,
      `wp_postmeta`.`post_id` AS `post_id`,
      `wp_postmeta`.`meta_key` AS `meta_key`,
       FROM_BASE64(`wp_postmeta`.`meta_value`) AS `meta_value` 
    FROM `wp_postmeta` WHERE 
      ((`wp_postmeta`.`meta_key` = 'mfn-page-items') AND
      (FROM_BASE64(`wp_postmeta`.`meta_value`) LIKE '%domain.net%')) ;
    
  4. Use search-replace plugin in wp to replace "domain.net" with "domain.com" in wp_base64_dec table .

  5. Replace the altered columns in the original table:

    REPLACE wp_postmeta
    SELECT 
      `meta_id`,`post_id`,`meta_key`,
       TO_BASE64(`meta_value`) as `meta_value`
    FROM `wp_base64_dec`
    
like image 85
frater_sourcecode Avatar answered Sep 29 '22 23:09

frater_sourcecode


The short answer is: you can't. Visual Composer, in an attempt to be helpful, base64 encodes URLs in the database. Search and replace (including wp-cli's version) doesn't work on these. It's a huge limitation, and WPBakery is aware of it--but hasn't offered a solution.

like image 21
James Avatar answered Sep 30 '22 01:09

James