Repository navigation
Patterns Custom Css Filters
This document describes the filter hooks available for customizing the Custom CSS output in DesignSetGo.
The Custom CSS Renderer provides five filter hooks that allow developers to customize how custom CSS is processed and output. These hooks enable use cases such as:
- CSS minification and optimization
- Custom preprocessing/postprocessing
- Integration with external CSS systems
- Conditional CSS loading
- Custom class naming schemes
- Additional sanitization rules
Description: Filters individual block CSS during collection (before any processing).
Parameters:
-
$custom_css(string) - The raw CSS from the block attribute -
$block_name(string) - The full block name (e.g., 'core/paragraph', 'designsetgo/stack') -
$block(array) - The complete block data array
Returns: string|null - Modified CSS string, or null/empty to skip this block's CSS entirely
Use Cases:
- Modify CSS per block type
- Add preprocessing
- Conditionally skip CSS for specific blocks
- Add block-specific transformations
add_filter( 'designsetgo/custom_css_block', function( $css, $block_name ) {
// Don't output custom CSS for paragraph blocks
if ( 'core/paragraph' === $block_name ) {
return null;
}
return $css;
}, 10, 2 );add_filter( 'designsetgo/custom_css_block', function( $css, $block_name, $block ) {
// Replace custom variables before processing
$css = str_replace( '{{primary-color}}', '#007cba', $css );
$css = str_replace( '{{spacing}}', '1rem', $css );
return $css;
}, 10, 3 );add_filter( 'designsetgo/custom_css_block', function( $css, $block_name ) {
// Only apply custom CSS on frontend, not in admin
if ( is_admin() ) {
return null;
}
return $css;
}, 10, 2 );Description: Filters the CSS class name generated for custom CSS.
Parameters:
-
$class_name(string) - The generated class name (e.g., 'dsgo-custom-css-abc123') -
$hash(string) - The hash generated from CSS content and block name -
$block_name(string) - The full block name -
$custom_css(string) - The CSS content being processed
Returns: string - Modified class name
Use Cases:
- Custom class naming schemes
- Integration with existing CSS systems
- Namespacing for multi-site setups
- Debugging/logging
add_filter( 'designsetgo/custom_css_class_name', function( $class_name, $hash ) {
// Use your own prefix instead of 'dsgo-custom-css-'
return 'my-site-css-' . $hash;
}, 10, 2 );add_filter( 'designsetgo/custom_css_class_name', function( $class_name, $hash, $block_name ) {
// Extract block type (e.g., 'stack' from 'designsetgo/stack')
$block_type = str_replace( 'designsetgo/', '', $block_name );
$block_type = str_replace( 'core/', '', $block_type );
return 'custom-' . $block_type . '-' . $hash;
}, 10, 3 );add_filter( 'designsetgo/custom_css_class_name', function( $class_name, $hash ) {
$site_id = get_current_blog_id();
return 'site-' . $site_id . '-css-' . $hash;
}, 10, 2 );Description: Filters CSS after default security sanitization.
Parameters:
-
$css(string) - The sanitized CSS after default security processing -
$original_css(string) - The original CSS before sanitization
Returns: string - Further sanitized or modified CSS
Use Cases:
- Additional sanitization rules
- Allowlist specific CSS features
- Remove vendor prefixes
- Custom security policies
add_filter( 'designsetgo/custom_css_sanitize', function( $css, $original_css ) {
// Remove !important declarations
$css = preg_replace( '/\s*!important\s*/i', '', $css );
return $css;
}, 10, 2 );add_filter( 'designsetgo/custom_css_sanitize', function( $css ) {
// Only allow specific CSS properties
$allowed_properties = ['color', 'background-color', 'font-size', 'margin', 'padding'];
// This is a simplified example - you'd need more robust parsing
foreach ( $allowed_properties as $prop ) {
// Keep allowed properties
}
return $css;
} );add_filter( 'designsetgo/custom_css_sanitize', function( $css ) {
// Remove vendor prefixes (-webkit-, -moz-, -ms-, -o-)
$css = preg_replace( '/-(?:webkit|moz|ms|o)-[a-z-]+\s*:/i', '', $css );
return $css;
} );Note: While this filter provides two parameters ($css and $original_css), you can declare only the parameters you need in your callback. Examples 2 and 3 demonstrate accepting just $css, which is sufficient when you don't need access to the original unsanitized CSS. If you need both parameters, use function( $css, $original_css ) and specify 10, 2 for the $accepted_args parameter (as shown in Example 1).
Description: Filters processed CSS for a single block (after selector replacement and sanitization).
Parameters:
-
$sanitized_css(string) - The processed and sanitized CSS -
$class_name(string) - The CSS class name being used -
$block_name(string) - The full block name -
$hash(string) - The hash identifier for this CSS block
Returns: string - Modified CSS string
Use Cases:
- Minification
- Vendor prefixing
- Post-processing
- CSS optimization
add_filter( 'designsetgo/custom_css_processed', function( $css ) {
// Add vendor prefixes for flexbox (simplified example)
$css = str_replace( 'display: flex;', 'display: -webkit-flex; display: flex;', $css );
return $css;
} );add_filter( 'designsetgo/custom_css_processed', function( $css, $class_name, $block_name ) {
// Add specific optimizations for Stack blocks
if ( 'designsetgo/stack' === $block_name ) {
// Add GPU acceleration hint
$css .= "\n." . $class_name . " { will-change: transform; }";
}
return $css;
}, 10, 3 );add_filter( 'designsetgo/custom_css_processed', function( $css ) {
// Remove comments
$css = preg_replace( '/\/\*.*?\*\//s', '', $css );
// Remove whitespace
$css = preg_replace( '/\s+/', ' ', $css );
return trim( $css );
} );Description: Filters the complete CSS output before rendering to the page.
Parameters:
-
$output_css(string) - The complete CSS string to be output -
$custom_css_data(array) - All collected CSS data (hash => [css, class, block])
Returns: string - Modified CSS output (return empty string to prevent inline output)
Use Cases:
- Global minification
- External file generation
- Caching
- CDN integration
- Complete output control
add_filter( 'designsetgo/custom_css_output', function( $css ) {
// Remove all whitespace and newlines
$css = preg_replace( '/\s+/', ' ', $css );
$css = str_replace( '; }', '}', $css );
return trim( $css );
} );add_filter( 'designsetgo/custom_css_output', function( $css, $css_data ) {
// Generate filename based on content hash
$hash = md5( $css );
$upload_dir = wp_upload_dir();
$file_path = $upload_dir['basedir'] . '/designsetgo-css-' . $hash . '.css';
$file_url = $upload_dir['baseurl'] . '/designsetgo-css-' . $hash . '.css';
// Write CSS to file if it doesn't exist
if ( ! file_exists( $file_path ) ) {
file_put_contents( $file_path, $css );
}
// Enqueue the external file
wp_enqueue_style( 'designsetgo-custom-css', $file_url, [], $hash );
// Return empty string to prevent inline output
return '';
}, 10, 2 );add_filter( 'designsetgo/custom_css_output', function( $css ) {
// Only output CSS on specific pages
if ( ! is_singular() ) {
return ''; // No output on archive pages
}
return $css;
} );add_filter( 'designsetgo/custom_css_output', function( $css ) {
// Add CSS containment for better performance
$css = '/* Performance optimized CSS */' . "\n" . $css;
return $css;
} );Here's a complete example showing how to use multiple filters together for a production optimization setup:
/**
* DesignSetGo Custom CSS Optimization
*
* This example shows how to:
* 1. Skip CSS for specific blocks
* 2. Preprocess variables
* 3. Minify output
* 4. Save to external file with caching
*/
// 1. Skip CSS for blocks that don't need it
add_filter( 'designsetgo/custom_css_block', function( $css, $block_name ) {
$skip_blocks = ['core/paragraph', 'core/heading'];
if ( in_array( $block_name, $skip_blocks ) ) {
return null;
}
return $css;
}, 10, 2 );
// 2. Preprocess custom variables
add_filter( 'designsetgo/custom_css_block', function( $css ) {
$theme_colors = [
'{{primary}}' => get_theme_mod( 'primary_color', '#007cba' ),
'{{secondary}}' => get_theme_mod( 'secondary_color', '#545454' ),
];
return str_replace( array_keys( $theme_colors ), array_values( $theme_colors ), $css );
}, 20 );
// 3. Add vendor prefixes
add_filter( 'designsetgo/custom_css_processed', function( $css ) {
$prefixes = [
'display: flex;' => 'display: -webkit-flex; display: flex;',
'flex-direction:' => '-webkit-flex-direction:',
];
return str_replace( array_keys( $prefixes ), array_values( $prefixes ), $css );
} );
// 4. Save to external file with versioning
add_filter( 'designsetgo/custom_css_output', function( $css ) {
if ( empty( $css ) ) {
return '';
}
// Minify
$css = preg_replace( '/\s+/', ' ', $css );
// Generate hash for cache busting
$hash = md5( $css );
$upload_dir = wp_upload_dir();
$file_path = $upload_dir['basedir'] . '/custom-css/dsgo-' . $hash . '.css';
$file_url = $upload_dir['baseurl'] . '/custom-css/dsgo-' . $hash . '.css';
// Ensure directory exists
wp_mkdir_p( dirname( $file_path ) );
// Write file if it doesn't exist
if ( ! file_exists( $file_path ) ) {
file_put_contents( $file_path, $css );
}
// Enqueue external file
wp_enqueue_style( 'designsetgo-custom', $file_url, [], $hash );
// Prevent inline output
return '';
} );- Performance: Use filters efficiently - avoid heavy processing in filters that run per-block
-
Security: When using
designsetgo/custom_css_sanitize, always maintain security standards - Caching: Consider caching filtered results to avoid repeated processing
- Priority: Use appropriate filter priorities to ensure correct order of operations
- Testing: Test filters thoroughly across different blocks and scenarios
- Preprocessing (variables, conditionals): Priority 10-20
- Sanitization additions: Priority 10
- Postprocessing (prefixes, optimization): Priority 20-30
- Output handling (files, caching): Priority 50+
- 1.2.0: Initial release of filter hooks
For questions or issues related to these filters, please open an issue on GitHub.
Auto-generated from
docs/patterns/CUSTOM-CSS-FILTERS.md. To update, edit the source file and changes will sync on next push to main.
- Accordion
- Advanced Heading
- Blobs
- Breadcrumbs
- Card
- Chart
- Comparison Table
- Countdown Timer
- Counter Group
- Divider
- Dynamic Image
- Fifty Fifty
- Flip Card
- Form Builder
- Grid
- Hotspot
- Icon
- Icon Button
- Icon List
- Image Accordion
- Map
- Modal
- Modal Api Reference
- Modal Auto Triggers
- Modal Fse Compatibility
- Modal Gallery Navigation
- Modal Trigger
- Pill
- Product Categories Grid
- Product Showcase Hero
- Progress Bar
- Query
- Query Filter
- Query Group Header
- Query No Results
- Query Pagination
- Query Results
- Row
- Scroll Accordion
- Scroll Marquee
- Scroll Slides
- Section
- Section Divider
- Slider
- Star Rating
- Sticky Sections
- Table Of Contents
- Tabs
- Text Path
- Timeline
- Background Video
- Block Animations
- Clickable Group
- Conditional Visibility
- Custom Css
- Draft Mode
- Dynamic Tags
- Expanding Background
- Grid Mobile Order
- Grid Span
- Hover Effects
- Max Width
- Responsive Visibility
- Reveal Control
- Scroll Parallax
- Sticky Header
- Style Binding
- Svg Patterns
- Text Alignment Inheritance
- Text Reveal
- Abilities Api
- Abilities Api Guide
- Block Bindings
- Draft Mode Api
- Form Webhooks
- Interactive Blocks
- Markdown Content Negotiation
- Rest Api Reference
- Wp Cli Reference
- Ai Assisted Development
- Best Practices Summary
- Block Controls Organization
- Block Development Best Practices Comprehensive
- Block Exclusion Guide
- Deploying To Wordpress Org
- Design System
- 2026 04 16 Blocks Editor Ux Design
- 2026 04 17 Theme 3 Inspector Ia
- 2026 04 21 Query Block Onboarding
- 2026 04 21 Query Capable Layout Blocks Design
- 2026 04 27 Block Roadmap Ideas
- 2026 07 01 Shape Divider Theme Inheritance
- 2026 07 02 Icon Block Dynamic Render Pilot
- 2026 07 06 Section Divider Block Design
- 2026 07 06 Section Style Variations Fse Design
- 2026 07 06 Section Styles Editor Preview
- 2026 07 07 Row Grid Overlay Hover Parity Design
- 2026 07 21 Conditional And Dup Label Sourcing
- 2026 07 23 Theme Block Type Animation Defaults
- 2026 08 16 Chart Block
- 2026 08 16 Greenshift Gap Roadmap
- 2026 08 17 Woocommerce Surface
- 2026 08 24 Loop Carousel
- 2026 08 24 Star Rating Block
- 2026 09 17 Abilities Drift Prevention
- 2026 09 17 Abilities Drift Prevention Implementation
- 2026 09 29 Existing User Regression Fixes
- 2026 09 29 Release Preflight Fixes
- Readme
- 2026 07 02 Block Authorability Audit
- 2026 09 29 Regression Fixes
- 2026 09 29 Release 2.9.0
- 2026 09 29 Release Fixes