Skip to main content

Style the AI Search box’s placements, or change what their results show

Premium Beginner AI Search

Give each AI Search box the layout that suits where it sits, a wide archive page, a narrow sidebar or a button that opens a dialog, and choose what each result shows.

placement is a benecaster_ai_search attribute, chosen per box (there is no site-wide default):

  • full-page (the default): wide, each result shows title, date and excerpt.
  • sidebar: narrow, the Search button under the input, each result shows title and date.
  • modal: a Search episodes button (.benecaster-ai-search__open) opening a native <dialog> (.benecaster-ai-search__dialog, with a .benecaster-ai-search__close button) that holds the full-page box.
  • Any other value is full-page.

The wrapper carries benecaster-ai-search--{placement} and data-placement. The base stylesheet wraps every rule in :where(), so a theme rule for those classes wins without raising specificity:

/* A theme's own sidebar look. */
.benecaster-ai-search--sidebar { font-size: 0.9em; }
.benecaster-ai-search--sidebar .benecaster-ai-search__submit { width: 100%; }

In a modal, keep the <dialog>, the opener and the form if you change the markup through benecaster_ai_search_output: the box’s script finds them by class, opens the dialog with showModal(), closes it on Esc, the close button or a backdrop click, and returns focus to the opener.

benecaster_ai_search_result_fields( array $fields, string $placement, array $atts ): array decides which of title, date and excerpt each result in this box shows; the default is all three, and [ 'title', 'date' ] for sidebar. title is always kept (it is the link) and anything else is dropped. The answer is written into the box as data-fields. The search route still returns every field, so this changes what is shown, not what is sent.

Code

<?php
// Show excerpts in sidebar boxes too, but never dates in a modal.
add_filter( 'benecaster_ai_search_result_fields', function ( array $fields, string $placement ): array {
    if ( 'sidebar' === $placement ) {
        return [ 'title', 'excerpt' ];
    }
    return 'modal' === $placement ? array_values( array_diff( $fields, [ 'date' ] ) ) : $fields;
}, 10, 2 );

View on GitHub →

Hooks Used

Need this built rather than just documented? See our services →