1

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.com/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?
} 
0

1 Answer 1

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\.com\/css(?:(?!\1).)+)(?:"|\')\)\;)/';
        $patternLinkTag = '/<link(?:\s+(?:(?!href\s*=\s*)[^>])+)?(?:\s+href\s*=\s*([\'"])((?:https?:)?\/\/fonts\.googleapis\.com\/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;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.