Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hirejordansmith/b1cd7b48973b39e6e065efe9e88cc76f to your computer and use it in GitHub Desktop.
Save hirejordansmith/b1cd7b48973b39e6e065efe9e88cc76f to your computer and use it in GitHub Desktop.
Insert Content in WordPress after a certain amount of characters or words
<?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