Skip to main contentSkip to footer

The Relevanssi attachment indexing assumes the files are connected to the posts using the WordPress attachment mechanism. What if you don’t use that but instead add the files to the pages using the File block in the block editor?

That’s not a problem, but it requires some extra code. Add this function to your site to index the contents of files added with the File block:

add_filter( 'relevanssi_block_to_render', function( $block ) {
    if ( 'core/file' === $block['blockName'] ) {
        $file_id      = $block['attrs']['id'];
        $file_content = get_post_meta( $file_id, '_relevanssi_pdf_content', true );
        if ( $file_content ) {
            $block['innerContent'][0] = $file_content;
        }
    }
    return $block;
} );

This function uses the relevanssi_block_to_render filter hook. Whenever a core/file block is encountered, the function fetches the file contents from the _relevanssi_pdf_content custom field for the attachment post and replaces the block contents with that.

This works great for indexing but does not include the file contents in excerpts. That requires another function:

add_filter( 'relevanssi_pre_excerpt_content', function( $content, $post) {
    $m = preg_match( '/<!-- wp:file.*?"id":(\d+).*?<!-- \/wp:file -->/s', $content, $matches );
    if ( $m > 0 ) {
        $file_id = $matches[1];
        $file_content = get_post_meta( $file_id, '_relevanssi_pdf_content', true );
        if ( $file_content ) {
            $content = preg_replace( '#' . preg_quote( $matches[0], '#' ) . '#', $file_content, $content );
        }
    }
    return $content;
}, 10, 2 );

This filter uses relevanssi_pre_excerpt_content and replaces the file block comment in the post content with the file contents from the custom field.

Your account

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

Search

Popular Resources

OceanWP

OceanWP is a popular WordPress theme. It works fine with Relevanssi, but how it handles excerpts on the search results pages is not fully compatible with Relevanssi. Relevanssi expects the theme to use the_excerpt() to print out the excerpts. OceanWP does that, but only for posts that have hand-made excerpts….…If the post doesn’t have an excerpt, OceanWP uses echo wp_trim_words( strip_shortcodes( $post->post_content ), $length ); to print out the excerpt, and that does not show the Relevanssi excerpt. To fix this problem, create a child theme for OceanWP if you don’t already have one. In the child theme, add……file partials/search/content.php with this content: <?php /** * Search result page entry content * * @package OceanWP WordPress theme */ // Exit if accessed directly. if ( ! defined( ‘ABSPATH’ ) ) { exit; } global $post; // Excerpt length. $length = apply_filters( ‘ocean_search_results_excerpt_length’, ’30’ ); ?> <div class=”search-entry-summary clr”<?php…

Debugging indexing problems
The easiest way to debug indexing problems is to use the WP CLI tools available in Relevanssi Premium. To see…
10Web Photo Gallery
The Photo Gallery plugin from 10Web uses a shortcode to add photo galleries to posts and pages. By default, Relevanssi…

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