Created
December 6, 2016 13:58
-
-
Save hirejordansmith/b1cd7b48973b39e6e065efe9e88cc76f to your computer and use it in GitHub Desktop.
Insert Content in WordPress after a certain amount of characters or words
This file contains 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 | |
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment