Skip to content

Instantly share code, notes, and snippets.

@dlh01
Created February 1, 2023 03:32
Show Gist options
  • Save dlh01/6f4a121496ee7c8d213af8574bd08d1a to your computer and use it in GitHub Desktop.
Save dlh01/6f4a121496ee7c8d213af8574bd08d1a to your computer and use it in GitHub Desktop.
Match Blocks example
<?php
// Find all paragraph blocks in a post:
$grafs = \Alley\WP\match_blocks( $post, [ 'name' => 'core/paragraph' ] );
// Include paragraphs in inner blocks:
$grafs = \Alley\WP\match_blocks(
$post,
[
'flatten' => true,
'name' => 'core/paragraph',
]
);
// Get all paragraphs and headings:
$blocks = \Alley\WP\match_blocks(
'<!-- wp:paragraph -->…',
[
'name' => [ 'core/heading', 'core/paragraph' ],
]
);
// Get all images credited to the Associated Press:
$images = \Alley\WP\match_blocks(
$post,
[
'attrs' => [
[
'key' => 'credit',
'value' => '/(The )?Associated Press/i',
'operator' => 'REGEX',
],
[
'key' => 'credit',
'value' => 'AP',
],
'relation' => 'OR',
],
'name' => 'core/image',
]
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment