1

Is there a way to know if a page is using Gutenberg or not?

The use case is migrating an old post to Gutenberg and letting crafted pages just render themselves with blocks but add some wrapping around old pages which probably won't be migrated since they're just text (Privacy policy and those). They need to coexist at least for a while.

A block editor page can usually be just rendered with

<?php

get_header();

while (have_posts()) :
  the_post();
  the_content();
endwhile;

get_footer();

While I have that handled for the homepage using a p front-page.php template, I'd like to have a generic template that can handle both types so I don't have to go slug by slug.

4
  • post content is just post content, technically a legacy post does use blocks, it uses a single classic block, eitherway your block content should have a wrapper tag around it anyway, e.g. an <article> tag, otherwise where is the post_class() function going to go? Or the ID? How will search engines and social networks know which parts of the page are the main content vs the header/footer? What will screen readers do? Note that it's also possible for a posts content to be a mixture of the two. Eitherway you shouldn't need to conditionally wrap the post content, and you shouldn't want to
    – Tom J Nowell
    Commented Dec 7, 2020 at 13:46
  • I don't think that's true all the time. If you're building a home page which is a collection of articles you don't want an article wrapper around that. That was an over-simplistic code but the reasoning still stands IMHO. Commented Dec 7, 2020 at 15:46
  • True an <article> or <aside> might not make sense, but you would still want a <section> or a <main> tag, or even a <div>, which tag you use isn't the point, it's that most things be it browsers/screen readers/search engines/etc expect the main content to be wrapped and marked in some way, even if that main content is a homepage layout. This might be a div container with an ID, a semantic tag such as <main> or <section>, but you always want a container of some sort, even if it doesn't show visually. The canonical answer for the problem you're trying to solve is: don't
    – Tom J Nowell
    Commented Dec 7, 2020 at 15:51
  • There's a container in all instances, my question still makes sense I think. The pages that are "legacy" are just "static" pages which need a different treatment. Commented Dec 7, 2020 at 15:55

1 Answer 1

0

I know it's been awhile, but yes. You can check if a post uses blocks, check out has_blocks() : https://developer.wordpress.org/reference/functions/has_blocks/

I'm using it when filtering content, to see if I need to parse blocks or not.

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.