I am able to disable dragging of metaboxes sitewide with this function:

function disable_drag_metabox() {
wp_deregister_script('postbox');
}
add_action( 'admin_init', 'disable_drag_metabox' );

But I want it only on a custom post type.
I tried the usual:

function disable_drag_metabox() {
global $current_screen;
if( 'event' == $current_screen->post_type ) wp_deregister_script('postbox');
}
add_action( 'admin_init', 'disable_drag_metabox' );

and also this one:

function disable_drag_metabox() {
$screen = get_current_screen();
if( in_array( $screen->id, array( 'event' ) ) ) {
    wp_deregister_script('postbox');
}
}
add_action( 'admin_init', 'disable_drag_metabox' );

Sadly it doesn’t work. What am I doing wrong? the custom post type is called event.

1 Answer
1

The current screen isn’t setup on the admin_init hook. That’s why global $current_screen and get_current_screen() don’t work.

Every admin page has a load-something hook that fires after the current screen is set up. Since you say this is for an events custom post type, you should use the load-post.php hook. So you’re code would look like:

function disable_drag_metabox() {
  if( 'events' === get_current_screen()->post_type ) {
    wp_deregister_script( 'postbox' );
  }
}
add_action( 'load-post.php', 'disable_drag_metabox' );

You can use the Query Monitor plugin to figure out what hooks fire on each page and in what order. It also does a lot of other cook stuff.

Leave a Reply

Your email address will not be published. Required fields are marked *