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

get_post_galleries() returns all blocks instead of just galleries #38954

Closed
tyrann0us opened this issue Feb 21, 2022 · 2 comments
Closed

get_post_galleries() returns all blocks instead of just galleries #38954

tyrann0us opened this issue Feb 21, 2022 · 2 comments

Comments

@tyrann0us
Copy link
Contributor

tyrann0us commented Feb 21, 2022

Description

When using get_post_galleries() to get the editor galleries of a post (editor support for that function landed in WordPress 5.9, see https://core.trac.wordpress.org/ticket/43826#comment:58), the function will not only return the gallery blocks but all blocks of that post.

Note: It's likely that this is not related to Gutenberg, but an issue that should be reported and handled in WordPress Trac. Please let me know if I should do so.

Step-by-step reproduction instructions

  1. Add a new post or edit an existing one (you can even use the "Sample Page")
  2. Add a gallery block somewhere on the post
  3. Use get_post_galleries() to retrieve the gallery blocks
  4. See that the function returns all blocks

Screenshots, screen recording, code snippet

Code snippets
Use this code snippet in functions.php:

add_filter(
    'the_content', // or 'render_block', etc.
    static function (string $content): string {
        $galleries = get_post_galleries(get_the_ID());
        return $content; // set breakpoint here to inspect contents of $galleries
    }
);
Example editor content (gallery images redacted; use your own images)
<!-- wp:paragraph -->
<p>This is an example page. It's different from a blog post because it will stay in one place and will show up in your site navigation (in most themes). Most people start with an About page that introduces them to potential site visitors. It might say something like this:</p>
<!-- /wp:paragraph -->

<!-- wp:gallery {"linkTo":"none"} -->
<figure class="wp-block-gallery has-nested-images columns-default is-cropped"><!-- wp:image {"id":7,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large"><img src="[…]" alt="" class="wp-image-7"/><figcaption>OLYMPUS DIGITAL CAMERA</figcaption></figure>
<!-- /wp:image -->

<!-- wp:image {"id":8,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large"><img src="[…]" alt="" class="wp-image-8"/><figcaption>OLYMPUS DIGITAL CAMERA</figcaption></figure>
<!-- /wp:image -->

<!-- wp:image {"id":9,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large"><img src="[…]" alt="" class="wp-image-9"/></figure>
<!-- /wp:image -->

<!-- wp:image {"id":6,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large"><img src="[…]" alt="" class="wp-image-6"/></figure>
<!-- /wp:image --></figure>
<!-- /wp:gallery -->

<!-- wp:quote -->
<blockquote class="wp-block-quote"><p>Hi there! I'm a bike messenger by day, aspiring actor by night, and this is my website. I live in Los Angeles, have a great dog named Jack, and I like piña coladas. (And gettin' caught in the rain.)</p></blockquote>
<!-- /wp:quote -->

<!-- wp:paragraph -->
<p>...or something like this:</p>
<!-- /wp:paragraph -->

<!-- wp:quote -->
<blockquote class="wp-block-quote"><p>The XYZ Doohickey Company was founded in 1971, and has been providing quality doohickeys to the public ever since. Located in Gotham City, XYZ employs over 2,000 people and does all kinds of awesome things for the Gotham community.</p></blockquote>
<!-- /wp:quote -->

<!-- wp:paragraph -->
<p>As a new WordPress user, you should go to <a href="http://stock-wordpress.local/wp-admin/">your dashboard</a> to delete this page and create new pages for your content. Have fun!</p>
<!-- /wp:paragraph -->

Screenshots
get_post_galleries(get_the_ID)):
Bildschirmfoto 2022-02-21 um 13 33 02

get_post_galleries(get_the_ID), false) (return data instead of HTML):
Bildschirmfoto 2022-02-21 um 13 37 11

Environment info

  • WordPress 5.9
  • no Gutenberg plugin
  • Theme: Twenty Twenty-Two
  • Chrome 98
  • macOS 11.6.4

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

@glendaviesnz
Copy link
Contributor

Thanks for reporting @tyrann0us. This is already being looked at here and here, so going to close this one.

@tyrann0us
Copy link
Contributor Author

Thanks a lot @glendaviesnz! Before creating this issue, I was searching for existing issues in both Trac and GitHub. Either I used the wrong search terms or too much time passed after I realized the bug and before I found the time to report it 😉 🤷🏽

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants