Hi there,
you would need to use a filter hook – for example this snippet will add custom HTML element before the first H2 on all single posts:
add_filter( 'the_content', 'db_prefix_first_h2_custom_html' );
function db_prefix_first_h2_custom_html( $content ) {
$html = '<div>My custom HTML before first H2</div>';
if ( is_single() && ! is_admin() ) {
return prefix_insert_after_paragraph( $html, 1, $content );
}
return $content;
}
function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
$closing_p = '<h2>';
$paragraphs = explode( $closing_p, $content );
if ($paragraphs) {
$paragraphs[0] = $paragraphs[0].$insertion;
}
return implode( '<h2>', $paragraphs );
}