Title: Highlight showing html code
Last modified: June 27, 2020

---

# Highlight showing html code

 *  Resolved [rely28](https://wordpress.org/support/users/rely28/)
 * (@rely28)
 * [5 years, 9 months ago](https://wordpress.org/support/topic/highlight-showing-html-code/)
 * Hi,
 * See example in the link. When I activate the personalized excerpt and highlight,
   selected a specific color, it is showing the html code with span, color etc. 
   rather than translating the code into what it should appear (same with p code
   or any other html in the excerpt.
 * `…id=”caption-attachment-42860″ class=”wp-caption-text”>Distanciation sociale
   dans le métro de Bangkok.</p> Chronologie de la pandémie en <span style=’color:#
   1e73be’>Thaïlande</span> <p>Pour rappel, la <span style=’color: #1e73be’>Thaïlande
   </span> a été le premier, pays a rapporter des…’
 * I’m not sure if I need to add any specific function in order to make it work 
   the the way it suppose to. Not sure also it could be from my theme. Any idea 
   how to correct this ?
    Regards.
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fhighlight-showing-html-code%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 11 replies - 1 through 11 (of 11 total)

 *  Plugin Author [Mikko Saari](https://wordpress.org/support/users/msaari/)
 * (@msaari)
 * [5 years, 9 months ago](https://wordpress.org/support/topic/highlight-showing-html-code/#post-13044226)
 * What is your theme doing to print out the excerpt in the search results template?
   Looks like the theme might be escaping the HTML tags, which is often a good idea,
   but not in this case, when the HTML codes should be displayed on the page.
 *  Thread Starter [rely28](https://wordpress.org/support/users/rely28/)
 * (@rely28)
 * [5 years, 9 months ago](https://wordpress.org/support/topic/highlight-showing-html-code/#post-13045432)
 * I’m not sure, here is what I have in the excerpt.php
 *     ```
       <?php
   
       if ( post_password_required() ) {
       	echo get_the_password_form();
       } else {
       	$excerpt_length = isset( $params['excerpt_length'] ) ? $params['excerpt_length'] : '';
       	$excerpt        = backpacktraveler_mikado_excerpt( $excerpt_length );
   
       	$link_page_exists = apply_filters( 'backpacktraveler_mikado_filter_single_links_exists', '' );
   
       	if ( ! empty( $excerpt ) && empty( $link_page_exists ) ) { ?>
       		<div class="mkdf-post-excerpt-holder">
       			<p itemprop="description" class="mkdf-post-excerpt">
       				<?php echo wp_kses_post( $excerpt ); ?>
       			</p>
       		</div>
       	<?php }
       } ?>
       ```
   
 *  Thread Starter [rely28](https://wordpress.org/support/users/rely28/)
 * (@rely28)
 * [5 years, 9 months ago](https://wordpress.org/support/topic/highlight-showing-html-code/#post-13045438)
 * In the blog-function.php I have this :
 *     ```
       if ( ! function_exists( 'backpacktraveler_mikado_excerpt' ) ) {
       	/**
       	 * Function that cuts post excerpt to the number of word based on previosly set global
       	 * variable $word_count, which is defined in mkdf_set_blog_word_count function.
       	 *
       	 * @param $length - default excerpt length
       	 *
       	 * @return string - formatted excerpt
       	 *
       	 * It current post has read more tag set it will return content of the post, else it will return post excerpt
       	 *
       	 */
       	function backpacktraveler_mikado_excerpt( $length ) {
       		global $post;
   
       		//does current post has read more tag set?
       		if ( backpacktraveler_mikado_post_has_read_more() ) {
       			global $more;
   
       			//override global $more variable so this can be used in blog templates
       			$more = 0;
   
       			return get_the_content( true );
       		}
   
       		$number_of_chars = backpacktraveler_mikado_get_meta_field_intersect( 'number_of_chars', backpacktraveler_mikado_get_page_id() );
       		$word_count      = $length !== '' ? $length : $number_of_chars;
   
       		//is word count set to something different that 0?
       		if ( $word_count > 0 ) {
   
       			//if post excerpt field is filled take that as post excerpt, else that content of the post
       			$post_excerpt = $post->post_excerpt !== '' ? $post->post_excerpt : strip_tags( strip_shortcodes( $post->post_content ) );
   
       			//remove leading dots if those exists
       			$clean_excerpt = strlen( $post_excerpt ) && strpos( $post_excerpt, '...' ) ? strstr( $post_excerpt, '...', true ) : $post_excerpt;
       			//if clean excerpt has text left
       			if ( $clean_excerpt !== '' ) {
       				//explode current excerpt to words
       				$excerpt_word_array = explode( ' ', $clean_excerpt );
   
       				//cut down that array based on the number of the words option
       				$excerpt_word_array = array_slice( $excerpt_word_array, 0, $word_count );
   
       				//and finally implode words together
       				$excerpt = implode( ' ', $excerpt_word_array );
   
       				//is excerpt different than empty string?
       				if ( $excerpt !== '' ) {
       					return rtrim( wp_kses_post( $excerpt ) );
       				}
       			}
   
       			return '';
       		} else {
       			return '';
       		}
       	}
       }
       ```
   
 *  Plugin Author [Mikko Saari](https://wordpress.org/support/users/msaari/)
 * (@msaari)
 * [5 years, 9 months ago](https://wordpress.org/support/topic/highlight-showing-html-code/#post-13045457)
 * You can try replacing
 * `<?php echo wp_kses_post( $excerpt ); ?>`
 * with
 * `<?php the_excerpt(); ?>`
 *  Thread Starter [rely28](https://wordpress.org/support/users/rely28/)
 * (@rely28)
 * [5 years, 9 months ago](https://wordpress.org/support/topic/highlight-showing-html-code/#post-13045748)
 * But If I do so, I’m afraid it’s gonna change all of the actual exceprt, not just
   the ones from the search list, isn’t it ?
 *  Thread Starter [rely28](https://wordpress.org/support/users/rely28/)
 * (@rely28)
 * [5 years, 9 months ago](https://wordpress.org/support/topic/highlight-showing-html-code/#post-13045796)
 * The strip tags is actually in the blog-function.php
 * `$post_excerpt = $post->post_excerpt !== '' ? $post->post_excerpt : strip_tags(
   strip_shortcodes( $post->post_content ) );`
 * I believe this is the culprit, but I’m afraid to messed up the functionality 
   if removing or changing improperly…
 *  Thread Starter [rely28](https://wordpress.org/support/users/rely28/)
 * (@rely28)
 * [5 years, 9 months ago](https://wordpress.org/support/topic/highlight-showing-html-code/#post-13045851)
 * By the way, I did try your solution, in case, with no luck…
 *  Plugin Author [Mikko Saari](https://wordpress.org/support/users/msaari/)
 * (@msaari)
 * [5 years, 9 months ago](https://wordpress.org/support/topic/highlight-showing-html-code/#post-13046720)
 * No, `strip_tags()` isn’t the problem, because the tags are not stripped, they’re
   escaped. Also, Relevanssi sets `$post->post_excerpt`, so that’s what the code
   uses. If the tags were stripped, that would look very nice and clean. This looks
   like the work of `esc_html()`, but there doesn’t seem to be `esc_html()` anywhere
   there.
 * In any case, the best solution would be to simply use `the_excerpt()` for the
   search results in the search results template, because that would get you just
   the Relevanssi-generated excerpts, without any problems whatsoever.
 *  Thread Starter [rely28](https://wordpress.org/support/users/rely28/)
 * (@rely28)
 * [5 years, 9 months ago](https://wordpress.org/support/topic/highlight-showing-html-code/#post-13048918)
 * Ok, That’s a useful information since I remembered seing it somewhere, and here
   is the file I have, loop.php
 *     ```
       <?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
       	<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
       		<div class="mkdf-post-content">
       	        <?php if ( has_post_thumbnail() ) { ?>
       		        <div class="mkdf-post-image">
       			        <a itemprop="url" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
       				        <?php the_post_thumbnail( 'thumbnail' ); ?>
       			        </a>
       		        </div>
       	        <?php } ?>
       	        <div class="mkdf-post-title-area <?php if ( ! has_post_thumbnail() ) { echo esc_attr( 'mkdf-no-thumbnail' ); } ?>">
       		        <div class="mkdf-post-title-area-inner">
       			        <h5 itemprop="name" class="mkdf-post-title entry-title">
       				        <a itemprop="url" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
       			        </h5>
       			        <?php
       			        $mkdf_my_excerpt = get_the_excerpt();
   
       			        if ( ! empty( $mkdf_my_excerpt ) ) { ?>
       				        <p itemprop="description" class="mkdf-post-excerpt"><?php echo wp_trim_words( esc_html( $mkdf_my_excerpt ), 30 ); ?></p>
       			        <?php } ?>
       		        </div>
       	        </div>
               </div>
           </article>
       <?php endwhile; ?>
       <?php else: ?>
       	<p class="mkdf-blog-no-posts"><?php esc_html_e( 'No posts were found.', 'backpacktraveler' ); ?></p>
       <?php endif; ?>
       ```
   
 * The only problem remaining is that, if I remove that esc_html, I indeed don’t
   see html anymore but the highlight is not working then…
 *  Plugin Author [Mikko Saari](https://wordpress.org/support/users/msaari/)
 * (@msaari)
 * [5 years, 9 months ago](https://wordpress.org/support/topic/highlight-showing-html-code/#post-13049140)
 * `wp_trim_words()` also removes HTML tags. Replacing
 * `echo wp_trim_words( esc_html( $mkdf_my_excerpt ), 30 );`
 * with
 * `echo $mkdf_my_excerpt;`
 * should work just fine.
 *  Thread Starter [rely28](https://wordpress.org/support/users/rely28/)
 * (@rely28)
 * [5 years, 9 months ago](https://wordpress.org/support/topic/highlight-showing-html-code/#post-13049548)
 * Awesome ! That made the trick ! Thanks a lot, gonna put 5 stars to your plugin
   straight away !

Viewing 11 replies - 1 through 11 (of 11 total)

The topic ‘Highlight showing html code’ is closed to new replies.

 * ![](https://ps.w.org/relevanssi/assets/icon-256x256.png?rev=2025044)
 * [Relevanssi - A Better Search](https://wordpress.org/plugins/relevanssi/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/relevanssi/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/relevanssi/)
 * [Active Topics](https://wordpress.org/support/plugin/relevanssi/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/relevanssi/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/relevanssi/reviews/)

## Tags

 * [excerpt](https://wordpress.org/support/topic-tag/excerpt/)
 * [html code](https://wordpress.org/support/topic-tag/html-code/)

 * 11 replies
 * 2 participants
 * Last reply from: [rely28](https://wordpress.org/support/users/rely28/)
 * Last activity: [5 years, 9 months ago](https://wordpress.org/support/topic/highlight-showing-html-code/#post-13049548)
 * Status: resolved