-
-
Save rupomkhondaker/d424cde88d242a8e9160eed08565a06e to your computer and use it in GitHub Desktop.
Add select2 to all select fields in WordPress Admin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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