Need a simple way to add content to a post or page after a certain amount of characters or words? Check this snippet out!
<?php | |
add_filter('the_content', 'hjs_insert_content_after_chars_words'); | |
function hjs_insert_content_after_chars_words($content) { | |
// only do this if post is longer than 1000 characters | |
$enable_length = 1500; | |
// insert after the first </p> after 500 characters | |
$after_character = 1500; | |
if (is_single() && strlen($content) > $enable_length) { | |
$before_content = substr($content, 0, $after_character); | |
$after_content = substr($content, $after_character); | |
$after_content = explode('</p>', $after_content); | |
$text = 'Add Content Here'; | |
array_splice($after_content, 1, 0, $text); | |
$after_content = implode('</p>', $after_content); | |
return $before_content . $after_content; | |
} | |
else { | |
return $content; | |
} | |
} |
Leave a Reply