/*------------------------------------------------------------------------*\
Filter Content: Add our own html_blocks/ads in between blocks
\*------------------------------------------------------------------------*/
add_filter( 'the_content', 'pd_post_list_content_filter', 1 );
function pd_post_list_content_filter( $content ) {
/**
* ! Filter if not front end and not list post then return
* * https://stackoverflow.com/questions/52306320/php-insert-html-after-the-nth-occurrence-of-a-p-tag
*/
error_log( print_r( "BEFORE", true ) );
error_log( print_r( $content, true ) );
global $post;
if ( has_blocks( $post->post_content ) ) {
echo str_replace_n_after("</p>", "<a href='https://www.google.com/'>test</a>",$html , 2);
$blocks = parse_blocks( $post->post_content );
// array_splice( $blocks, 2, 0, array('<!-- wp:html --><div class="spomething"></div><!-- /wp:html -->') );
// $content = "";
// foreach ($blocks as $block) {
// error_log( print_r( $block, true ) );
// $content .= $block;
// if( !empty($block['blockName']) ){
// $counter_blocks++;
// }
// }
// error_log( print_r( "this post has " . $counter_blocks . " blocks.", true ) );
if ( $blocks[0]['blockName'] === 'core/heading' ) {
}
}
// error_log( print_r( "AFTER", true ) );
// error_log( print_r( $content, true ) );
// Returns the content.
return $content;
}
function str_replace_n_after($search, $replace, $subject, $occurrence) {
$search = preg_quote($search,'/');
return preg_replace("/((?:.*?$search){".$occurrence."})/s", "$1$replace", $subject);
}