C’mon… make it easy on yourself. Especially if you’re using MetaBox.io ❤️

C’mon… make it easy on yourself. Especially if you’re using MetaBox.io ❤️
<?php
// Appends JS to the footer of admin pages. Forces select2 inputs to autofocus when clicked.
// https://snipsnip.pro/s/158
namespace ff2G6Nm;
if (\is_admin() && \current_user_can('manage_options')) {
\add_action( 'admin_footer', '\ff2G6Nm\add_script_to_page' );
function add_script_to_page() {
?>
<script type="text/javascript">
window.addEventListener('DOMContentLoaded', ()=>{
if (window.jQuery) {
jQuery(document).on('select2:open', () => {
setTimeout(focusInputOfTheOpenSelect2,100);
});
function focusInputOfTheOpenSelect2(){
const basicClass = '.select2-container';
const opendClass = '.select2-container--open';
const focusClass = '.select2-container--focus';
const dropdClass = '.select2-search.select2-search--dropdown';
const fieldClass = `${dropdClass} input.select2-search__field[type="search"]`;
const field = document.querySelector(`${opendClass} ${fieldClass}`);
if (field) {
// console.log(field);
field.focus();
}
}
}
});
</script>
<?php }
}