Last active
March 2, 2023 20:34
-
-
Save steve10287/52cbfa1dd0fe19477d8de9faa540ab7b to your computer and use it in GitHub Desktop.
Parse Wordpress Gutenberg blocks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just use like this: