admin管理员组

文章数量:1415460

is there a filter for removing style-tags from the <head></head> area in wordpress?

I want remove this style for instance:

<head>
     <style>
        @import url(';display=swap');
    </style>
</head>

I already achieved to remove link-tags by using this wordpress filter and some regex:

add_filter( 'style_loader_tag', 'removeGoogleLinks');

I guess i havbe to use wp_head somehow but I'm not sure how to use this as filter?

add_filter( 'wp_head',  $removeGoogleFontStyle); 

public function removeGoogleFontStyle($content){
   //Filter googleapi styles with regex but
   //how to use this funtion/filter?
} 

is there a filter for removing style-tags from the <head></head> area in wordpress?

I want remove this style for instance:

<head>
     <style>
        @import url('https://fonts.googleapis/css?family=Chilanka&display=swap');
    </style>
</head>

I already achieved to remove link-tags by using this wordpress filter and some regex:

add_filter( 'style_loader_tag', 'removeGoogleLinks');

I guess i havbe to use wp_head somehow but I'm not sure how to use this as filter?

add_filter( 'wp_head',  $removeGoogleFontStyle); 

public function removeGoogleFontStyle($content){
   //Filter googleapi styles with regex but
   //how to use this funtion/filter?
} 
Share Improve this question edited Sep 8, 2019 at 12:07 murcoder asked Sep 8, 2019 at 11:55 murcodermurcoder 991 gold badge1 silver badge13 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 1

My solution removes now all <link></link> tags and <style>@import url()</style> googleapi entries in the given HTML:

add_action( 'wp_footer', 'SPDSGVOPublic::removeGoogleFonts' );


     /**
     * Remove all occurrences of google fonts
     */
    public static function removeGoogleFonts()
    {
        ob_start();
        $content = ob_get_clean();
        $patternImportUrl = '/(@import[\s]url\((?:"|\')((?:https?:)?\/\/fonts\.googleapis\\/css(?:(?!\1).)+)(?:"|\')\)\;)/';
        $patternLinkTag = '/<link(?:\s+(?:(?!href\s*=\s*)[^>])+)?(?:\s+href\s*=\s*([\'"])((?:https?:)?\/\/fonts\.googleapis\\/css(?:(?!\1).)+)\1)(?:\s+[^>]*)?>/';

        preg_match_all($patternImportUrl, $content, $matchesImportUrl);
        preg_match_all($patternLinkTag, $content, $matchesLinkTag);

        $matches = array_merge($matchesImportUrl,$matchesLinkTag);

        foreach( $matches as $match ) {
            $content = str_replace( $match, '', $content );
        }

        echo $content;
    }

本文标签: filtersRemove style tags from head