Skip to content

Instantly share code, notes, and snippets.

@yudapc
Forked from yanknudtskov/wp-admin-select2.php
Created November 30, 2022 02:28
Show Gist options
  • Save yudapc/8f58d77ad4f980b6c26e0dd4dd87b181 to your computer and use it in GitHub Desktop.
Save yudapc/8f58d77ad4f980b6c26e0dd4dd87b181 to your computer and use it in GitHub Desktop.
Add select2 to all select fields in WordPress Admin
<?php
function enqueue_select2_jquery() {
wp_register_style( 'select2css', '//cdnjs.cloudflare.com/ajax/libs/select2/3.4.8/select2.css', false, '1.0', 'all' );
wp_register_script( 'select2', '//cdnjs.cloudflare.com/ajax/libs/select2/3.4.8/select2.js', array( 'jquery' ), '1.0', true );
wp_enqueue_style( 'select2css' );
wp_enqueue_script( 'select2' );
}
add_action( 'admin_enqueue_scripts', 'enqueue_select2_jquery' );
function select2jquery_inline() {
?>
<style type="text/css">
.select2-container {margin: 0 2px 0 2px;}
.tablenav.top #doaction, #doaction2, #post-query-submit {margin: 0px 4px 0 4px;}
</style>
<script type='text/javascript'>
jQuery(document).ready(function ($) {
if( $( 'select' ).length > 0 ) {
$( 'select' ).select2();
$( document.body ).on( "click", function() {
$( 'select' ).select2();
});
}
});
</script>
<?php
}
add_action( 'admin_head', 'select2jquery_inline' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment