I originally posted this question as a javascript question here, but think maybe it would be better asked here. I tried adding select2 to all wordpress admin screens. However, since many selects are created dynamically, I needed to find a way to recognize the dynamic selects. I got it working, but the dynamic selects do not load with the right width and positioning. Take for example the http://example.com/wp-admin/edit.php page. When clicking quick edit, select2 does not find the correct width and positioning of the new selects. If you look at the source, you can see that the dropdown with the following style attributes:
style="width:0; top:0; left:0;
Because of this, it doesn’t show when you click on it. However, if you click on a new select and then click again right below it, it updates the styles, but now moves the select2 overlay just to the right of the selects.
Here is an example plugin if anyone wants to test it followed by some screenshots:
<?php
/**
* Plugin Name: Admin Select2 Not Working Everywhere
* Plugin URI:
* Description: Adds select2 to all admin screens
* Version: 1.0
* Author: Bryan Willis
* Author URI:
* License: GPL2
*/
function enqueue_select2_jquery() {
wp_register_style( 'select2css', 'http://cdnjs.cloudflare.com/ajax/libs/select2/3.4.8/select2.css', false, '1.0', 'all' );
wp_register_script( 'select2', 'http://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 ($) {
$("select").select2();
$( document.body ).on( "click", function() {
$("select").select2();
});
});
</script>
<?php
}
add_action( 'admin_head', 'select2jquery_inline' );