Adding content sources for dynamic CSS generation

By default, GenerateBlocks scans your content area for its blocks and generates the necessary CSS based on your block settings.

In some cases, you may be adding blocks to other areas not within your content area. In these cases, you need to tell GenerateBlocks about the existence of these other areas, so it know to scan them.

For example, GeneratePress Elements use a custom post type to insert blocks throughout the theme. In order for GenerateBlocks to generate the necessary dynamic CSS, we need to tell GenerateBlocks about this custom post type so it can scan its content.

To do this, we use the generateblocks_do_content filter. Here’s an example:

add_filter( 'generateblocks_do_content', function( $content ) {
    $post_id = 123; // A post ID to check the content of. Can be dynamically set.

    if ( has_blocks( $post_id ) ) {
        $block_element = get_post( $post_id );

        if ( ! $block_element || 'your_post_type' !== $block_element->post_type ) {
            return $content;
        }

        if ( 'publish' !== $block_element->post_status || ! empty( $block_element->post_password ) ) {
            return $content;
        }

        $content .= $block_element->post_content;
    }

    return $content;
} );