Skip to content

Conversation

@s3rgiosan
Copy link
Member

Description of the Change

This PR introduces four new JavaScript filters to the ContentPicker and ContentSearch components, enabling developers to extend queried fields and customize UI display to better meet client requirements.

This approach:

  • Reduces custom component development time on client projects
  • Improves content editor experience with relevant contextual information
  • Maintains backward compatibility with existing implementations
  • Flexible filter system accommodates various client requirements

Problem

Client projects frequently require additional information beyond the default fields (title, URL) when picking or searching content. Common requests include displaying post IDs, parent page titles, or custom metadata to help content editors make informed selections.

Solution

Added filters that allow developers to:

  1. Query additional fields from WordPress REST API endpoints
  2. Customize the display of picked items and search results
  3. Remove unnecessary information (like URLs) when not needed by editors

New Filters

ContentPicker

  • tenup.contentPicker.queryFields - Extends fields queried from core WP REST endpoints
addFilter(
    'tenup.contentPicker.queryFields',
    'fooBar/contentPickerQueryFields',
    ( fields, mode ) => {
        if( mode === 'post' ) {
            fields.push('excerpt');
        }
        return fields;
    }
);
  • tenup.contentPicker.pickedItem - Customizes picked item UI display
addFilter(
    'tenup.contentPicker.pickedItem',
    'fooBar/contentPickerPickedItem',
    ( item, result ) => {
        const info = `<strong>ID:</strong> ${result.id}<br>${result.excerpt.rendered}`;
        return {...item, url: '', info };
    }
);

ContentSearch

  • tenup.contentSearch.queryFields - Extends search query fields (requires PHP companion filter)
addFilter(
    'tenup.contentSearch.queryFields',
    'fooBar/contentSearchQueryFields',
    ( fields, mode ) => {
        if( mode === 'post' ) {
            fields.push('excerpt');
        }
        return fields;
    }
);

Additionally in PHP:

function add_search_result_field() {
    register_rest_field(
        'search-result',
        'excerpt',
        array(
            'get_callback'    => function ( $post ) {
                return get_the_excerpt( $post['id'] );
            },
            'update_callback' => null,
            'schema'          => null,
        )
    );
}
add_action( 'rest_api_init', __NAMESPACE__ . '\add_search_result_field' );
  • tenup.contentSearch.searchResult - Customizes search result display
addFilter(
    'tenup.contentSearch.searchResult',
    'fooBar/contentSearchSearchResult',
    ( item, result ) => {
        item.url = '';
        item.info = `<strong>ID:</strong> ${result.id}<br>${result.excerpt}`;
        return item;
    }
);

How to test the Change

  1. Create a test WordPress environment with the updated block components
  2. Add the example filters to your theme's JavaScript
  3. For ContentSearch, add the PHP example to register additional fields
  4. Test ContentPicker component:
  • Verify additional fields are queried from REST API
  • Confirm picked items display custom information
  • Check that URL field can be hidden when set to empty string
  1. Test ContentSearch component:
  • Verify search results include additional queried fields
  • Confirm search result display matches custom formatting
  1. Verify no regressions in existing functionality without filters applied

Changelog Entry

Added - New JavaScript filters for ContentPicker and ContentSearch components to extend queried fields and customize item display

Credits

Props @s3rgiosan

Checklist:

@s3rgiosan s3rgiosan requested a review from fabiankaegy May 26, 2025 23:21
@s3rgiosan
Copy link
Member Author

… and to filter the search result before it is returned
@s3rgiosan s3rgiosan requested a review from rickalee May 26, 2025 23:26
# Conflicts:
#	components/content-picker/PickedItem.tsx
#	components/content-picker/SortableList.tsx
#	package-lock.json
#	package.json
@s3rgiosan
Copy link
Member Author

Merge conflicts were addressed. This is ready for review.

"@wordpress/icons": "^10.0.0",
"array-move": "^4.0.0",
"clsx": "^2.1.1",
"dompurify": "^3.2.6",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

* @param {ContentSearchMode} mode - The mode of the content search.
* @returns {string[]} - The filtered fields.
*/
fields = applyFilters('tenup.contentSearch.queryFields', fields, mode) as string[];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little worried about the introduction of all these filters... Unlike in PHP here in JS they just behave oddly and are no longer really recommended by the core team...

Also I am a bit worried that there is no way to figure out the context of which component we are talking about. It just applies to all ContentSearch components on the site.

I would rather see an implementation that has the filters needed within the content connect plugin and then passes its callback function into the component as an optional prop. That feels more "safe" to me here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants