Skip to content

Instantly share code, notes, and snippets.

@samediamba
Forked from yanknudtskov/wp-admin-select2.php
Created April 7, 2022 13:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samediamba/0b5ed327e533b505786e628fa25af1a5 to your computer and use it in GitHub Desktop.
Save samediamba/0b5ed327e533b505786e628fa25af1a5 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