Anyone know how to disable this functionality so the meta boxes can’t be repositioned?
Leave a Reply
You must be logged in to post a comment.
Anyone know how to disable this functionality so the meta boxes can’t be repositioned?
You must be logged in to post a comment.
I had the same problem, and Google lead me here. Unfortunately none of these answers helped, but I ultimately figured out the answer, and it’s quite easy!
admin_enqueue_scripts
, and it worked fine.Disable the sorting functionality by putting this in that JavaScript file:
Essentially this just disables jQuery UI Sortable, which powers the metabox dragging functionality (postbox.dev.js:64). This also switches the cursor on the metabox handle to a standard mouse pointer instead of a move cursor (idea courtesy of brasofilo below).
Hope this helps!
Edit: I should add that it’s probably worth following some of the other advice here and disabling the saving of the metabox order. It will prevent confusion on the off-chance something gets mistakenly re-enabled.
Second edit: For the benefit of future generations (and future Google searchers), this fix was tested on WordPress 3.3.1. I can’t speak to other versions!
I answered a similar question with the suggestion to allow dragging, but disable saving the new order on the server side. This might give you more control, and be more future-proof as the JavaScript could change quickly, but the protocol to communicate with the server might stay more robust. This example disables all dragging, but you could expand it to check for your specific box or meta page.
The fastest way is deactivate the JS for this function. But i think, it is better when you also deregister the style for the box and init a custom style without the effects for the mouse and the open/close icon on the meta boxes.
The wordpress javascript identifies the draggable metaboxes by their h3 title with a class of “hndle”. It’s simple enough to disable these specifically by referencing the metabox in question (if you are creating custom metaboxes, you will have assigned it an identifier) and disabling any hndle classes by removing the classname or by renaming it. In my case, I have several separator types that I labeled with .hndle h3’s, but it is unlikely anyone else will have done things this way. So, you can do what I did below, or you may use .find(‘.hndle’).attr(‘class’,”)…. or something similar. This would go in a .js file that you enqueued in your functions.php file (whether it be in your themes folder or your plugins folder). The enqueueing would be called by an admin_print_scripts, init or whatever hook you prefer to use to add stuff to your admin pages.
I would also add this Javascript Hack :
… and this CSS :
I used that code to take advantage of the meta boxes but without the drag-and-drop and the open/close functions.
Just found the simple way, hope new seeker would helped with this. Assuming you could adding a css file on admin enqueue style, I only using css to do that and sorry for my bad english.
Hope it helps.
I noticed this question has remain unaswered, insofar as the asker not selecting a correct answer.
Jan gave a working example of stopping the metabox re-ordering being saved over Ajax, whilst others gave suggestions relating to the JS.
As far as i understand it all you want to do is disable dragging, nothing more. To do that you’ll need two things, firstly a function to intercept the ajax saving action, but secondly you also need to stop the JS dragging and dropping without killing of functionality anywhere else in the page, whilst also being to do it selectively for a post type or particular metabox.
Using Jans function and some jQuery we can do that without totally killing other functionality the postbox script creates, like so..
PHP Code for theme functions file or plugin file
Uncomment 1 of the appropriate lines to make the enqueue work.
jQuery/JS for the Javascript file referenced in the above
Very basic jquery that removes the metabox sortable class from appliable elements, this prevents the dragging.
As you can see i’ve added in 1 example post type to add the code, book in this case. However you mentioned wanting to also have the possibility of disabling it for specific metaboxes.
It can be done, there’s just a few small side-effects one of those being, that by removing classes from given metaboxes to prevent dragging, you do also prevent the toggle function working(ie. the metabox title toggle functionality).
That said, it can be done…
First, you’d update the
disable_metabox_dragging
function to..Again, noting you need to uncomment the applicable
wp_enqueue_script
line.The array inside the localize call is what determines which metaboxes to disable, the empty 0 keyed item is there purposely because the localize script function strips any 0 keyed indexes in the array.
Second, the new JS file referenced in the above tweaked enqueue function.
Only thing you need do is determine the ID for the metaboxes you want to hide, and pass them into the array that sets the disabled metaboxes(in the
wp_localize_scipt
call).Overall i don’t think selectively disabling metaboxes is short of drawbacks, there’s just no support to re-config the sortable init action in WordPress, so disabling metabox sorting on a per element basis is going to be hacky at best(my code above is evidence of that). Ideally, what’s needed here is an action in WordPress to hook the sortable init, but that’s currently hardcoded into the postbox javascript(which does more than just setup sortable).
In any case, i hope that’s helped address the original question.
To add to all of the previous answers, if you also want to prevent WordPress from loading custom positions, the following should do the trick (replace
post
with any post type):