Skip to content

Instantly share code, notes, and snippets.

@seothemes
Created January 3, 2023 04:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seothemes/ba65f797d61e76820cba1567b5a5eb8b to your computer and use it in GitHub Desktop.
Save seothemes/ba65f797d61e76820cba1567b5a5eb8b to your computer and use it in GitHub Desktop.
Insert custom content after second paragraph of content
<?php
declare( strict_types=1 );
namespace Blockify\Test;
use function add_filter;
use function explode;
use function implode;
use function is_singular;
add_filter( 'the_content', __NAMESPACE__ . '\\insert_after_second_paragraph' );
/**
* Insert custom content after second paragraph of post content.
*
* @param string $content The content.
*
* @return string
*/
function insert_after_second_paragraph( string $content ): string {
if ( ! is_singular( [ 'post', 'page' ] ) ) {
return $content;
}
// Change this to the paragraph number you want to insert the content after.
$count = 2;
// Change this to your own content.
$custom_content = '<br/><strong>Custom Content</strong>';
$explode = explode( "</p>", $content );
$explode[ $count ] = $custom_content . $explode[ $count ];
return implode( '</p>', $explode );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment