/**
* Featured Image, Classic Editor
*
*/
function be_single_featured_image_classic( $content ) {
// Only run on the main post
global $wp_query;
if( 0 !== $wp_query->current_post )
return $content;
// Only run on classic editor, see next function for block editor
if( has_block( 'core/paragraph', $content ) )
return $content;
$image = get_the_post_thumbnail( get_the_ID(), 'large' );
if( !empty( $image ) )
$content = ea_insert_after_paragraph( $image, 2, $content );
return $content;
}
add_filter( 'the_content', 'be_single_featured_image_classic', 5 );
/**
* Featured Image, Block Editor
*
*/
function be_single_featured_image_block( $content, $block ) {
// Only run on the main post
global $wp_query;
if( 0 !== $wp_query->current_post )
return $content;
if( 'core/paragraph' !== $block['blockName'] )
return $content;
global $be_current_paragraph;
if( empty( $be_current_paragraph ) )
$be_current_paragraph = 1;
else
$be_current_paragraph++;
if( 2 !== $be_current_paragraph )
return $content;
$image = get_the_post_thumbnail( get_the_ID(), 'large' );
if( !empty( $image ) )
$content .= '<div class="wp-block-image">' . $image . '</div>';
return $content;
}
add_filter( 'render_block', 'be_single_featured_image_block', 10, 2 );
/**
* Insert after Paragraph
* When running on the_content, use priority > 20 so it doesn't affect oEmbed
*
* @param string $insertion
* @param int $paragraph_id
* @param string $content
* @return string $modified_content
*/
function ea_insert_after_paragraph( $insertion, $paragraph_id, $content, $include_at_end = true ) {
$closing_p = '</p>';
$paragraphs = explode( $closing_p, wpautop( $content ) );
foreach ($paragraphs as $index => $paragraph) {
if ( trim( $paragraph ) ) {
$paragraphs[$index] .= $closing_p;
}
if ( $paragraph_id == $index + 1 ) {
$paragraphs[$index] .= $insertion;
$inserted = true;
}
}
if( ! $inserted && $include_at_end )
$paragraphs[] = $insertion;
return implode( '', $paragraphs );
}