Blog, WordPress, WordPress Snippet Codes

Remove Extra Attributes From WordPress Theme

Removing extra attributes from WordPress helps to standard website’s HTML code. This article shows you how to remove extra attributes from your website code.

How to Remove Extra Attributes From WordPress website code

In WordPress some tags have extra attributes – mostly <rel> and <script> tags. These tags do a specific job. “rel” adds Style Sheet file to HTML document and the “Script” tag adds JavaScript file to document.

Some attributes of these tags must be removed. These attributes generate errors or warnings in the W3C standard code check.

To remove attributes, add code below into functions.php file.

add_action('wp_loaded', 'output_buffer_start');
function output_buffer_start()
{
    ob_start("output_callback");
}

function output_callback($buffer)
{
    return preg_replace("%[ ]type=[\'\"]text\/(javascript|css)[\'\"]%", '', $buffer);
}

This code will remove “type” attribute which has “javascript” or “css” value. In rel tag removes css and in script tag removes javascript.