Created
January 3, 2023 04:17
-
-
Save seothemes/ba65f797d61e76820cba1567b5a5eb8b to your computer and use it in GitHub Desktop.
Insert custom content after second paragraph of content
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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