You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何移除WordPress中script标签的type属性以消除页面警告?

Fixing "The type attribute is unnecessary for JavaScript resources" Warning in WordPress

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 like header.php or footer.php (where most site scripts are loaded). Search for instances of <script type="text/javascript"> and remove the type="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 the script_loader_tag filter to strip out the type attribute automatically. Add this code to your theme's functions.php file, 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 using wp_enqueue_script() in your theme or plugin, double-check that you aren't manually setting the type attribute. The default behavior of wp_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);
      

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:25:06