Skip to content

Instantly share code, notes, and snippets.

@levantoan
Last active November 29, 2020 12:11
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save levantoan/519bb0d42c9f7bd6c4d78ef1686bb848 to your computer and use it in GitHub Desktop.
Save levantoan/519bb0d42c9f7bd6c4d78ef1686bb848 to your computer and use it in GitHub Desktop.
Fix $template.get is not a function in VC when updated WordPress to 4.5
<?php
/*
Find html2element in
Version < 4.8: /wp-content/plugins/js_composer/assets/js/backend/composer-view.js
Version > 4.9: wp-content/plugins/js_composer/assets/js/dist/backend-actions.min.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) { // **errors on this line**
attributes[attr.name] = attr.value;
});
this.$el.attr(attributes).html($template.html());
this.setContent();
this.renderContent();
},
/*
Then replace to function
*/
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()
},
/*by http://levantoan.com/cach-sua-loi-uncaught-typeerror-template-get-not-function-cua-plugin-visual-composer-khi-update-wordpress-len-4-5/*/
@gangsthub
Copy link

Cannot read property 'attributes' of undefined :(

@bdew70
Copy link

bdew70 commented Oct 4, 2016

+1. I copied the code above, and not working. I also deactivated, and then reactivated the plugin after the edit. I get the same console error: Uncaught TypeError: Cannot read property 'attributes' of undefined : composer-view.js?ver=4.5.1:157 on this part exactly: $template.get(0).attributes

@amritoshpandey
Copy link

Noticed that code was not being passed into the html2element function, but did exist in the function calling it (render)

The following code has completely corrected my problems, I can load the page, add, clone, remove, etc

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

@marcelovani
Copy link

This fixed it for me, thanks

@mateuspaulo
Copy link

This fixed it for me too, thanks. Good job!

@richellyitalo
Copy link

Thanks! Nice job!

@alexwcoleman
Copy link

worked, thanks!
WP 4.9.5
VC 4.7.4

@elliottbenzle
Copy link

Works. Only needed to change render function from @amritoshpandey. Original html2element function did not need to be changed.

@duchuyta
Copy link

Noticed that code was not being passed into the html2element function, but did exist in the function calling it (render)

The following code has completely corrected my problems, I can load the page, add, clone, remove, etc

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

It works, thank @amritoshpandey

@segebee
Copy link

segebee commented Nov 29, 2020

This was really helpful...replacing htmlelement worked for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment