wp enqueue script - How to remove all enqueued assets from the active theme?

has code that removes all the enqueued styles including those that may be coming from other plugins besides the ones f

/ has code that removes all the enqueued styles including those that may be coming from other plugins besides the ones from the active theme.

Is it possible to remove all enqueued assets from only the current active theme without hardcoding the theme name or when it is not known which theme is active?

i.e., achieve the result of

remove_action( 'wp_enqueue_scripts', 'twentynineteen_scripts' );

when twentynineteen theme is active

or

remove_action( 'wp_enqueue_scripts', 'twentytwenty_register_styles' );
remove_action( 'wp_enqueue_scripts', 'twentytwenty_register_scripts' );

when twentytwenty is active.

https://gelwp/articles/removing-all-enqueued-and-default-css-scripts-in-wordpress/ has code that removes all the enqueued styles including those that may be coming from other plugins besides the ones from the active theme.

Is it possible to remove all enqueued assets from only the current active theme without hardcoding the theme name or when it is not known which theme is active?

i.e., achieve the result of

remove_action( 'wp_enqueue_scripts', 'twentynineteen_scripts' );

when twentynineteen theme is active

or

remove_action( 'wp_enqueue_scripts', 'twentytwenty_register_styles' );
remove_action( 'wp_enqueue_scripts', 'twentytwenty_register_scripts' );

when twentytwenty is active.

Share Improve this question asked Nov 14, 2019 at 5:23 Sridhar KatakamSridhar Katakam 1552 silver badges23 bronze badges 1
  • WordPress doesn’t know whether scripts or styles were enqueue by a theme or plugin. You can’t even rely on the URL, because themes could enqueue external assets. What’s your ultimate goal here? If it’s a child theme, the just check the parent theme manually to find all the asset handles. – Jacob Peattie Commented Nov 14, 2019 at 7:10
Add a comment  | 

1 Answer 1

Reset to default 2

Here you go. I took this out of my mailoptin plugin. Adapt the code to your use case.

add_action('wp_enqueue_scripts', function () use ($wp_get_theme) {
                global $wp_styles;
                global $wp_scripts;

            $wp_get_theme = wp_get_theme();

                $child_theme  = $wp_get_theme->get_stylesheet();
                $parent_theme = $wp_get_theme->get_template();

                foreach ($wp_scripts->registered as $key => $value) {
                    $src = $value->src;
                    if (strpos($src, "themes/$child_theme/") !== false || strpos($src, "themes/$parent_theme/") !== false) {
                        unset($wp_scripts->registered[$key]);
                    }

                    if (strpos($src, "/uploads/$child_theme/") !== false || strpos($src, "/uploads/$parent_theme/") !== false) {
                        unset($wp_scripts->registered[$key]);
                    }


                }

            }, 20);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745002313a4605571.html

相关推荐

  • wp enqueue script - How to remove all enqueued assets from the active theme?

    has code that removes all the enqueued styles including those that may be coming from other plugins besides the ones f

    16小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信