如何移除WordPress中script标签的type属性以消除页面警告?
Hey there! Let's sort out that annoying warning popping up on every page of your WordPress site. That message is just letting you know that adding type="text/javascript" to your <script> tags is no longer needed—modern browsers automatically recognize script tags as JavaScript, so that attribute is redundant now.
Here are a few practical ways to fix it:
Check and edit your theme's template files
Head over to Appearance > Theme Editor in your WordPress dashboard. Look for files likeheader.phporfooter.php(where most site scripts are loaded). Search for instances of<script type="text/javascript">and remove thetype="text/javascript"part, leaving just<script>.
Pro tip: Always back up your theme files first, or use a child theme to make edits—this way, your changes won't get overwritten when the theme updates.Filter script tags with a WordPress hook
If the warning is coming from plugins or scripts enqueued via WordPress's built-in functions, you can use thescript_loader_tagfilter to strip out the type attribute automatically. Add this code to your theme'sfunctions.phpfile, or use a code snippet plugin like Code Snippets to avoid editing theme files directly:add_filter('script_loader_tag', function($tag, $handle) { // Remove the unnecessary type attribute from all script tags return str_replace("type=\"text/javascript\"", "", $tag); }, 10, 2);This function will run every time a script loads on your site and clean up the redundant attribute automatically.
Fix enqueued scripts manually
If you're adding custom scripts usingwp_enqueue_script()in your theme or plugin, double-check that you aren't manually setting thetypeattribute. The default behavior ofwp_enqueue_script()doesn't add this attribute, but if you've included it in the optional arguments, remove it. For example:- Before (with unnecessary type):
wp_enqueue_script('custom-script', get_template_directory_uri() . '/js/custom.js', array(), '1.0', true, array('type' => 'text/javascript')); - After (fixed):
wp_enqueue_script('custom-script', get_template_directory_uri() . '/js/custom.js', array(), '1.0', true);
- Before (with unnecessary type):
This warning doesn't break your site's functionality, but fixing it will clean up your browser console and keep your code aligned with modern web standards.
内容的提问来源于stack exchange,提问作者Archana Sharma

