• I am building a custom Gutenberg block to display image galleries.

    I need a way to check in the Gutenberg editor what the current template/Post type that the the user is editing.

    I have a setting called sourceFromCurrent which tells the block to get the images that will be displayed in the gallery from a meta field associated with that Post Type.

    Of course, for this to work and not cause confusion, I’ll want to hide that setting if the user is not editing a template. For instance, if the user is editing a post (not a post single template), I don’t want them to be able to select the sourceFromCurrent option, however if they are editing a Single Post, Page, etc. I do want to enable that setting.

    Is it possible to get the currently-being-edited Template?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Brian Alexander

    (@ironprogrammer)

    Hi, @oguruma ???? Would checking for wp.data.select('core/edit-site') provide enough context here? I.e. “Is the user in the site editor?” versus editing a post/page directly.

    Thread Starter oguruma

    (@oguruma)

    @ironprogrammer Thanks for the input. I don’t think that would work because I need the context of the currently-being-edited post type. For example, a user might a have meta field called post_gallery for the Post post type, and a separate meta field called image_gallery for the Projects post type.

    Gotcha — The template can be accessed from that same core/edit-site store, and checking the post ID will return the template’s slug (in format theme-slug//template-name). For instance, with the TT4 theme:

    > wp.data.select('core/edit-site').getEditedPostId();
    // Blog Home template
    "twentytwentyfour//home"
    // Single Posts template
    "twentytwentyfour//single"
    // Pages template
    "twentytwentyfour//page"

    Does that help?

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Gutenberg: Checking current template type?’ is closed to new replies.