Documentation

wp-hide/module/general_css_combine/add_media_query

Posted in: Getting Started (4) How To (15) Plugin Options Explained (37) Actions / Filters (31)      Code Example (16)  

This functionality is available for PRO version.

Name: wp-hide/module/general_css_combine/add_media_query
Type: Filter
Arguments:
(bool) $add_media_query
(text) $code_block

The wp-hide/module/general_css_combine/add_media_query filter allows you to control whether the @media queries are included in the processed CSS assets by the WP Hide plugin. By default, the plugin adds @media data to the combined CSS files. However, using this filter, you can modify this behavior based on specific conditions.

The following example demonstrates how to use the wp-hide/module/general_css_combine/add_media_query filter. It checks if the $code_block contains a specific string related to media loading and disables the addition of @media queries if the condition is met.


    add_filter ( 'wp-hide/module/general_css_combine/add_media_query', 'wp_hide_module_general_css_combine_add_media_query', 10, 2 );
    function wp_hide_module_general_css_combine_add_media_query ( $add_media_query, $code_block )
        {
            if ( $add_media_query   &&  stripos( $code_block, 'onload="this.onload=null;this.media=\'all\'') )
                $add_media_query    =   FALSE; 
            
            return $add_media_query;   
        }

Explanation

  1. Filter Hook: The add_filter function is used to attach the wp_hide_module_general_css_combine_add_media_query function to the wp-hide/module/general_css_combine/add_media_query filter hook.
  2. Function Definition: The wp_hide_module_general_css_combine_add_media_query function takes two parameters: $add_media_query and $code_block.
  3. Condition Check: Inside the function, a condition checks if $add_media_query is TRUE and if $code_block contains the string onload="this.onload=null;this.media='all'.
  4. Modify $add_media_query: If the condition is met, $add_media_query is set to FALSE, indicating that @media queries should not be added.
  5. Return Value: The modified value of $add_media_query is returned.

Usage Context

This filter can be particularly useful when you want to exclude @media queries from the combined CSS based on specific criteria. For instance, you might have certain CSS blocks that should not have @media queries added due to specific loading behaviors or optimizations.

By implementing this filter, you gain finer control over the CSS combining process in the WP Hide plugin, allowing for more customized and optimized handling of your site’s stylesheets.

Important!
The above code should be placed within wp-content/mu-plugins/ directory.

The above filter is included in the PostProcessing Replacements component.

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn
Scroll to top