Created
June 16, 2026 04:39
-
-
Save BruceMcKinnon/8ea475e6b683952379ffbac14c1a11dd to your computer and use it in GitHub Desktop.
Use resuable blocks in templates
This file contains hidden or 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
| // | |
| // Reusable blocks | |
| // | |
| add_action( 'admin_menu', 'reusable_blocks_link_wp_admin' ); | |
| function reusable_blocks_link_wp_admin() { | |
| add_menu_page( 'linked_url', 'Reusable Blocks', 'read', 'edit.php?post_type=wp_block', '', 'dashicons-editor-table', 22 ); | |
| } | |
| // | |
| // Parse and modify URLs in a block of HTML | |
| // | |
| function htmlLinkParser( $html, $debug = false ) { | |
| // This new list will html be a set of <p> | |
| $retHtml = ''; | |
| try { | |
| $dom = new \DOMDocument(); | |
| $dom->loadXML($html); | |
| if ( $debug ) { | |
| fb_log('dom: '.print_r($dom,true)); | |
| } | |
| // Loop over all links within the `<body>` element | |
| if ( $dom->getElementsByTagName('ul')[0] ) { | |
| foreach($dom->getElementsByTagName('ul')[0]->getElementsByTagName('li') as $list_item) { | |
| // Work on each list item | |
| // Get the URL | |
| foreach($list_item->getElementsByTagName('a') as $link) { | |
| $oldLink = $link->getAttribute('href'); | |
| $newLink = get_controlled_file_dnl( $oldLink ); | |
| } | |
| $retHtml .= '<p><input type="button" onclick="window.location.href=\'' . $newLink .'\';" value="'.$list_item->nodeValue.'"/></p>'; | |
| } | |
| } | |
| } catch (Exception $ex) { | |
| $retHtml = $html; | |
| } | |
| return $retHtml; | |
| } | |
| // | |
| // Get a guteberg reusable block - https://stackoverflow.com/questions/55382244/get-reusable-block-in-php | |
| // | |
| function get_reusable_block($block, $block_id = 0, $apply_link_parser = true ) { | |
| $retHtml = ''; | |
| //fb_log('get_reusable_block:'.$block.' | '.$block_id); | |
| // important to have post_type as 'wp_block' since it is a reusable block | |
| if ( $block_id > 0 ) { | |
| $args = array( | |
| 'post_type' => 'wp_block', | |
| 'p' => $block_id, | |
| 'post_status' => 'published', | |
| 'posts_per_page' => 1, | |
| ); | |
| } else { | |
| $args = array( | |
| 'post_type' => 'wp_block', | |
| 'title' => $block, | |
| 'post_status' => 'published', | |
| 'posts_per_page' => 1, | |
| ); | |
| } | |
| $query = new WP_Query( $args ); | |
| if (!empty($query->post)) { | |
| $reusable_block = $query->post; | |
| $reusable_block_content = apply_filters('the_content', $reusable_block->post_content); | |
| if ( $apply_link_parser ) { | |
| $retHtml = htmlLinkParser( $reusable_block_content ); | |
| } else { | |
| $retHtml = $reusable_block_content; | |
| } | |
| } | |
| wp_reset_query(); | |
| return $retHtml; | |
| } | |
| // | |
| // end Reusable blocks | |
| // |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment