Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to get core-editor data in Site Editor for Templates or Template Parts #44477

Closed
ALJ opened this issue Sep 26, 2022 · 4 comments
Closed
Labels
Needs Technical Feedback Needs testing from a developer perspective. [Type] Help Request Help with setup, implementation, or "How do I?" questions.

Comments

@ALJ
Copy link

ALJ commented Sep 26, 2022

Description

I regularly use select('core/editor').getCurrentPostType() to read the type of post currently being edited in the block editor. I use this to tailor the editor and allow certain blocks for ease of editing. This works wonderfully on post, page, and any custom post type. But, this does not work in the Site Editor when editing a Template or Template Part.

I'm eager to find out if there is any way to identify exactly which Template or Template Part is currently being edited.

Without that data, I can't streamline the editor to, e.g., provide a footer-specific block in the "Footer" Template Part only. Without this, the otherwise excellent #42729 isn't as useful as custom post types are to let us simplify the experience for content editors.

Step-by-step reproduction instructions

The following examples use [email protected] with the TwentyTwentyTwo theme active. I have tried with and without [email protected] active, and the results are the same.

Baseline example on a post (i.e. working data stores providing good data)

  1. Open a post in the block editor
  2. In the browser's console, run wp.data.select('core/editor').getCurrentPostType()
    ✅ See "post" returned, which is what I expect
  3. Run wp.data.select('core/editor').getCurrentPostId()
    ✅ See 837 (or whatever ID you have) returned, which is what I expect

Non-working example in the Site Editor

  1. Open the Site Editor through Appearance > Editor
  2. Edit the site's Home template
  3. In the browser's console, run wp.data.select('core/editor').getCurrentPostType()
    ❌ See null returned, which is surprising. I expect to see wp_template (or wp_template_part when editing a Template Part).
  4. In the browser's console, run wp.data.select('core/editor').getCurrentPostId()
    ❌ See null returned, which is surprising. I expect to see twentytwentytwo//home (or twentytwentytwo//header when editing the Header Template Part).

Screenshots, screen recording, code snippet

No response

Environment info

Please confirm that you have searched existing issues in the repo.

Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

Yes

@Mamaduka Mamaduka added the Needs Technical Feedback Needs testing from a developer perspective. label Sep 27, 2022
@talldan talldan added the [Type] Help Request Help with setup, implementation, or "How do I?" questions. label Sep 30, 2022
@talldan
Copy link
Contributor

talldan commented Sep 30, 2022

@ALJ There are some different selectors in the site editor:

const templateType = wp.data.select( 'core/edit-site' ).getEditedPostType();
const templateId = wp.data.select( 'core/edit-site' ).getEditedPostId();

If you're working on a block and trying to make it work in both the post editor and site editor, that may be a bit challenging.

You can probably check for the availability of 'core/edit-site' selectors:

if ( wp.data.select( 'core/edit-site' ) ) {
    // This is the site editor
}

@ALJ
Copy link
Author

ALJ commented Sep 30, 2022

Thank you @talldan, that's exactly what I was looking for. I missed that data store in the documentation.

@merijnponzo
Copy link

thanks @talldan
Do you know if this will be supported ? I have found some links about using 'core/edit-site') with for example getBlocks which is marked as deprecated.

i was thinking to use your example to save some additional content whenever the site-editor.php is used on template parts

const templateType = wp.data.select( 'core/edit-site' ).getEditedPostType();
const templateId = wp.data.select( 'core/edit-site' ).getEditedPostId();

I checked the functions of core/edit-site

Screenshot 2023-08-04 at 14 52 05

But none the functions would return the actual post id of the wp_template_part within the wp_posts table.
Instead it returns the template parts name as an id like ponzotheme//footer

I could try to use wp-json like

http://sitename.test/wp-json/wp/v2/template-parts/ponzotheme//footer

but i suppose i would get the content of before the save instead of after the save

Any ideas on how to get the actual id and context of the template part within

document.addEventListener("DOMContentLoaded", function () {
  if (typeof wp !== "undefined") {
    if (wp.data !== undefined) {
      const { dispatch } = wp.data;
      wp.data.subscribe(() => {

        // is within site editor
        if ( wp.data.select( 'core/edit-site' ) ) {
            
          const templateType = wp.data.select( 'core/edit-site' ).getEditedPostType();
          const templateId = wp.data.select( 'core/edit-site' ).getEditedPostId();
          const templateContent = wp.data.select( 'core/edit-site' ).getEditedPostContext();
          console.log(wp.data.select( 'core/edit-site' ), templateType, templateId, templateContent)
        }

      

thanks

@merijnponzo
Copy link

merijnponzo commented Aug 9, 2023

this is also discussed within #53230

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Technical Feedback Needs testing from a developer perspective. [Type] Help Request Help with setup, implementation, or "How do I?" questions.
Projects
None yet
Development

No branches or pull requests

4 participants