Have you tried using parse_blocks()
(pass in get_the_content()
)? That returns an array of all the blocks in your content. From there you can pull out block names and id attributes using an array map or foreach.
Assuming you only want to pull anchor tags from the headings, you could do something like:
$blocks = parse_blocks( get_the_content() );
$block_ids = array();
function get_block_id( $block ) {
if ( $block['blockName'] !== 'core/heading' ) return;
$block_html = $block['innerHTML'];
$id_start = strpos( $block_html, 'id="' ) + 4;
if ( $id_start === false ) return;
$id_end = strpos( $block_html, '"', $id_start );
$block_id = substr( $block_html, $id_start, $id_end - $id_start );
return $block_id;
}
foreach( $blocks as $block ) {
$block_id = get_block_id( $block );
if ( $block_id ) array_push ( $block_ids, $block_id );
}
Related Posts:
- apply_filters(‘the_content’, $content) vs do_shortcode($content)
- How would one modify the filtering Gutenberg applies to pasted content?
- Gutenberg: Is there a way to know if current block is inside InnerBlocks?
- the_content and is_main_query
- How to add a custom CSS class to core blocks in Gutenberg editor?
- How to appending to the_content using add_filter with custom post type?
- How to wrap an element around an iframe or embed in content automatically?
- WordPress Internal @ Mentions
- Where to hook into post content?
- Insert Custom HTML After Shortcode
- How can I extend the Gutenberg table block transform to allow colspans/rowspans on pasted table elements?
- Add whitespace between Chinese and other letters
- Hook added to the_content seems to be called multiple times
- Changing text within the Block Editor
- Get excerpt from $post->post_content
- Add Class to Specific Paragraph of the_content()
- add_filter for specific pages
- Remove left alignment option in core/image block
- What params are available with the_content filter?
- Using variable from one filter in another filter
- apply_filters(‘the_content’, $content) alternative
- What effect does the_content filter have on performance?
- How can I display image metadata?
- Add post/page ID to inserted links within the_content
- How to apply content filter permanently?
- How to prepend text to the_content, but after img/shortcode
- Format content value from DB outside of WordPress filters
- What is the filter hook for custom fields content?
- Changing Gutenberg / WP block editor width only on pages, not posts or other taxonomies
- What functions are included in apply_filter(‘the_content’)
- Return array of images after content
- Nested calls the the_content filter
- How do I replace a render_callback function for a block?
- How can I filter block registration based on post-type? (Block alignment settings)
- Gutenberg disable the “block” tab in right sidebar
- Editing
- the_content filter – checking the post
- Apply the_content filter, but prevent other hooked actions
- Wrap h1-h6 in a div
- How to change the order (priority) of registered filters (or actions) (e.g. for the_content)?
- Function the_content not working
- How to change the order of HTML output of a core block?
- ‘the_content’ filter removing html tags from post content
- add_filter(‘the_content’, ‘…’) stops pagination from working
- Post Content, Special Characters and Filters
- Gutenberg – Add align controls to a custom block
- Can the wordpress color palettes by changed through Javascript?
- Filter on the_content doesn’t update the content being searched via register_rest_route
- Inline Styles on all native blocks
- How to link the whole list item in Gutenberg’s latest posts block?
- add_filter(allowed_block_types) -> adding core/paragraph bugs Gutenberg editor
- Set default terms for new posts / CPTs
- How to hide Ads in between posts on AMP? [closed]
- Add content as soon starts
- Remove Content Filter
- tag removed not using a filter
- Replace audio links with jplayer using the_content filter
- Add a div of content within the_content after a certain block
- Detect when gutenberg editor title is available in Dom after editor load
- Gutenberg Block – Post Featured Image Filter Hook
- How do you get specific tags from the_content?
- Can i use multiple ‘the_content’ filters?
- Set default value for “Additional CSS class(es)” for specific block types
- get_post() containing gallery is outputting an unmatched closing div at the end of the content
- Load Posts’ individual body content on index starting at char 200 of each post
- Excerpt is being added above the content, but it should be after it
- What is the earliest hook to modify post content?
- Filter content for get_the_content()
- How to hide/remove GhostKit component panel in gutenberg block inspector
- Filter for author list in gutenberg core editor
- Why is overwriting $GLOBALS killing the_content()?
- add_filter the_content doesn’t work
- How can I get the default content of WordPress post?
- alternative to the_content filter
- add_filter to the_content after apply_filters
- How to stop DOMDocument destroying embeds?
- How to optionally append components to the output of a gutenberg block?
- Scanning for custom embed and prefetching
- Problem width wp_insert_post_data and Gutenberg block editor
- the_content getting current page content instead specified ID
- WordPress Gutenberg core/heading block (wp-block-heading)
- Changing the category for existing Gutenberg blocks
- get_post() with filters applied
- How do I check if I can use the allowed_block_types filter?
- How to add lazy field in content endpoint using Gutenberg blocks
- How to wrap pattern-matches from the_content in [wiki]-shortcode tags
- Set Microsoft Word links to open in new window/tab
- How do I isolate the reason a wordpress filter is not running?
- the_content filter not working when Jetpack activated. Any idea?
- Apply a filter str_replace on specific caracters array in the_content()
- changing size image within the content
- Can’t get content of all (19) posts – Incomplete Chunked Encoding
- Add class to all parent elements inside the_content
- filter on the_content stopped working when I updated to WP 3.6.1
- post->post_content filter
- Replace image scr with it’s surrounding href
- After adding filter to plugin’s code, post’s content doesn’t display
- How to add custom HTML attribute to reusable block div in WordPress admin
- How to change this WP logo and posts url in block editor?
- Limit total tags in the_content