Search code examples
wordpresscontent-management-systemwordpress-rest-apiwordpress-gutenberg

Managing Content in WordPress Best Practices


Question, and looking for advice. I'm trying to wrap my ahead around how to properly manage content in WordPress.

In the past I've always used custom fields, ACF, CMB2.

There are some nice things about Gutenberg. I can live with the html output too. Biggest benefit seems to be how quickly content can be created. No fuss.

The big BUT is this: It still stores content with HTML in the DB. REST is no help either. Do to the same reasons.

Custom fields keep content nice and clean. for easy extraction, pick up and move. It would be interesting if the block editor stored either; clean content, or could render CF in the blocks. (shortcode is limited and not a content creator savvy thing).

So in your opinion/experience, what's a good path forward (using WP)?

Thanks in advance!


Solution

  • It would be interesting if the block editor stored either; clean content, or could render CF in the blocks.

    Well, don't you worry you glowing princess! You can actually extract content from blocks as easy as 123!

    Instead of looping though the whole content, like you would usually do, you can loop through SPECIFIC blocks or block TYPES.

    Setting a class, for example, enable you to target a specific block.

    enter image description here

    Then you can loop through that block with parse_blocks().

    //...
    blocks = parse_blocks( get_the_content() );
    foreach ( $blocks as $block ) :
        if ( $block['attrs']['className'] == 'myawesomeclass' ) :
                echo wp_strip_all_tags( render_block( $block ) );
        endif;
    endforeach;
    

    But wait! that's not it my sweet random internet friend! Next part is why Gutenberg is really awesome-ish (minus bugs).

    You can use template and template_lock arguments upon registering your custom post type.

    A short example would be something along the lines of...

    <?php
    $args = [
        //...
        'template_lock' => 'all',
        'template' => [
            [ 'core/paragraph', [ 'className' => 'myawesomeclass', ] ],
            [ 'core/file' ],
            //...
        ],
        //...
    ];
    register_post_type( $post_type, $args );
    ?>
    

    Combine both... mind blown. Now, I guess you're really feeling like this.