How to control Autoptimize optimize css in front-end
-
I want to add my own extra condition before Autoptimize css js enqueue in front-end.
In your plugin where I can add this http://prntscr.com/stfeey
-
well, suppose you want to disable Autoptimize, you can use a code snippet like this one to do so;
add_filter('autoptimize_filter_noptimize','noptimize_some',10,1); function noptimize_some( $flag_in ) { if ( stripos( $_SERVER['HTTP_USER_AGENT'], 'Chrome-Lighthouse') !== false ) { return true; } else { return $flag_in; } }Do take into account this can only work reliably if you don’t have a page cache (plugin, server-level, hoster or 3rd party) ..
Actually it disable the Autoptimize. But I want Autoptimize does not print CSS and js on front-end. I know this sounds wired. it breaks design but I am looking for it.
-
This reply was modified 6 years, 4 months ago by
Rokeybur Rahman.
so removing all CSS/JS when the user-agent is
Chrome-Lighthouse, that’s what you want ? πyeah.
away from my normal computer (it crashed :/ ), so i can’t test now, but this might work:
add_filter('autoptimize_filter_cache_getname','noptimize_some',10,1); function noptimize_some( $name_in ) { if ( stripos( $_SERVER['HTTP_USER_AGENT'], 'Chrome-Lighthouse') !== false ) { return ''; } else { return $name_in; } }frank
No it’s not working. I also try to reverse the condition to test if it’s works in a normal browser. but nothing happens.
*nothing* happens? at all? that’s.. weird. can you share the URL where this is active?
https://rokeyfx.com
http://prntscr.com/sttwx5
All cache disabled even cloudflare is development mode.darn .. in that case you’ll probably have to use the
autoptimize_html_after_minifyfilter to remove the resources with some regular expressions rokeyfx ..could you give me the code please.
Not enough time and not on my normal computer, so afraid not Rokey, sorry.
I just change filter name to that but it not print html also. i mean full blank.
no, not all filters are that simple, here’s a stub;
add_filter('autoptimize_html_after_minify', 'remove_autoptimize'); function remove_autoptimize( $html_in ) { if ( stripos( $_SERVER['HTTP_USER_AGENT'], 'Chrome-Lighthouse') !== false ) { $html_out = preg_replace('<add regular expression magic here>', '', $html_in; return $html_out; } else { return $html_in; } }the
<add regular expression magic here>is what you’ll have to add.hope this makes things more clear π
frankI think this is my url . can u give me the code of regex.
<link media=”all” href=”https://rokeyfx.com/wp-content/cache/autoptimize/css/autoptimize_9c86b3775c2eb4d917a7e0b6fe8c10ac.css” rel=”stylesheet” /><script defer src=”https://rokeyfx.com/wp-content/cache/autoptimize/js/autoptimize_11de6af203deefea04296c8797a12a3a.js”></script>
Those indeed are examples of the URL’s, but the hash (random-looking string) in the URL will change, which is why you need a regular expression. But I’m afraid I don’t have time to do this for you rokeyfx, sorry. Maybe this is the moment for you to dive into regular expressions? π
-
This reply was modified 6 years, 4 months ago by
The topic ‘How to control Autoptimize optimize css in front-end’ is closed to new replies.