Halloween Sale 15% off Ends in See the plans
PHP

Custom CSS for Specific Blocks, Loaded Only Where Used

Lets you write extra CSS for individual block types (for example tables or quotes) that WordPress prints only on pages that actually contain those blocks.

Don’t have WPIDE yet? Install it on your site

Code

// Block name => CSS for that block.
$wpide_block_css = array(
    'core/table' => '.wp-block-table table{font-size:.95em;}.wp-block-table th{text-align:left;}',
    'core/quote' => '.wp-block-quote{border-left-width:4px;padding-left:1.25em;font-style:italic;}',
    'core/code'  => '.wp-block-code{border-radius:6px;padding:1em 1.25em;overflow-x:auto;}',
);

add_action( 'init', function () use ( $wpide_block_css ) {
    if ( ! function_exists( 'wp_enqueue_block_style' ) ) {
        return;
    }

    foreach ( $wpide_block_css as $block => $css ) {
        $handle = 'wpide-block-' . sanitize_key( str_replace( '/', '-', $block ) );

        wp_register_style( $handle, false, array(), '1.0' );
        wp_add_inline_style( $handle, wp_strip_all_tags( $css ) );
        wp_enqueue_block_style( $block, array( 'handle' => $handle ) );
    }
} );

Notes

Works best with block themes, or classic themes that load block styles separately. Edit the CSS per block name.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.