Plugin throwing TypeError after WordPress 4.5 update

后端 未结 10 1409
甜味超标
甜味超标 2020-12-07 19:06

I\'m debugging a visual composer plugin that broke after I updated WordPress to 4.5 and I can\'t figure out why it is throwing a TypeError.

The error message in the

相关标签:
10条回答
  • 2020-12-07 19:11

    I'm using the theme Applay (2.1.3, a bit outdated). I've just updated WP and all the plugins to the most recent version (4.5.2) and came to this issue as well. I did not analyzed the flow of this component (js_composer), just this "broken" function (it's not really broken). I realized that this.template and $template are getting wrong object types (it needs another validation aside _.isString(html)). I solved it by adding a try & catch block as follows:

    ORIGINAL

        html2element:function (html) {
            var attributes = {}, 
                $template;
            if (_.isString(html)) {
                this.template = _.template(html);
                $template = $(this.template(this.model.toJSON()).trim());
            } else {
                this.template = html;                                                                                                                                            
                $template = html;
            }
            _.each($template.get(0).attributes, function (attr) {
                attributes[attr.name] = attr.value;
            }); 
            this.$el.attr(attributes).html($template.html());
            this.setContent();
            this.renderContent();
        },
    

    MODIFIED

        html2element:function (html) {
            var attributes = {}, 
                $template;
            if (_.isString(html)) {
                this.template = _.template(html);
            } else {
                try {
                    this.template = _.template(html());                                                                                                                          
                } catch (err) {
                    this.template = html;
                }   
            }   
            $template = $(this.template(this.model.toJSON()).trim());
            _.each($template.get(0).attributes, function (attr) {
                attributes[attr.name] = attr.value;
            }); 
            this.$el.attr(attributes).html($template.html());
            this.setContent();
            this.renderContent();
        },
    
    0 讨论(0)
  • 2020-12-07 19:13

    Well, i found the solution in this site: https://wordpress.org/support/topic/visual-composer-is-not-working

    first: edit html2element:.... in in /wp-content/plugins/js_composer/assets/js/backend/composer-view.js

    html2element: function(html) {
            var $template, attributes = {},
                template = html;
            $template = $(template(this.model.toJSON()).trim()), _.each($template.get(0).attributes, function(attr) {
                attributes[attr.name] = attr.value
            }), this.$el.attr(attributes).html($template.html()), this.setContent(), this.renderContent()},
    

    second: However if you open up the frontend editor you'll have this "trim" issue on custom_views.js:101 , and line 467 of another file. I forgot the name, but I think it may be frontend_editor.js.

    edit in: \wp-content\plugins\js_composer\assets\js\frontend_editor\

    • frontend_editor.js
    • custom_views.js

    Bad code:

    this.$controls = $( _.template( template, data, _.extend( {},
                vc.template_options,
                { evaluate: /\{#([\s\S]+?)#}/g } ) ).trim() ).addClass( 'vc_controls' );
    

    Fixed code:

    this.$controls = $( _.template( template, data, _.extend( {},
                vc.template_options,
                { evaluate: /\{#([\s\S]+?)#}/g } ) )().trim() ).addClass( 'vc_controls' );
    

    third: See the black magic.

    Cheers.

    0 讨论(0)
  • 2020-12-07 19:14

    try updating your theme. Goto Appearance>Themes, and check for an update. This resolved the issue for me automatically upon update.

    The error arises when you update to WP 4.5 for me running the Nimva theme. You have to update to 2.02 of Nimva, which allows auto updates.

    0 讨论(0)
  • 2020-12-07 19:15

    @Ben This works perfect!

    Cause: Admin was not loading the correct visual editor for js_composer plugin after updatethis plugins.

    =====================================================

    Error:

    Error: TypeError: $template.get is not a function Source File: wp-content/plugins/js_composer_salient/assets/js/dist/backend.min.js?ver=4.10 Line: 4047

    =====================================================

    Solution Goto file /wp-content/plugins/js_composer_salient/assets/js/dist/backend.min.js around line 4045:

    ======> Replace the code =====================================================

        html2element: function(html) {
            var $template, attributes = {};
            _.isString(html) ? (this.template = _.template(html), $template = $(this.template(this.model.toJSON(), vc.templateOptions["default"]).trim())) : (this.template = html, $template = html), _.each($template.get(0).attributes, function(attr) {
                attributes[attr.name] = attr.value
            }), this.$el.attr(attributes).html($template.html()), this.setContent(), this.renderContent()
        },
    

    ======> Replace with this code ========================================

        html2element: function(html) {
            var $template, attributes = {},
            template = html;
            $template = $(template(this.model.toJSON()).trim()), _.each($template.get(0).attributes, function(attr) {
                    attributes[attr.name] = attr.value}), 
                    this.$el.attr(attributes).html($template.html()), this.setContent(), 
                    this.renderContent()
        },
    
    0 讨论(0)
  • 2020-12-07 19:20

    I was still getting this error after trying the patch in Ben's answer: Uncaught TypeError: Cannot read property 'custom' of undefined

    So I modified the html2element in composer-view.js as follows:

     html2element: function(html) {
            var $template, attributes = {},
                template = html;
    
            $template = $(template(this.model.toJSON()).trim());
            if($template.get(0))
            {
                _.each($template.get(0).attributes, function(attr) {
                attributes[attr.name] = attr.value
            })};
    
            this.$el.attr(attributes).html($template.html()),
            this.setContent(),
            this.renderContent()
        },
    
    0 讨论(0)
  • 2020-12-07 19:22

    I made this modification that works on my WP 4.8.1 (PHP7)

    in the file wp-content/plugins/js_composer/assets/js/backend/composer-view.js

    you must modify the render method :

    replace this line

    this.html2element(_.template($shortcode_template_el.html(), this.model.toJSON())); 
    

    by this line

    this.html2element( $shortcode_template_el.html() );
    

    It seems the _.template() function that doesn't work perfectly and doesn't return the good object, so better give the html code instead.

    0 讨论(0)
提交回复
热议问题