在视觉作曲家的原始 html 块中更改 url

问题描述 投票:0回答:6

Visual Composer 将原始 HTML 块保存为数据库中的 base64 编码(和 url 编码)字符串。我的站点需要从 http 转到 https,并且我需要更改这些原始 HTML 块中使用的资产 URL 以使用 https 提供服务。很明显,Wordpress 搜索/替换工具在这里不起作用。

有人知道这个的解决方案吗?

wordpress migrate visual-composer
6个回答
10
投票

我也有这个烦恼,为此我写了小脚本
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');

2
投票

这个 WordPress 插件成功了: https://wordpress.org/plugins/better-search-replace/ 我用编码字符串试了一下,成功了。

由此: https%3A%2F%2Fevergreendent.com%2Fhu

为此:https%3A%2F%2Fevergreendent.hu

像这样: http://prntscr.com/qxu65n

它取代了 Visual Composer / WP Bakery pagebuilder 链接。

由此: https://evergreendent.com/hu

为此: https://evergreendent.hu


1
投票

将以下代码放入主题的 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);
}

它会在前端帮助你。 chnage example.com 到您的网站名称


0
投票

我需要一个支持请求的解决方案,并通过以下方式解决了它。

您可以使用 Go Live Update Urls 插件 以及以下过滤器。

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 );

正常运行插件,您会发现所有 URL 都已被替换,原始 HTML 块也已被替换。

  1. 旧网址:
    http:
    (或任何其他网址)。
  2. 新网址:
    https:
    (或任何其他网址)。

-1
投票

使用 https://wordpress.org/plugins/velvet-blues-update-urls/ 并从管理面板更新 url,这将更改网站中的所有 url,无论是内容还是 url 链接。


-1
投票

我没有足够的声誉来发表评论,但我想补充一点,如果您设置了分类法,请务必更改您的帖子类型。在我们的案例中,“产品”和“技术”用于查找与这些帖子类型关联的 wp-bakery 页面。谢谢 Alexander ... WP Bakery/Visual Composer 应该在他们的知识库中引用此代码。

© www.soinside.com 2019 - 2024. All rights reserved.