插件在WordPress 4.5更新后抛出TypeError

Plugin throwing TypeError after WordPress 4.5 update

本文关键字:TypeError 更新 WordPress 插件      更新时间:2024-06-03

我正在调试一个可视化作曲家插件,该插件在我将WordPress更新到4.5后崩溃,我不知道为什么它会抛出TypeError。

控制台中的错误消息:

JQMIGRATE: Migrate is installed, version 1.4.0              load-scripts.php?....
Uncaught TypeError:     $template.get is not a function     composer-view.js?ver=4.1.1.1:73

$template的唯一出现项可在下面的代码中找到。我知道这不是很多上下文,但是,我该如何解决此错误?

/**
 * Convert html into correct element
 * @param html
 */
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) { // **errors on this line**
    attributes[attr.name] = attr.value;
  });
  this.$el.attr(attributes).html($template.html());
  this.setContent();
  this.renderContent();
},


更新:

看起来这可能是jQuery的问题。WordPress 4.5包含jQuery 1.12,它修复了一个错误,该错误允许某些代码以不正确的语法运行。我假设插件代码一定有不正确的语法,但直到现在仍然运行。

https://wordpress.org/support/topic/read-this-first-wordpress-45-master-list#post-8271654

我能够解决这个问题。原来我使用的是旧版本的JS作曲家。更新到最新版本破坏了我的网站,所以我追踪了错误并将html2element功能更新为

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()
        },

在本的答案中尝试补丁后,我仍然收到此错误: 未捕获的类型错误:无法读取未定义的属性"自定义">

所以我在作曲家视图中修改了 html2element .js如下所示:

 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()
    },

@Ben 这很完美!

原因:更新此插件后,管理员没有为js_composer插件加载正确的可视化编辑器。

====

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

错误:

错误:

类型错误:$template.get 不是函数源文件: wp-content/plugins/js_composer_salient/assets/js/dist/backend.min.js?ver=4.10行: 4047

====

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

溶液转到文件/wp-content/plugins/js_composer_salient/assets/js/dist/backend.min.js在第 4045 行附近:

====

===> 替换代码 ===

========================================================
    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()
    },
=====

==> 替换为此代码 ===

=========================================
    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()
    },

注意到代码没有传递到 html2element 函数中,但确实存在于调用它的函数中(渲染(

下面的代码完全纠正了我的问题,我可以加载页面,添加,克隆,删除等

render: function () {
			var $shortcode_template_el = $( '#vc_shortcode-template-' + this.model.get( 'shortcode' ) );
			if ( $shortcode_template_el.is( 'script' ) ) {
				var newHtmlCode =  _.template( $shortcode_template_el.html(),
												this.model.toJSON(),
												vc.templateOptions.default );
				if(!_.isString(newHtmlCode)){
					newHtmlCode = $shortcode_template_el.html();
				}
				this.html2element( newHtmlCode );
			} else {
				var params = this.model.get( 'params' );
				$.ajax( {
					type: 'POST',
					url: window.ajaxurl,
					data: {
						action: 'wpb_get_element_backend_html',
						data_element: this.model.get( 'shortcode' ),
						data_width: _.isUndefined( params.width ) ? '1/1' : params.width,
						_vcnonce: window.vcAdminNonce
					},
					dataType: 'html',
					context: this
				} ).done( function ( html ) {
					this.html2element( html );
				} );
			}
			this.model.view = this;
			this.$controls_buttons = this.$el.find( '.vc_controls > :first' );
			return this;
		},

我正在使用主题Applay(2.1.3,有点过时了(。我刚刚将WP和所有插件更新到最新版本(4.5.2(,也遇到了这个问题。我没有分析这个组件(js_composer(的流程,只是这个"损坏"的功能(它并没有真正损坏(。我意识到这个.template和$template得到了错误的对象类型(除了_.isString(html)之外,它还需要另一个验证(。我通过添加一个try&catch块解决了它,如下所示:

源语言

    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();
    },

改 性

    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();
    },

我正在使用 Astra 主题。此修复程序 99.9% 有效。对于某些人来说,这只会停止旋转轮,但是一旦页面加载,视觉作曲家就不会停止。

我对此代码进行了轻微更改(现在

已发布到处(

原始 Astra 主题代码在这里(作曲家视图.js(

        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();
    },

有效的代码:

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

},

主要区别在于此处(与原始代码相比(

}); this.$el.attr

有一个分号代替原来的逗号:):

}), this.$el.attr

干杯的人:)所以

好吧,我在这个网站上找到了解决方案:https://wordpress.org/support/topic/visual-composer-is-not-working

首先:编辑HTML2元素:....在/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()},

第二:但是,如果您打开前端编辑器,您将在 custom_views.js:101 和另一个文件的第 467 行上遇到此"修剪"问题。我忘记了名字,但我认为它可能frontend_editor.js。

编辑位置: ''wp-content''plugins''js_composer''assets''js''frontend_editor''

  • frontend_editor.js
  • custom_views.js

错误代码:

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

固定代码:

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

第三:看黑魔法。

干杯。

在下面的代码中签出两个 $template.get 不是一个函数和 未捕获的类型错误:无法读取未定义的属性"属性"。为我工作。

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()
}

我做了这个修改,适用于我的WP 4.8.1(PHP7(

在文件中 wp-content/plugins/js_composer/assets/js/backend/composer-view.js

您必须修改渲染方法:

替换此行

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

通过这条线

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

似乎 _.template(( 函数不能完美运行并且不返回好的对象,所以最好改用 html 代码。

尝试更新您的主题。转到外观>主题,然后检查是否有更新。这在更新后自动为我解决了问题。

当您为我运行 Nimva 主题更新到 WP 4.5 时会出现错误。您必须更新到 Nimva 的 2.02,这允许自动更新。