Created
August 11, 2016 08:38
-
-
Save jimmy89Li/2f84422b059c4cb45456496771d60d0d to your computer and use it in GitHub Desktop.
Allow HTML tags in the excerpt
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
// Custom excerpt accepting html tags | |
function wpse_allowedtags() { | |
// Allow the following HTML tags: | |
return '<br>,<p>'; | |
} | |
if ( ! function_exists( 'wpse_custom_wp_trim_excerpt' ) ) : | |
function wpse_custom_wp_trim_excerpt($wpse_excerpt) { | |
$raw_excerpt = $wpse_excerpt; | |
if ( '' == $wpse_excerpt ) { | |
$wpse_excerpt = get_the_content(''); | |
$wpse_excerpt = strip_shortcodes( $wpse_excerpt ); | |
$wpse_excerpt = apply_filters('the_content', $wpse_excerpt); | |
$wpse_excerpt = str_replace(']]>', ']]>', $wpse_excerpt); | |
$wpse_excerpt = strip_tags($wpse_excerpt, wpse_allowedtags()); /* Keep only if you want some specific tags selected in the begining. Remove if you want all the HTML tags to be allowed */ | |
return $wpse_excerpt; | |
} | |
return apply_filters('wpse_custom_wp_trim_excerpt', $wpse_excerpt, $raw_excerpt); | |
} | |
endif; | |
remove_filter('get_the_excerpt', 'wp_trim_excerpt'); | |
add_filter('get_the_excerpt', 'wpse_custom_wp_trim_excerpt'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment