Wordpress 4.5更新后插件抛出TypeError

spe*_*.sm 51 html javascript wordpress jquery

我正在调试一个可视化作曲家插件,在我将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
Run Code Online (Sandbox Code Playgroud)

唯一出现的$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();
},
Run Code Online (Sandbox Code Playgroud)


更新:

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

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

Ben*_*Ben 121

我能够解决这个问题.事实证明我使用的是旧版本的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()
        },
Run Code Online (Sandbox Code Playgroud)

一切都很适合我!希望这有助于其他人.

  • 工作.该文件位于:/home/statingt/public_html/wp-content/plugins/js_composer/assets/js/backend/composer-view.js (9认同)
  • 万一你找不到需要编辑的文件,我的情况就在这里:`/ wp-content/plugins/js_composer/assets/js/dist/backend.min.js` (5认同)
  • composer-view.js?ver = 4.5.3:6未捕获TypeError:无法读取未定义的属性'attributes'我在更改代码后得到此属性 (4认同)
  • @DanHastings和Apeiron,请参阅下面的答案,修复"Uncaught TypeError:无法读取未定义的属性'属性'". (2认同)

ECC*_*Dan 33

在Ben的回答中尝试修补程序后我仍然遇到此错误:Uncaught TypeError:无法读取未定义的属性'custom'

所以我修改了composer-view.js中的html2element,如下所示:

 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()
    },
Run Code Online (Sandbox Code Playgroud)


小智 18

@Ben这完美!

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

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

错误:

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

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

解决方案 转到第4045行附近的文件/wp-content/plugins/js_composer_salient/assets/js/dist/backend.min.js:

======>替换代码======================================== =============

    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()
    },
Run Code Online (Sandbox Code Playgroud)

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

    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()
    },
Run Code Online (Sandbox Code Playgroud)


小智 5

注意到代码没有传递给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;
		},
Run Code Online (Sandbox Code Playgroud)