2

When I make a post with a theme that supports (all) post formats, WordPress will suggest the format type it thinks best fits my post.

How does it do this? Is it possible to leverage this in, say, my own WP plugin?

enter image description here

Update: As far as I know, this is built-in functionality. As shown in the image.

3
  • 1
    As of WP 5.2.3, WordPress cannot suggest the best post format by core, as far as I know. So, I'm afraid, I'm actually a bit puzzled with your query. Do you want to know: How WP stores the post format information? Or, how you can make a plugin so that it can suggest the best post format from its content? Can you please edit your question and add some context or background about your query? Commented Sep 9, 2019 at 6:34
  • I’m also unfamiliar with this functionality. Could you elaborate on what you’re referring to? Commented Sep 9, 2019 at 6:39
  • I've added a screen capture of the suggestion appearing. It is generally spot on too. Commented Sep 9, 2019 at 6:50

1 Answer 1

2

This functionality appears to be exclusively a feature of the block editor. It's not especially clever either. All it does is check that if the post has 1 or 2 blocks (and no more), and then maps the first block against a list of appropriate post formats, like so:

switch ( name ) {
    case 'core/image':
        return 'image';
    case 'core/quote':
    case 'core/pullquote':
        return 'quote';
    case 'core/gallery':
        return 'gallery';
    case 'core/video':
    case 'core-embed/youtube':
    case 'core-embed/vimeo':
        return 'video';
    case 'core/audio':
    case 'core-embed/spotify':
    case 'core-embed/soundcloud':
        return 'audio';
}

This is part of the getSuggestedPostFormat() selector function that's part of the core/editor data store. You can see the full source in the Gutenberg repository here. This seems like a good overview of what the data stores in the block editor are.

To use this function in JS in the editor you could use:

wp.data.select('core/editor').getSuggestedPostFormat();

The logic is simple enough that it's trivial to implement in PHP with parse_blocks(), but since it's based on the blocks used it would only be useful for content built with the block editor.

function wpse_346884_get_suggested_post_format( $content ) {
    if ( ! has_blocks( $content ) ) {
        return null;
    }

    $block  = null;
    $blocks = parse_blocks( $post->post_content );

    if ( 1 === count( $blocks ) ) {
        $block = $blocks[0]['blockName'];
    }

    if ( 2 === count( $blocks ) ) {
        if ( 'core/paragraph' === $blocks[1]['blockName'] ) {
            $block = $blocks[0]['blockName'];
        } 
    }

    switch ( $block ) {
        case 'core/image':
            return 'image';
        case 'core/quote':
        case 'core/pullquote':
            return 'quote';
        case 'core/gallery':
            return 'gallery';
        case 'core/video':
        case 'core-embed/youtube':
        case 'core-embed/vimeo':
            return 'video';
        case 'core/audio':
        case 'core-embed/spotify':
        case 'core-embed/soundcloud':
            return 'audio';
    }

    return null;
}

Then you might want to implement some additional logic to make sure the theme supports the returned post type.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.