Remove Extra Attributes From WordPress Theme Code

Remove Extra Attributes From WordPress Theme Code

If you are a WordPress developer, you will face a really simple problem with code standard checkers about some extra attributes. Removing extra attributes from WordPress helps to standardize the website’s HTML code. This article shows you how to remove extra attributes from your WordPress website code with code in the functions.php file.

How to Remove Extra Attributes From WordPress Website Code

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

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

To remove attributes, add the code below to the 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 the “type” attribute which has a “javascript” or “CSS” value. In rel tag removes CSS and in script tag removes javascript.

Shopping Cart