Skip to main contentSkip to footer

If your posts have lots of programming code examples in <pre> and <code> tags, those might look pretty bad in the search results. A snippet of programming code isn’t usually a good excerpt, and if you use those tags purely for code snippets, they won’t likely contain significant search content, either.

Fortunately it’s easy to completely remove all <pre> and <code> tags and their content both in indexing and excerpt-building. All you need to do is to add the following code to your theme functions.php:

add_filter( 'relevanssi_post_content', 'rlv_pre_code', 10 );
add_filter( 'relevanssi_excerpt_content', 'rlv_pre_code', 10 );
function rlv_pre_code( $content ) {
	$content = preg_replace( '#<pre.*?</pre>#mis', '', $content );
	$content = preg_replace( '#<code.*?</code>#mis', '', $content );
	return $content;
}

This will find all <pre> and <code> tags in posts and replace them and their contents with nothing, both when indexing and when building excerpts. This will immediately take effect for excerpts, and requires reindexing to apply in indexing.

Your account

Not logged in. Log in to see your license details.

Search

Popular Resources

Related Posts

…option. You can also add taxonomies, like posts and categories. Using taxonomies requires that Relevanssi indexes the taxonomy; you can adjust that on the Relevanssi indexing settings page. You can choose which post types Relevanssi uses for the related posts. “Matching post type” means that for posts, Relevanssi uses posts……results from the settings page: you can choose whether Relevanssi displays the title, the thumbnail or the excerpt for the posts. Also, you can set up a default thumbnail for thumbnails if the post doesn’t have a thumbnail. You can also set up the minimum width of the related posts….…the post – for example, if you have related posts automatically added to all pages, there are probably pages where you don’t want them, like the front page. You can also disable to post so that Relevanssi never uses it as a related post for another post. You can also…

WordCamp Nordic 2019
Relevanssi Premium is a proud sponsor of the inaugural WordCamp Nordic. There have been several WordCamps in Nordic countries, but…
Automatic redirects to category archives

While Relevanssi Premium can return taxonomy terms as search results, sometimes sending the searcher directly to the category archive is better. This is especially true for WooCommerce sites, where displaying taxonomy terms in search results pages is complicated. Relevanssi Premium has a redirect feature you can use to redirect searches…

Related Posts:

Comment Section:

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed