Skip to content

Instantly share code, notes, and snippets.

@steve10287
Last active March 2, 2023 20:34
Show Gist options
  • Save steve10287/52cbfa1dd0fe19477d8de9faa540ab7b to your computer and use it in GitHub Desktop.
Save steve10287/52cbfa1dd0fe19477d8de9faa540ab7b to your computer and use it in GitHub Desktop.
Parse Wordpress Gutenberg blocks
<?php
function parseBlocks($blocks, $block_id)
{
foreach ($blocks as $block) {
if($block['blockName'] == $block_id) {
return $block['attrs']['data'];
}
if (!empty($block['innerBlocks'])) {
if ($data = parseBlocks($block['innerBlocks'], $block_id)) {
return $data;
}
}
}
return false;
}
function getBlockFromPage($post_id, $block_id)
{
$post = get_post($post_id);
if (!$post) return false;
return parseBlocks(
parse_blocks($post->post_content),
$block_id
);
}
@steve10287
Copy link
Author

Just use like this:

$block_data = getBlockFromPage($post_id, 'acf/test_name');

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