Disable dashboard drag&drop

I need to disable drag&drop functionality for dashboard widgets.

This should be realized with jquery, just as it is for the dashboard_browser_nag. I’m no jquery expert – so can anybody tell me how to find and modify my widgets with jquery?

Read More

So far, I know that I have to do some .sortable(“cancel”), but how exactly?

Thanks!

Related posts

Leave a Reply

3 comments

  1. This will remove the draggable functionality from each widget:

    jQuery('.widget.ui-draggable').draggable('destroy')
    

    You have find a good way to inject this into your code, either through wp_enqueue_script() or echo it inline on the Widget page.

    To target a specific Widget you would have to do something along these lines:

    jQuery('.widget.ui-draggable').each(function() {
        if (jQuery(this).find('h4').text() == 'Archives')
            jQuery(this).draggable('destroy');
    }
    

    Or select your way around to find the widget by id:

    widget-2_calendar-__i__
    widget-3_categories-__i__
    ...
    

    If you browse around the source you’ll soon find out how these are internally formed by WordPress. The rest is up to jQuery, selectors, maybe regular expressions (though I don’t recommend them in this case).

  2. Not tested, but this would atleast get you started. Add this to functions.php or make your own plugin for it.

    function remove_drag_and_drop() {
        wp_dequeue_script( 'jquery-ui-sortable' );
    }
    add_action('wp_dashboard_setup', 'remove_drag_and_drop' );
    
  3. Interesting approach. Isn’t working like that however. My best so far is to prevent adding any footer scripts:

     function remove_footer_scripts() { return false; }
     add_filter('print_footer_scripts', 'remove_footer_scripts');
    

    Clearly not a good solution. But the suggested wp_dequeue_script function to remove a single script is not working – maybe ‘wp_dashboard_setup’ is the wrong hook? I also tried to hook into ‘wp_default_scripts’ and to modify $wp_scripts itself, but without any success…

    @Ijaas: good point, it would also break admin_widgets and admin_gallery, but isn’t it possible to add the action/filter depending on the $current_screen? Should be, as it is a footer script which is added after set_current_screen() was called.