3

Visual Composer saves the raw HTML block as a base64 encoded (and url encoded) string in the database. My site needs to go from http to https, and I would need to change asset URL's used in these raw HTML blocks to be served with https. Clearly the Wordpress search/replace tool won't work here.

Does anybody know a solution for this?

Seer
  • 739
  • 4
  • 22
  • A simple PHP query can do the job. Select the tables that contain VC's data, then do a `str_replace()`, then re-insert – ProEvilz Nov 16 '17 at 12:48

5 Answers5

10

I have this trouble too, and i wrote little script for this.
https://github.com/lSanik/WordPress-VisualComposer-RawHtml-Base64-Replace

# WordPress VisualComposer RawHtml Base64  data base mass Replacing

For developers only!

This is little script for mass changes in wordpress visual composer database. 
You can change all domains, html code, scripts and etc, data that in base64 coded.

MAKE BACK-UP database table WP_POSTS!

All what you need, its take this in function.php at your theme folder.
Change sample data to yours needs.
Run url - your_site.com/replace_composer_html_raw_base64

If success - all data will be changed and you are happy now ;)


<?php
function replace_composer_html_raw_base64(){

    if($_SERVER['REQUEST_URI'] == '/replace_composer_html_raw_base64'){
        global $wpdb;

        ini_set('error_reporting', E_ALL);
        ini_set('display_errors', 1);
        ini_set('display_startup_errors', 1);

        $response = $wpdb->get_results('SELECT ID,post_content FROM wp_posts WHERE post_type="page"',ARRAY_A);
        $count = count($response);
        $tag = 'vc_raw_js';//'vc_raw_js';//'vc_raw_html';//vc_raw_js
        $pattern = '\['.$tag.'].*?\]';

        foreach ($response as $post){

            $matches = '';
            preg_match_all('/' . $pattern . '/s', $post['post_content'], $matches);
            $content = replacedComposerHtmlRawString($matches[0],$tag);

            if(!$content && count($content)<1){
                continue;
            }

            foreach ($content as $key=>$item){
                $post['post_content'] = str_replace($item['original'],$item['modified'],$post['post_content']);
            }

            //$post['post_content'] = replacedComposerRawFromTo();

            $upd = array(// Update the post into the database
                'ID'           => $post['ID'],
                'post_content' => $post['post_content'],
            );
            wp_update_post( $upd );
        }

        die('If no errors, all successful! =)  ');
    }
}
// String with shortcode tag, and different tag send, like vc_raw_html,vc_raw_js, etc.
function replacedComposerHtmlRawString($strings,$tag){

    if(!is_array($strings)){
        return false;
    }

    $return=array();
    foreach ($strings as $key=>$string){
        $return[$key]['original']= $string;
        $string = str_replace('['.$tag.']','',$string);
        $string = str_replace('[/'.$tag.']','',$string);
        $string = base64_decode($string);
        $string = rawurldecode($string);//If something is not working, try changing rawurldecode to urldecode, etc... dumped it =)

        //place to replace decoded string
        $string = replacedComposerRawFromTo($string);
        //end place..

        $string = rawurlencode($string);
        $string = base64_encode($string);
        $string = '['.$tag.']'.$string.'[/'.$tag.']';
        $return[$key]['modified'] = $string;
    }

    return $return;
}

function replacedComposerRawFromTo($string,$fromTo=false){
    //Changed from 'value' = > to 'value';
    $fromTo=array(
    //Sample data to change!
        'http'=>'https',
        'token123'=>'token321',
        '<script>Hello World</script>'=>'<script>HI WORLD</script>',

    );

    foreach ($fromTo as $from=>$to){
        $string = str_replace($from,$to,$string);
    }

    return $string;
}

add_action('init', 'replace_composer_html_raw_base64');
god_is_love
  • 571
  • 5
  • 18
Alexander Sanik
  • 168
  • 1
  • 8
  • Good job. Next time when I need to use this, I will come back and try it out. Thank you – Seer Oct 09 '18 at 18:59
  • for completion's sake.. your comment `//If something not working, try change this rawurlencode to urlencode, etc... dumped it =)` should be `rawurldecode` and `urldecode` respectively. – frezq Jan 25 '19 at 21:40
  • Thanks a ton mate. Just a reminder for other users to not forget to change this line as per their own use `$tag = 'vc_raw_js';//'vc_raw_js';//'vc_raw_html';//vc_raw_js` – Yug Kapoor Mar 08 '20 at 13:01
  • Hi, i have a bloc like this : [vc_raw_html css=".vc_custom_1587030205472{margin-top: 0px !important;margin-right: 0px !important;margin-bottom: 0px !important;margin-left: 0px !important;}"] The script doesn't get it. I think, it's because the css attribute and the {}. How can i edit the regex to make it ? Thanks – djoo Dec 11 '20 at 13:50
  • Great job. Thank you! – Mike Jan 15 '21 at 15:04
