1

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

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

Hope anyone can help

2

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).

| improve this answer | |
  • Wow thank you so much, that worked a treat! But now I'm confused. Im currently calling add_action() inside a class constructor, which caused the problem. If I move it outside of the class however it works. Any ideas? – studioromeo Jan 22 '12 at 1:52
1

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       
    }
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.