Remove version parameters from WordPress javascript and css urls


In WordPress version number are append to javascript and css url. you can see these in browser by press ‘Ctrl+U’ or right click on web page then click on ‘View page source’.

You will see similar url as follows

<link rel='stylesheet' id='mytheme-style-css' href='http://www.example.com/wp-content/themes/mytheme/style.css?ver=4.0.1' type='text/css' media='all' />
<script type='text/javascript' src='http://www.example.com/wp-includes/js/jquery/jquery.js?ver=1.11.0'></script>
<script type='text/javascript' src='http://www.example.com/wp-includes/js/jquery/jquery-migrate.min.js?ver=1.2.1'></script>

After using wordpress filters you will get clean url as follows

<link rel='stylesheet' id='mytheme-style-css' href='http://www.example.com/wp-content/themes/mytheme/style.css' type='text/css' media='all' />
<script type='text/javascript' src='http://www.example.com/wp-includes/js/jquery/jquery.js'></script>
<script type='text/javascript' src='http://www.example.com/wp-includes/js/jquery/jquery-migrate.min.js'></script>

Open functions.php file and write following function.

function remove_version_para_from_style_and_script( $style_or_js_url ) {
// check if ver parameter present in url
if ( strpos( $style_or_js_url, 'ver=' ) )
// Removes an item or list from the wordpress query string
$style_or_js_url = remove_query_arg( 'ver', $style_or_js_url );
//return clean url to browser
return $style_or_js_url;
}

add_filter( 'style_loader_src', 'remove_version_para_from_style_and_script', 10 );
add_filter( 'script_loader_src', 'remove_version_para_from_style_and_script', 10 );
 
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s