autoptimize_filter_css_defer_inline | WordPress.org

[ad_1]

Hi – i like this filter for it’s easier to handle than the rules under the critical css tab. The problem with this filter is, that returning the whole critical css string is rather unwieldy. I tried another approach:

add_filter('autoptimize_filter_css_defer_inline','my_ao_css_defer_inline',10,1);
     function my_ao_css_defer_inline($inlined) {
    if (is_archive() OR is_404()) {
        $inlined = file_get_contents(get_template_directory_uri() . '/css/critical/archive_critical.css');
        return $inlined;
     } else if (is_page()) {
        $inlined = file_get_contents(get_template_directory_uri() . '/css/critical/standard_critical.css');
        return $inlined;
     } else if (is_product()) {
        $inlined = file_get_contents(get_template_directory_uri() . '/css/critical/prod_critical.css');
        return $inlined;
     } 
        return $inlined; 
 }

I used file_get_contents() and this seemed to work, but when i came back to the site i noticed that that the <style> tag in the head was no longer present. I found out that file_get_contents() requires to be echoed and apparently return $inlined does not work.
Do you have a suggestion how to insert the critical css without doing something like: return '<your frontpage CCSS goes here>';?
I’am also uncertain if a condition like is_archive() OR is_404() can be used with this filter.
Thanks for your interest.
theo

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer