Can I have HTML tags in Excerpts?

I am using Essential Grids and I have a placeholder for Excerpts on the following page:

http://valentinafair.com/category/home/

However, the excerpts are displaying as one paragraph even though I am using <p> and
tags. Are tags not supported in excerpts?

  • Tyler Postle
    • Recruit

    Hey there,

    Hope you’re doing well today :slight_smile:

    By default WordPress strips html tags from excerpts. You can change this by adding some code to your themes functions.php file.

    function wpse_allowedtags() {
    // Add custom tags to this string
    return '<script>,<style>,<br>,<em>,<i>,<ul>,<ol>,<li>,<a>,<p>,<img>,<video>,<audio>';
    }

    if ( ! function_exists( 'wpse_custom_wp_trim_excerpt' ) ) :

    function wpse_custom_wp_trim_excerpt($wpse_excerpt) {
    global $post;
    $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()); /*IF you need to allow just certain tags. Delete if all tags are allowed */

    //Set the excerpt word count and only break after sentence is complete.
    $excerpt_word_count = 75;
    $excerpt_length = apply_filters('excerpt_length', $excerpt_word_count);
    $tokens = array();
    $excerptOutput = '';
    $count = 0;

    // Divide the string into tokens; HTML tags, or words, followed by any whitespace
    preg_match_all('/(<[^>]+>|[^<>s]+)s*/u', $wpse_excerpt, $tokens);

    foreach ($tokens[0] as $token) {

    if ($count >= $excerpt_word_count && preg_match('/[,;?.!]s*$/uS', $token)) {
    // Limit reached, continue until , ; ? . or ! occur at the end
    $excerptOutput .= trim($token);
    break;
    }

    // Add words to complete sentence
    $count++;

    // Append what's left of the token
    $excerptOutput .= $token;
    }

    $wpse_excerpt = trim(force_balance_tags($excerptOutput));

    $excerpt_end = ' <a href="'. esc_url( get_permalink() ) . '">' . '&nbsp;&raquo;&nbsp;' . sprintf(__( 'Read more about: %s &nbsp;&raquo;', 'wpse' ), get_the_title()) . '</a>';
    $excerpt_more = apply_filters('excerpt_more', ' ' . $excerpt_end);

    //$pos = strrpos($wpse_excerpt, '</');
    //if ($pos !== false)
    // Inside last HTML tag
    //$wpse_excerpt = substr_replace($wpse_excerpt, $excerpt_end, $pos, 0); /* Add read more next to last word */
    //else
    // After the content
    $wpse_excerpt .= $excerpt_end; /*Add read more in new paragraph */

    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');

    The code is from here: http://stackoverflow.com/questions/24151161/how-to-prevent-wordpress-from-stripping-html-tags-in-excerpt in-case you want further explanation on it :slight_smile: they explain each part of it.

    Hope this helps! Let us know if you need any further assistance with this.

    All the best,

    Tyler

  • [email protected]
    • Design Lord, Child of Thor

    I tried it and can’t get it to work. I copied your code, but perhaps it’s something I have before it in my functions file:

    `<?php

    /*

    * Simple Child Theme generated by Ultimatum Framework

    */

    $theme = array(

    ‘theme_name’ => ‘VCT’,

    ‘theme_slug’ =>’VCT’,

    );

    require_once( get_template_directory() . ‘/wonderfoundry/wonderworks.php’ );

    function wpse_allowedtags() {

    // Add custom tags to this string

    return ‘<script>,<style>,
    ,,<i>,

    if ( ! function_exists( ‘wpse_custom_wp_trim_excerpt’ ) ) :

    function wpse_custom_wp_trim_excerpt($wpse_excerpt) {

    global $post;

    $raw_excerpt = $wpse_excerpt;

    if ( ” == $wpse_excerpt ) {

    $wpse_excerpt = get_the_content(”:wink:;

    $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()); /*IF you need to allow just certain tags. Delete if all tags are allowed */

    //Set the excerpt word count and only break after sentence is complete.

    $excerpt_word_count = 75;

    $excerpt_length = apply_filters(‘excerpt_length’, $excerpt_word_count);

    $tokens = array();

    $excerptOutput = ”;

    $count = 0;

    // Divide the string into tokens; HTML tags, or words, followed by any whitespace

    preg_match_all(‘/(<[^>]+>|[^<>s]+)s*/u’, $wpse_excerpt, $tokens);

    foreach ($tokens[0] as $token) {

    if ($count >= $excerpt_word_count && preg_match(‘/[,;?.!]s*$/uS’, $token)) {

    // Limit reached, continue until , ; ? . or ! occur at the end

    $excerptOutput .= trim($token);

    break;

    }

    // Add words to complete sentence

    $count++;

    // Append what’s left of the token

    $excerptOutput .= $token;

    }

    $wpse_excerpt = trim(force_balance_tags($excerptOutput));

    $excerpt_end = ‘ ‘ . ‘ » ‘ . sprintf(__( ‘Read more about: %s  »’, ‘wpse’ ), get_the_title()) . ‘‘;

    $excerpt_more = apply_filters(‘excerpt_more’, ‘ ‘ . $excerpt_end);

    //$pos = strrpos($wpse_excerpt, ‘</’:wink:;

    //if ($pos !== false)

    // Inside last HTML tag

    //$wpse_excerpt = substr_replace($wpse_excerpt, $excerpt_end, $pos, 0); /* Add read more next to last word */

    //else

    // After the content

    $wpse_excerpt .= $excerpt_end; /*Add read more in new paragraph */

    return $wpse_excerpt;

    }

    return apply_filters(‘wpse_custom_wp_trim_excerpt’, $wpse_excerpt, $raw_excerpt);

    }

    endif;

    remove_filter(‘get_the_excerpt’, ‘wp_trim_excerpt’:wink:;

    add_filter(‘get_the_excerpt’, ‘wpse_custom_wp_trim_excerpt’:wink:;

  • Ash
    • Code Norris

    Hello @[email protected]

    I hope you are well today.

    The above function will work if the_excerpt function is used. I don’t think Essential Grid using this function as it has some limitations, rather they can use their own function to get the excerpt. In that case, the above code won’t help at all.

    I would recommend to ask the plugin developer if they has any filter or settings for this? I can see it shows the
    tag, that means it supports html but they are stripping some html tag like – p tag.

    Cheers

    Ash