PluginIntegrationsPro onlyeasy-footnotesindexingcustom-tables

Index Easy Footnotes content

Include Easy Footnotes text in the custom content index used by Better Search Pro and Contextual Related Posts Pro.

Why the integration is needed

Easy Footnotes moves footnote text into output added through the_content. The custom content indexer does not run that filter. This integration extracts enclosed footnote text before shortcode content is discarded.

Better Search Pro and Contextual Related Posts Pro share the custom content table. The snippet registers both indexing filters so it can run whichever plugin performs the index update.

After installation

Run a reindex to update previously published posts. For Better Search Pro, use Tools to rebuild the index, or run the following on the intended WordPress site:

wp bsearch ecsi reindex --force

The integration recognizes the efn_note and note shortcode names. Escaped shortcodes are skipped. It does not change how footnotes appear to visitors.

Requirements

  • Better Search
  • Contextual Related Posts
  • The corresponding Pro plugin must be installed and active.
  • Better Search Pro or Contextual Related Posts Pro with custom content indexing enabled.
  • Easy Footnotes must be installed and active.
  • Reindex existing content after installing this integration.

Source

better-search/custom-tables/bsearch-easy-footnotes-integration.php
<?php
/**
 * Plugin Name: Better Search - Easy Footnotes Integration
 * Plugin URI:  https://webberzone.com/plugins/better-search/
 * Description: Makes footnote text added via the Easy Footnotes plugin searchable by Better Search Pro's Custom Tables (Enhanced Content Search Index).
 * Author: Ajay D'Souza
 * Author URI: https://webberzone.com
 * Version: 1.0
 *
 * @package Better_Search_Pro
 * @license GPL-2.0+
 */

/**
 * Easy Footnotes' [efn_note] / [note] shortcode replaces its own enclosed text with a
 * numbered marker and only re-appends the note text via a the_content filter, which
 * Better Search Pro's indexer doesn't run. This pulls the note text back out of the raw
 * shortcode content before it's discarded, using the bsearch_pre_index_content_parts
 * filter (and its Contextual Related Posts Pro equivalent, since the two plugins share
 * the same custom content table and only one of them syncs on save_post).
 *
 * After installing, run a reindex so already-published posts pick up their footnote
 * text: WP Admin > Better Search Pro > Tools, or `wp bsearch ecsi reindex --force`.
 */

if ( ! function_exists( 'bsearch_efn_integration_extract_notes' ) ) {
	/**
	 * Extract footnote text from Easy Footnotes shortcodes before it is discarded.
	 *
	 * @param string[] $extra_parts Extra text fragments already queued for the index.
	 * @param string   $content     Raw post content, before block/shortcode processing.
	 * @return string[] Extra text fragments, with recovered footnote text appended.
	 */
	function bsearch_efn_integration_extract_notes( array $extra_parts, string $content ): array {
		if ( ! class_exists( 'easyFootnotes' ) ) {
			return $extra_parts;
		}

		// Easy Footnotes registers both [note] and [efn_note] as aliases of the same shortcode.
		$pattern = get_shortcode_regex( array( 'note', 'efn_note' ) );

		if ( ! preg_match_all( '/' . $pattern . '/', $content, $matches, PREG_SET_ORDER ) ) {
			return $extra_parts;
		}

		foreach ( $matches as $match ) {
			// Skip escaped shortcodes, e.g. [[efn_note]]...[[/efn_note]].
			if ( '[' === $match[1] && ']' === $match[6] ) {
				continue;
			}

			$note = trim( $match[5] );

			if ( '' === $note ) {
				continue;
			}

			$extra_parts[] = wp_strip_all_tags( do_shortcode( $note ) );
		}

		return $extra_parts;
	}
}

if ( ! has_filter( 'bsearch_pre_index_content_parts', 'bsearch_efn_integration_extract_notes' ) ) {
	add_filter( 'bsearch_pre_index_content_parts', 'bsearch_efn_integration_extract_notes', 10, 2 );
}
if ( ! has_filter( 'crp_pre_index_content_parts', 'bsearch_efn_integration_extract_notes' ) ) {
	add_filter( 'crp_pre_index_content_parts', 'bsearch_efn_integration_extract_notes', 10, 2 );
}

Details

Type
Plugin
Last updated
GitHub
Source · Edit companion · History

Developer references