Unbind postbox click handler

I’m trying to unbind the click handler on post metaboxes so they no longer hide when the handle is clicked. I need to do this because I use the handle to contain <select> elements & when changing these the click event is fired.

The code that binds the click handler is here: https://github.com/WordPress/WordPress/blob/master/wp-admin/js/postbox.dev.js#L8

Read More

Unfortunately the only way to unbind it seems to be editing this file & inserting the unbind inside add_postbox_toggles

Hope anyone can help

Related posts

Leave a Reply

2 comments

  1. You can just put the necessary javascript in a file and enqueue it on the necessary page:

    add_action( 'admin_enqueue_scripts', 'add_admin_scripts', 10, 1 );
    function add_admin_scripts( $hook ) {    
    //You can globalise $post here and enqueue the script for only certain post-types.
        if ( $hook == 'post-new.php' || $hook == 'post.php') {
            wp_register_script( 'my_js_handle','/path/to/js/my-js-file.js',array('jquery'),1,true);
            wp_enqueue_script('my_js_handle');
       }
    }
    

    With the javascript file containing:

        jQuery(document).ready(function() {
           jQuery('.postbox h3, .postbox .handlediv').unbind('click.postboxes');
        });
    

    (In, fact you could probably just ‘print’ it in the admin-footer).

  2. I will share my code and I hope someone reviews it since I’m not expert in event handling.

    This goes inside your plugin or your theme functions.php:

    /**
     * Disable meta box toggling (collapse/expand) for specified post types
     */
    add_action( 'admin_footer', 'wpse_39723_disable_metabox_toggle' );
    function wpse_39723_disable_metabox_toggle() {    
    
        $current_screen = get_current_screen();
    
        // Array of post types where we want to remove metabox toggling
        $post_types = array(
            'post',
            // 'page',
            // 'my_custom_post_type',
        );
    
        if( in_array( $current_screen->id, $post_types ) ) {
            ?>
            <script type="text/javascript">
                jQuery( document ).ready( function($) {
                    $( '.postbox' ).removeClass( 'closed' );
                    $( '.postbox .hndle' ).css( 'cursor', 'default' );
                    $( document ).delegate( '.postbox h3, .postbox .handlediv', 'click', function() {
                        $( this )
                            .unbind( 'click.postboxes' )
                            .parent().removeClass( 'closed' );
                    } );
                } );
            </script>
            <?php       
        }
    }