build_dropdown_script_block_core_categories()
Generates the inline script for a categories dropdown field.
Хуков нет.
Возвращает
Строку. Returns the dropdown onChange redirection script.
Использование
build_dropdown_script_block_core_categories( $dropdown_id );
- $dropdown_id(строка) (обязательный)
- ID of the dropdown field.
Список изменений
| С версии 5.0.0 | Введена. |
Код build_dropdown_script_block_core_categories() build dropdown script block core categories WP 6.9.1
<?php
function build_dropdown_script_block_core_categories( $dropdown_id ) {
ob_start();
$exports = array( $dropdown_id, home_url() );
?>
<script>
( ( [ dropdownId, homeUrl ] ) => {
const dropdown = document.getElementById( dropdownId );
function onSelectChange() {
setTimeout( () => {
if ( 'escape' === dropdown.dataset.lastkey ) {
return;
}
if ( dropdown.value && dropdown instanceof HTMLSelectElement ) {
const url = new URL( homeUrl );
url.searchParams.set( dropdown.name, dropdown.value );
location.href = url.href;
}
}, 250 );
}
function onKeyUp( event ) {
if ( 'Escape' === event.key ) {
dropdown.dataset.lastkey = 'escape';
} else {
delete dropdown.dataset.lastkey;
}
}
function onClick() {
delete dropdown.dataset.lastkey;
}
dropdown.addEventListener( 'keyup', onKeyUp );
dropdown.addEventListener( 'click', onClick );
dropdown.addEventListener( 'change', onSelectChange );
} )( <?php echo wp_json_encode( $exports, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ); ?> );
</script>
<?php
return wp_get_inline_script_tag(
trim( str_replace( array( '<script>', '</script>' ), '', ob_get_clean() ) ) .
"\n//# sourceURL=" . rawurlencode( __FUNCTION__ )
);
}