Visual Composer extension fails with TypeError following WordPress 4.5 upgrade

After upgrading my WordPress site to version 4.5, one of my visual composer extensions stopped working and I’m getting a JavaScript error that I can’t seem to fix.

Console Error:

JQMIGRATE: Migrate is installed, version 1.4.0
Uncaught TypeError: $element.get is not a function at builder-script.js?ver=4.1.1.1:73

I found the problematic code in my extension files. The error happens on the line where I try to access element attributes:

/**
 * Parse HTML string and create DOM element
 * @param htmlString
 */
parseHtmlString: function(htmlString) {
  var elementAttrs = {},
    $element;
  if (_.isString(htmlString)) {
    this.htmlTemplate = _.template(htmlString);
    $element = $(this.htmlTemplate(this.model.toJSON()).trim());
  } else {
    this.htmlTemplate = htmlString;
    $element = htmlString;
  }
  _.each($element.get(0).attributes, function(attribute) { // **TypeError occurs here**
    elementAttrs[attribute.name] = attribute.value;
  });
  this.$el.attr(elementAttrs).html($element.html());
  this.setupContent();
  this.displayContent();
},

I think this might be related to jQuery changes in WordPress 4.5. The new jQuery version apparently fixed some bugs that allowed incorrect code to work before. Has anyone else encountered this issue and found a solution?

Same thing happened to me after the WP 4.5 update. The problem is you’re assuming $element is always a jQuery object, but sometimes it’s not. Just wrap it with $element = $($element) before calling .get() so it always has jQuery methods. Fixed it for me!

I experienced a similar issue with the transition to WordPress 4.5. The culprit is indeed the changes in jQuery that came with the update, leading to your code failing when expecting $element to always be a jQuery object equipped with the .get() method.

The problem lies within your logic: $element can either be a jQuery object or just a raw HTML string. When it’s the latter, you won’t have access to .get(), hence the error. To remedy this, ensure you perform a check before trying to access attributes:

if ($element.get && $element.get(0) && $element.get(0).attributes) {
    _.each($element.get(0).attributes, function(attribute) {
        elementAttrs[attribute.name] = attribute.value;
    });
}

Alternatively, you could rework your else statement to guarantee $element is always a jQuery object by changing it to $element = $(htmlString);. This will ensure your code remains consistent and should eliminate the TypeError.

This error drove me crazy for weeks after updating to WordPress 4.5. It’s because jQuery got stricter with type checking in newer versions. Your code assumes $element always has jQuery methods, but when htmlString isn’t actually a string, you’re just assigning it directly to $element without wrapping it in jQuery. I fixed it by adding a simple type check before accessing attributes. Replace that problematic section with:

if ($element && $element.length && $element[0] && $element[0].attributes) {
    _.each($element[0].attributes, function(attribute) {
        elementAttrs[attribute.name] = attribute.value;
    });
}

This checks if the element exists and has the right structure before trying to loop through its attributes. The main thing to know is jQuery 1.12+ doesn’t silently fail anymore - it throws actual errors instead.