Created
January 23, 2023 21:33
-
-
Save felipeelia/2793d231112b41abdad5bcf0cdf6d699 to your computer and use it in GitHub Desktop.
Add a "See All X Results" item to the autosuggest panel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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