Upgraded To WordPress 4.5 – Syntax Error, Unrecognized Expression


About The Author
Cabe Nolan is the founder of WP Cover where he shares his insight into WordPress, development, & entrepreneurship. Outside of WPCover, Cabe continues to run a successful WordPress development firm, Bold City Design as well as a few high profile websites, Arrivala, Two Way Resume, Dock Skipper, and a successful outdoors brand, DolfinPack.







Upgraded To WordPress 4.5 – Syntax Error, Unrecognized Expression

Quick post regarding a syntax error related to a recent upgrade to WordPress 4.5.  I’ve been using this smooth scroll technique from CSS Tricks for the greater part of five years.  Today, when upgrading to WordPress 4.5 I noticed the sites scripts.js file was kicking back some new errors.  After investigating my console, I saw this particular line:

Syntax error, unrecognized expression: a[href*=#]:not([href=#])

The error is related to the newest version of jQuery that comes pre-packaged with WordPress and thankfully it’s a simple fix.  The issue is the selector is not valid within the first line of the below smooth scrolling function:



$(function() {
  $('a[href*="#"]:not([href="#"])').click(function() {
    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
      var target = $(this.hash);
      target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
      if (target.length) {
        $('html, body').animate({
          scrollTop: target.offset().top
        }, 1000);
        return false;
      }
    }
  });
});

To fix it, just replace the first line of:

$('a[href*=#]:not([href=#])').click(function() { 

with:

$('a[href*=\\#]:not([href=\\#])').click(function() { 

Now we are escaping the special character that is the pound (or hashtag) sign. All set, no more errors!


Share Your Thoughts

Leave a Reply

Your email address will not be published.


Related Stuff You Might Like







WordPress News, Tips, & Code Snippets

Join the WP Cover mailing list and get wordpress news, tips, code snippets, security warnings, and more delivered right to your inbox.  We won't flood your inbox, newsletters typically go out every 1-2 weeks unless it involves an important security release.

You have Successfully Subscribed!