Prevent indexing segments of content
You may have some posts that have some content in the main editor you would like to have SearchWP index, but other content within that same post you do not want indexed. You can take advantage of SearchWP’s ability to process Shortcodes using the searchwp_do_shortcode
hook to make that happen!
To begin, we’ll need to set up support for a custom Shortcode. For this KB article we’ll be wrapping the content we want to exclude in [searchwp_no_index]
[/searchwp_no_index]
tags:
Any content included within our custom Shortcode tags will be excluded from indexing simply by adding the following to our theme’s functions.php
:
<?php | |
// tell SearchWP to process shortcodes | |
add_filter( 'searchwp_do_shortcode', '__return_true' ); | |
// if the SearchWP indexer is running prevent returning the | |
// content between [searchwp_no_index] and [/searchwp_no_index] | |
function shortcode_searchwp_no_index( $atts, $content = null ) { | |
// if the searchwp_indexer_running action fired, the indexer is running so don't return anything | |
return did_action( 'searchwp_indexer_running' ) ? '' : $content; | |
} | |
add_shortcode( 'searchwp_no_index', 'shortcode_searchwp_no_index' ); |
Note: make sure this hook is in place before adding your Shortcodes, when you add the Shortcodes to the content and click Update the post will be automatically purged and reindexed by SearchWP.