Gutenberg: set_style throws "Style is not enqueued"
Version
- Carbon Fields: 3.1
- WordPress: 5.2.2
- PHP: 7.2
Expected Behavior
I add function in the carbon_fields_register_fields hook. First thing I register my style accordion-style with wp_register_style. In the Block, after the fields I set ->set_style('accordion-style'). I expect the style being loaded.
Actual Behavior
Carbon fields throws an "Style accordion-style is not enqueued." exception. This is thrown in core/Block_Container.php on line 192.
Container definition
wp_register_style(
'accordion-style',
plugins_url( "styles/accordion-style.css", __FILE__ )
);
Block::make( 'Accordion' )
->add_fields(
array(
Field::make('complex', 'items')->add_fields(
array(
Field::make( 'text', 'title' ),
Field::make( 'rich_text', 'content' )
)
)->set_collapsed( true )
)
)
->set_description( 'Simple accordion' )
->set_style( 'accordion-style' )
->set_render_callback( array( self::class, 'render' ) );
Steps to Reproduce the Problem
- Register style
- Add style to block
Comments
If I remove the wp_style_is check in Block_Container.php, the block loads correctly and the style is actually enqueued and loaded as well. If I check global $wp_styles, the the style is in there. No matter what though, wp_style_is will always return false.
After checking the documentation of wp_style_is it shows that this function by default only checks for enqueued styles. By passing registered as a second argument, it actually checks the registered styles and it works. So one could change the function to
if ( ! wp_style_is( $handle, 'registered' ) && ! wp_style_is( $handle) ) {
throw new \Exception( __( "Style '$handle' is not enqueued.", 'crb' ) );
}
Faced the same problem
I believe this pull request fix #1001 this.
This issue was fixed in the current release (v3.5.0). Please update the WordPress core and Carbon Fields to the newest version and let us know if the issues still exist.