Last active
June 21, 2018 14:27
-
-
Save rxnlabs/5feff77685a8d32f232c to your computer and use it in GitHub Desktop.
WordPress - WP Get the excerpt starting at the end of a sentence. Get an excerpt of a paragraph and show the "Read More" link after the end of a sentence. Can be modified to use in other PHP apps.
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 | |
/** | |
* Get the excerpt of a paragraph or string of long text. | |
* | |
* Get the shortened version of text for the wyisiwig. Use with custom wysiwyg (e.g. Advanced Custom Fields). This will print the paragraph and breaks tag but will strip all other tags | |
* | |
* @since 2014-11-06 | |
* @version 2016-03-09 | |
* @author De'Yonte W.<[email protected]> | |
* @link http://stackoverflow.com/questions/11758465/preg-split-how-to-include-the-split-delimiter-in-results#answer-11758732 | |
* @param string $full_text The text you want to grab the excerpt from | |
* @param int $character_limit The maximum number of characters you want shown in the excerpt | |
* @return string Excerpted version of text with "read more" link at the end of the sentence | |
*/ | |
function get_the_excerpt_formatted( $full_text = '', $read_more_text = 'Read More...', $read_more_link = '', $character_limit = 55 ) { | |
global $post; | |
if ( empty($read_more_text) ) { | |
$read_more_text = 'Read More...'; | |
} | |
if ( empty($full_text) ) { | |
$full_text = $post->post_content; | |
} | |
$character_limit = (int)$character_limit; | |
if ( empty($character_limit) ) { | |
$character_limit = 55; | |
} | |
if ( empty($read_more_link) ) { | |
$read_more_link = get_permalink( $post->ID ); | |
} | |
$excerpt = strip_shortcodes( $full_text ); | |
$excerpt = apply_filters('the_content', $excerpt); | |
$excerpt = str_replace(']]>', ']]>', $excerpt); | |
if ( class_exists( 'DOMDocument' ) ) { | |
$dom = new DOMDocument(); | |
libxml_use_internal_errors(true); | |
$allowed_tags = array( 'p', 'strong', 'hr', 'br', 'a', 'em', 'i', 'b' ); | |
// List the attributes you want to allow here | |
$allowed_attrs = array ( 'class', 'id', 'style', 'href', 'title' ); | |
if ( $dom->loadHTML( $excerpt, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD ) ) { | |
$excerpt = $dom->saveHTML(); | |
!d($excerpt); | |
foreach ( $dom->getElementsByTagName("*") as $tag ){ | |
if ( ! in_array( $tag->tagName, $allowed_tags ) ) { | |
$tag->parentNode->removeChild($tag); | |
} else { | |
foreach ( $tag->attributes as $attr ) { | |
// only allow certain attributes and ARIA attributes | |
if ( ! in_array( $attr->nodeName, $allowed_attrs ) || substr( $attr->nodeName, 0, 4 ) === 'aria' ) { | |
$tag->removeAttribute( $attr->nodeName ); | |
} | |
} | |
} | |
} | |
$excerpt = $dom->saveHTML(); | |
} | |
$excerpt = $dom->saveHTML(); | |
libxml_use_internal_errors( false ); | |
} else { | |
$excerpt = strip_tags( $excerpt, '<p><br><hr><strong>' ); | |
} | |
$character_count = strlen($excerpt); | |
if( $character_count > $character_limit ){ | |
$new_excerpt = ''; | |
$excerpt_character_count = 0; | |
// break apart the excerpt to an array using one of the end of line punctuation marks but keep the punctuation marks | |
$paragraphs = preg_split('/([^\.|\?|!]+[\.|\?|!]+)/s', $excerpt, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); | |
$index = 0; | |
if( count($paragraphs) > 0 ){ | |
do{ | |
$new_excerpt .= $paragraphs[$index]; | |
$excerpt_character_count = strlen($new_excerpt); | |
$index++; | |
}while( $excerpt_character_count < $character_limit ); | |
} | |
$excerpt = $new_excerpt; | |
} | |
$excerpt_more = apply_filters('excerpt_more', ' ' . $read_more_text); | |
$excerpt_more = sprintf('<a class="read-more" href="%s">%s</a>', $read_more_link, __($excerpt_more)); | |
$excerpt = $excerpt . $excerpt_more; | |
return $excerpt; | |
} | |
add_filter('excerpt_format', 'get_the_excerpt_formatted', 10, 4); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment