Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felipeelia/2793d231112b41abdad5bcf0cdf6d699 to your computer and use it in GitHub Desktop.
Save felipeelia/2793d231112b41abdad5bcf0cdf6d699 to your computer and use it in GitHub Desktop.
Add a "See All X Results" item to the autosuggest panel
let autosuggestTotal = 0;
const autosuggestDataFilter = (data) => {
autosuggestTotal = data?.hits?.total || 0;
return data;
}
wp.hooks.addFilter('ep.Autosuggest.data', 'myTheme/autosuggestDataFilter', autosuggestDataFilter);
const autosuggestListHTMLFilter = (listHTML, options, input) => {
const allUrl = new URL(input.form.action);
const formData = new FormData(input.form);
const urlParams = new URLSearchParams(formData);
allUrl.search = urlParams.toString();
const url = allUrl.toString();
listHTML += `<li class="autosuggest-item" role="option" aria-selected="false" id="autosuggest-option-all">
<a href="${url}" class="autosuggest-link" data-url="${url}" tabindex="-1">
View All ${autosuggestTotal} Results
</a>
</li>`;
return listHTML;
};
wp.hooks.addFilter('ep.Autosuggest.listHTML', 'myTheme/autosuggestListHTMLFilter', autosuggestListHTMLFilter);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment