fix: open advanced search from empty home search
Co-authored-by: rafa <rcalvotorrejon@gmail.com> Signed-off-by: rafa <rcalvotorrejon@gmail.com>
This commit is contained in:
@@ -924,6 +924,58 @@ function fea_is_front_page(): bool {
|
|||||||
return $id && in_array($id, fea_front_page_ids(), true);
|
return $id && in_array($id, fea_front_page_ids(), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** URL de la búsqueda avanzada en el idioma actual, con fallback al español. */
|
||||||
|
function fea_home_advanced_search_url(): string {
|
||||||
|
$url = home_url('/buscar/');
|
||||||
|
if (!function_exists('pll_current_language') || !function_exists('pll_get_post')) return $url;
|
||||||
|
|
||||||
|
$lang = pll_current_language();
|
||||||
|
$default = function_exists('pll_default_language') ? pll_default_language() : 'es';
|
||||||
|
if (!$lang || $lang === $default) return $url;
|
||||||
|
|
||||||
|
$page = get_page_by_path('buscar');
|
||||||
|
if (!$page) return $url;
|
||||||
|
|
||||||
|
$translated_id = pll_get_post($page->ID, $lang);
|
||||||
|
return $translated_id ? get_permalink($translated_id) : $url;
|
||||||
|
}
|
||||||
|
|
||||||
|
// En desktop, una lupa sin texto abre la búsqueda avanzada en vez de activar la
|
||||||
|
// validación nativa del campo requerido del bloque Search de WordPress.
|
||||||
|
add_action('wp_footer', function() {
|
||||||
|
if (!fea_is_front_page()) return;
|
||||||
|
$advanced_url = wp_json_encode(fea_home_advanced_search_url());
|
||||||
|
?>
|
||||||
|
<script>
|
||||||
|
(function() {
|
||||||
|
function configureDesktopSearch() {
|
||||||
|
if (!window.matchMedia('(min-width: 601px)').matches) return;
|
||||||
|
|
||||||
|
document.querySelectorAll('form.wp-block-search, .wp-block-search form').forEach(function(form) {
|
||||||
|
var input = form.querySelector('input[type="search"], input[name="s"]');
|
||||||
|
if (!input || form.dataset.feaEmptySearchReady) return;
|
||||||
|
|
||||||
|
form.dataset.feaEmptySearchReady = 'true';
|
||||||
|
form.noValidate = true;
|
||||||
|
input.removeAttribute('required');
|
||||||
|
form.addEventListener('submit', function(event) {
|
||||||
|
if (input.value.trim() !== '') return;
|
||||||
|
event.preventDefault();
|
||||||
|
window.location.assign(<?php echo $advanced_url; ?>);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (document.readyState === 'loading') {
|
||||||
|
document.addEventListener('DOMContentLoaded', configureDesktopSearch);
|
||||||
|
} else {
|
||||||
|
configureDesktopSearch();
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
</script>
|
||||||
|
<?php
|
||||||
|
});
|
||||||
|
|
||||||
/** True para la landing de Escuela, que no muestra título visible. */
|
/** True para la landing de Escuela, que no muestra título visible. */
|
||||||
function fea_is_escuela_page(): bool {
|
function fea_is_escuela_page(): bool {
|
||||||
if (!is_page()) return false;
|
if (!is_page()) return false;
|
||||||
|
|||||||
Reference in New Issue
Block a user