Skip to content

Block's sourced attributes missing with parse_blocks #46029

Closed as not planned
Closed as not planned

Description

@AldoS

Description

Some attributes are missing from core blocks when I use parse_blocks. Some examples:
From core/paragraph block: content
From core/button block: text & url

Step-by-step reproduction instructions

  1. Add a core/paragraph block to a page and type something - save the page/post
  2. On console run wp.data.select('core/block-editor').getBlocks()
  3. There is an attribute called content that has just the value of the paragraph (plain text)
  4. Run parse_blocks($post->post_content); for the above page/post
  5. The content attribute is missing

Screenshots, screen recording, code snippet

No response

Environment info

  • WordPress version: 6.1.1

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

Activity

  1. added
    Needs Technical Feedback
    on Nov 24, 2022
  2. luisherranz commented on Nov 24, 2022

    @luisherranz
    Member

    The sourced attributes of static blocks are stored in the markup that is serialized (saved) to the database. WordPress does not have access to a full HTML parser in PHP, so it is not possible to extract the value of those attributes from the markup.

    If you use the Gutenberg plugin, you can use the new HTML Tag Processor API to extract some of the sourced attributes that are saved in the HTML attributes. For example, for the core/button URL, you can do something like:

    $w = new WP_HTML_Tag_Processor( $button_html );
    $w->next_tag( 'a' );
    $url = $w->get_attribute( 'href' );

    But the HTML Tag Processor is not a full HTML parser, so it can't extract the core/paragraph content or core/button text. There's no known workaround at this point, as relying on something like DOMDocument is not possible because it's not installed on every host (unless this is only for your own personal use).

    EDIT: Please note that the HTML Tag Processor is still experimental.

  3. changed the title [-]Block's attributes missing with parse_blocks[/-] [+]Block's sourced attributes missing with parse_blocks[/+] on Nov 24, 2022
  4. added
    [Type] Question
    [Status] Not Applicable
    and removed
    Needs Technical Feedback
    on Nov 24, 2022
  5. luisherranz commented on Nov 24, 2022

    @luisherranz
    Member

    @AldoS, I'm going to close this issue as it is a known problem and has no solution at the moment, but feel free to reopen it if you think it is necessary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    [Status] Not Applicable[Type] Question

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions