Support » Plugin: WP-Matomo Integration (WP-Piwik) » How to remove unnecessary type attribute for JavaScript?

  • Hey @braekling

    When I use the “default tracking” option, the plugin inserts a snippet of javascript, typically in the footer. First three lines shown here:

    !-- Matomo -->↩
    <script type="text/javascript">↩
      var _paq = window._paq = window._paq || [];↩
    

    While that works perfectly fine, it still triggers a single warning when validating html:

    Warning: The type attribute is unnecessary for JavaScript resources.
    <script type="text/javascript">
    

    For details see: https://validator.w3.org/nu/?showsource=yes&doc=https%3A%2F%2Foldrup.dk%2F#l416c31

    Since this is the only warning I get, I’m obviously interested in getting rid of it. I can enter the tracking code manually, leaving out the “type=text/javascript”, but it’s much less error prone, using the built in default tracking.

    Would you consider removing the unnecessary type attribute from the default code, or could you hint a code line in the plugin, where I could change it myself?

    All the best
    Bjarne

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author braekling

    (@braekling)

    Hi @oldrup,

    the tracking code is delivered by the Matomo API, already including the type attribute, see this example: https://demo.matomo.cloud/?module=API&method=SitesManager.getJavascriptTag&idSite=1&piwikUrl=&format=xml&token_auth=anonymous

    But I understand that you want to get rid of this message. The best way would be to ask the Matomo team to update the tracking code, beside this I can add an option to remove the attribute with the next update I’m currently preparing.

    Findus23

    (@findus23)

    Thread Starter Bjarne Oldrup

    (@oldrup)

    He @braekling

    I totally respect if the matomo team chooses to leave in the old code to support e.g. IE8 users, although I don’t really think it benefits neither site owners nor or users.

    So if “…beside this I can add an option to remove the attribute with the next update I’m currently preparing.” – is a real possibility, I would definitely applaud it and gladly help out beta testing a new update should you wish so.

    A great weekend everyone 🙂
    Bjarne

    Thread Starter Bjarne Oldrup

    (@oldrup)

    FYI there is a GitHub discussion here as well 🙂 https://github.com/matomo-org/matomo/issues/17281

    For anyone interested in a temporary workaround, this snippet added to functions.php filters out the type attributes from the HTML output, effectively allowing to pass w3c validation (unless there are other issues of course)

    /* Snippet seeks to remove the remaining warnings when validating HTML on w3c.org:
       Warning: The type attribute is unnecessary for JavaScript resources.
       Warning: The type attribute for the style element is not needed and should be omitted.
    */
    	
    add_action( 'template_redirect', function(){
    	ob_start( function( $buffer ){
    		$buffer = str_replace( array( ' type="text/css"', " type='text/css'" ), '', $buffer );
    		$buffer = str_replace( array( ' type="text/javascript"', " type='text/javascript'" ), '', $buffer );        
    		return $buffer;
    	});
    });
    

    The search/replace of the entire output is bound to have some performance costs, but I have not been able to measure any noticeable difference on the front end. Ideally, the type attribute is left out in the first place.

    All the best,
    Bjarne

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.