Suppose I created a custom metabox for the add/edit post page that uses a wide width (not suitable for being in the sidebar), is there a way of keeping it in the middle column only and sortable while not allowing the user to drag it to the side bar?
2 comments
Comments are closed.
If you want the meta box to appear below the content area, use the context parameter ‘normal’. See Function Reference/add meta box – Parameters. Initially, your meta box should only go to the side bar if the context parameter is set to ‘side’.
As far as making it impossible for the user to drag it to the side, study the answer at “Block metabox – No expanding, no moving around” for some direction on this.
Since you wish to make your meta box sortable while restricting it to the column beneath the content area, see if you can discover which part of
postbox.js
determines whether a postbox is set beneath the content or on the
side bar. It seems that you want it to appear in #poststuff and not in #side-sortables. I don’t have a script to do this at the moment, but hopefully this set you in the right direction.
First of all, thanks to RyanLoremIpsum for pointing me to the right direction. I’ve found the solution to my problem, which uses the method from the post suggested by Ryan Block metabox – No expanding, no moving around and the answer from jQuery UI Sortable – prevent dropping on one particular list to restrict a specific item from dropping into another drop area.
In the case of restricting metaboxes from being placed into the sidebar, here’s how to make it work:
Add a filter class
.no-sidebar
to your metabox element(s).By using the
postbox_classes_post_METABOX_NAME
filter, we can include custom classes to be attached to that specific metabox upon its creation.Add a footer script block in the admin page to restrict
.no-sidebar
metaboxes from being dropped into the sidebar.Here, we target the normal container (that houses the non-sidebar widgets) to bind the jQuery UI Sortable widget
beforeStop
event to check if the sidebar container is about the receive a.no-sidebar
metabox. If so, returnfalse
to cancel the drop.This method ensures that any future metaboxes with the class
no-sidebar
can only be sorted in the normal container. A potential UX problem is that the user can still see the placeholder box while dragging the metabox over the sidebar area. This might fool the user into thinking that they were unsuccessful at sorting the box the first time.