【问题标题】:Changing urls in visual composer's raw html block在可视化作曲家的原始 html 块中更改 url
【发布时间】:2018-04-30 00:19:57
【问题描述】:

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

有人知道解决方案吗?

【问题讨论】:

  • 一个简单的 PHP 查询就可以完成这项工作。选择包含VC数据的表,然后做str_replace(),然后重新插入

标签: wordpress migrate visual-composer


【解决方案1】:

我需要support request 的解决方案,并通过以下方式解决。

您可以将Go Live Update Urls plugin 与以下过滤器一起使用。

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

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 干得好。下次我需要用这个的时候,我会回来试一试。谢谢
    • 为了完成..你的评论//If something not working, try change this rawurlencode to urlencode, etc... dumped it =)应该分别是rawurldecodeurldecode
    • 非常感谢。只是提醒其他用户不要忘记根据自己的使用更改此行$tag = 'vc_raw_js';//'vc_raw_js';//'vc_raw_html';//vc_raw_js
    • 嗨,我有一个这样的块:[vc_raw_html css=".vc_custom_1587030205472{margin-top: 0px !important;margin-right: 0px !important;margin-bottom: 0px !important;margin -left: 0px !important;}"] 脚本没有得到它。我认为,这是因为 css 属性和 {}。我如何编辑正则表达式来制作它?谢谢
    • 干得好。谢谢!
    【解决方案3】:

    这个 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 页面构建器链接。

    从这里: https://evergreendent.com/hu

    对此: https://evergreendent.hu

    【讨论】:

    • 这应该是公认的答案。坚实的发现。只需要在查找和替换中使用 url 编码的字符串(以及在非 VC 的任何地方使用常规的、未编码的查找和替换),你就很好了!谢谢!
    • 非常感谢。 (%2F),非常棘手(它有效)[节省我的大脑和时间]
    • 不得不说,我已经按照您的确切建议和屏幕截图做了很多尝试,当我可以在 WPbakery 中清楚地看到时,它会在空运行时找到 0 个实例(其他几个插件也找到 0 个实例)旧 URL 仍然存在的可视化作曲家生成器工具。在这种情况下,它们位于“原始 HTML”块中,并且我知道至少有 6 个实例。有人吗?
    【解决方案4】:

    将下面的代码放在你主题的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);
    }
    

    它将在前端为您提供帮助。 将 example.com 更改为您的网站名称

    【讨论】:

    • 这将替换 ANY 帖子中的 EVERY 'http://' 实例,无论是指向另一个站点的 URL 还是其他任何内容。 It will help you on the front end -> 至少解释一下您的代码,因为如果在不完全了解其功能的情况下这样做,可能会损坏 OP 的网站。
    • 您可以添加您网站的来信 str_replace('yoursite', 'yoursite', $content);
    • 这不适用于带有 http://www 的网站,如果 OP 混合了 http://wwwhttp://,那么这将不会 100% 有效。
    • 它是一个过滤器来改变内容输出。它不会在您的数据库中执行任何操作。
    • 谢谢,但 Visual Composer 元素通常不会触发 the_content 挂钩。经过快速测试,它不适用于原始 html 元素
    【解决方案5】:

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

    【讨论】:

    • 请再次阅读问题。这是一个很好的插件,但不会替换视觉作曲家存储的编码字符串中的任何内容
    猜你喜欢
    • 2017-09-26
    • 2018-01-09
    • 1970-01-01
    • 1970-01-01
    • 2017-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-12
    相关资源
    最近更新 更多