2

This WordPress plugin did the trick: https://wordpress.org/plugins/better-search-replace/ I tried it with encoded strings and succeed.

From this: https%3A%2F%2Fevergreendent.com%2Fhu

To this:https%3A%2F%2Fevergreendent.hu

Like this: http://prntscr.com/qxu65n

It replaced Visual Composer / WP Bakery pagebuilder links.

From this: https://evergreendent.com/hu

To this: https://evergreendent.hu

  • This should be the accepted answer. Solid find. Just have to use the url encoded strings in the find and replace (as well as a regular, un-encoded find and replace for everywhere that isn't VC) and you are good! Thanks! – Samyer Oct 01 '20 at 20:10
  • Thank you very much. (%2F), was very tricky ( It works ) [ Save my Brain and Time ] – Shahinul Islam Jan 03 '21 at 12:59
  • 1
    Have to say, I've made many attempts following your exact advice and screenshot and it finds 0 instances on a dry run (several other plugins also find 0 instances) when I can clearly see in the WPbakery visual composer builder tool that the old URLs are still there. In this case they are in a "Raw HTML" block and there are at least 6 instances that I know of. Anyone? – user3035649 Aug 16 '21 at 15:11
1

place the below code in your theme's functions.php

add_filter('the_content', 'chnage_http_to_https', 10);
function chnage_http_to_https($content){
    return str_replace('http://example.com','https://example.com', $content);
}

it will help you on front end. chnage example.com to your site name

Rajkumar Gour
  • 1,131
  • 12
  • 26
  • This will replace **EVERY** instance of 'http://' in **ANY** post, whether that be a URL to another site or what ever. `It will help you on the front end` -> at least explain your code because this could have potentially damaged OP's site if did it without knowing what it does fully. – ProEvilz Nov 16 '17 at 12:56
  • you can add come letter of your site str_replace('http://yoursite', 'https://yoursite', $content); – Rajkumar Gour Nov 16 '17 at 12:57
  • This will not work for sites with `http://www` and if OP has a mix of `http://www` and `http://` then this will not 100% work. – ProEvilz Nov 16 '17 at 12:59
  • it is a filter to chnage the content output. it will do nothing in your database. – Rajkumar Gour Nov 16 '17 at 13:00
  • 1
    Thanks, but Visual Composer elements usually not triggers the `the_content` hook. Tested quickly and it's not working with the raw html element – Seer Nov 16 '17 at 13:00
  • view your content by clickking view page button. if is not working chnage the number 10 to 100 – Rajkumar Gour Nov 16 '17 at 13:01
  • and chnage exaple.com to your site name in the function – Rajkumar Gour Nov 16 '17 at 13:02
0

I needed a solution for a support request and resolved it in the following way.

You may use the Go Live Update Urls plugin along with the following filter.

add_action( 'go-live-update-urls/database/after-update', function ( $old_url, $new_url ) {
    global $wpdb;
    $possibles = $wpdb->get_results( "SELECT ID, post_content FROM {$wpdb->posts} WHERE post_content LIKE '%[vc_raw_html]%';" );

    foreach ( $possibles as $possible ) {
        $replaced = preg_replace_callback( '/\[vc_raw_html\](.*?)\[\/vc_raw_html]/', function ( $matches ) use ( $old_url, $new_url ) {
            list( $full, $encoded ) = $matches;
            $replaced = str_replace( $old_url, $new_url, base64_decode( $encoded ) );
            return str_replace( $encoded, base64_encode( $replaced ), $full );
        }, $possible->post_content );

        if ( $replaced !== $possible->post_content ) {
            $wpdb->update( $wpdb->posts, [
                'post_content' => $replaced,
            ], [
                'ID' => $possible->ID,
            ] );
        }
    }
}, 10, 2 );

Run the plugin like normal and you will find all the URL have been replaced within the raw HTML blocks have been replaced as well.

  1. Old URL: http: (or any other URL).
  2. New URL: https: (or any other URL).
Mat Lipe
  • 725
  • 8
  • 14
-1

use https://wordpress.org/plugins/velvet-blues-update-urls/ and update the url from admin panel this will change all the Urls in the site either in content or url links.

Rashid Wasim
  • 85
  • 1
  • 4
  • 1
    read the question again please. this is a good plugin, but won't replace anything in the encoded string stored by visual composer – Seer Nov 16 '17 at 20:11