get_registered( $parsed_block['blockName'] ); if ( ! block_has_support( $block_type, 'customCSS', true ) ) { return $parsed_block; } $custom_css = trim( $parsed_block['attrs']['style']['css'] ?? '' ); if ( empty( $custom_css ) ) { return $parsed_block; } // Validate CSS doesn't contain HTML markup (same validation as global styles REST API). if ( preg_match( '#next_tag() ) { $tags->add_class( 'has-custom-css' ); $tags->add_class( $matches[0] ); } return $tags->get_updated_html(); } // Remove core filters and action to avoid rendering duplicate custom CSS styles. if ( function_exists( 'wp_render_custom_css_class_name' ) ) { remove_filter( 'render_block', 'wp_render_custom_css_class_name' ); } if ( function_exists( 'wp_render_custom_css_support_styles' ) ) { remove_filter( 'render_block_data', 'wp_render_custom_css_support_styles' ); } if ( function_exists( 'wp_enqueue_block_custom_css' ) ) { remove_action( 'wp_enqueue_scripts', 'wp_enqueue_block_custom_css' ); } // Add Gutenberg filters and action. add_filter( 'render_block', 'gutenberg_render_custom_css_class_name', 10, 2 ); add_filter( 'render_block_data', 'gutenberg_render_custom_css_support_styles', 10, 1 ); add_action( 'wp_enqueue_scripts', 'gutenberg_enqueue_block_custom_css', 1 ); /** * Registers the style block attribute for block types that support it. * * @param WP_Block_Type $block_type Block Type. */ function gutenberg_register_custom_css_support( $block_type ) { // Setup attributes and styles within that if needed. if ( ! $block_type->attributes ) { $block_type->attributes = array(); } // Check for existing style attribute definition e.g. from block.json. if ( array_key_exists( 'style', $block_type->attributes ) ) { return; } $has_custom_css_support = block_has_support( $block_type, array( 'customCSS' ), true ); if ( $has_custom_css_support ) { $block_type->attributes['style'] = array( 'type' => 'object', ); } } // Register the block support. WP_Block_Supports::get_instance()->register( 'custom-css', array( 'register_attribute' => 'gutenberg_register_custom_css_support', ) );