我用Javascript解决了这个问题。如果你想完全阻止它,你也应该在服务器端这样做,因为你可以在禁用Javascript的情况下编辑小部件(试试!)。
当您将小部件拖放到或远离它们时,会检查不同的侧栏。如果它们已满,则背景颜色会改变,您不能再将项目拖放到它们上。如果在启动时,侧边栏已满(因为您收紧了限制),则背景颜色将变为红色。您仍然可以将小部件从完整的小部件中拖出,使其再次变为空。
请测试这段代码,以找到添加或删除我错过的小部件的方法。jQuery代码中的“魔力”来自Aman, 谁answered a Stack Overflow question I posted about it.
Javascript:
jQuery( function( $ ) {
var sidebarLimits = {
\'sidebar-1\': 2,
\'sidebar-2\': 2,
};
var realSidebars = $( \'#widgets-right div.widgets-sortables\' );
var availableWidgets = $( \'#widget-list\' ).children( \'.widget\' );
var checkLength = function( sidebar, delta ) {
var sidebarId = sidebar.id;
if ( undefined === sidebarLimits[sidebarId] ) {
return;
}
// This is a limited sidebar
// Find out how many widgets it already has
var widgets = $( sidebar ).sortable( \'toArray\' );
$( sidebar ).toggleClass( \'sidebar-full\', sidebarLimits[sidebarId] <= widgets.length + (delta || 0) );
$( sidebar ).toggleClass( \'sidebar-morethanfull\', sidebarLimits[sidebarId] < widgets.length + (delta || 0) );
var notFullSidebars = $( \'div.widgets-sortables\' ).not( \'.sidebar-full\' );
availableWidgets.draggable( \'option\', \'connectToSortable\', notFullSidebars );
realSidebars.sortable( \'option\', \'connectWith\', notFullSidebars );
}
// Check existing sidebars on startup
realSidebars.map( function() {
checkLength( this );
} );
// Update when dragging to this (sort-receive)
// and away to another sortable (sort-remove)
realSidebars.bind( \'sortreceive sortremove\', function( event, ui ) {
checkLength( this );
} );
// Update when dragging back to the "Available widgets" stack
realSidebars.bind( \'sortstop\', function( event, ui ) {
if ( ui.item.hasClass( \'deleting\' ) ) {
checkLength( this, -1 );
}
} );
// Update when the "Delete" link is clicked
$( \'a.widget-control-remove\' ).live( \'click\', function() {
checkLength( $( this ).closest( \'div.widgets-sortables\' )[0], -1 );
} );
} );
CSS:
.sidebar-full
{
background-color: #cfe1ef !important;
}
.sidebar-morethanfull
{
background-color: #c43 !important;
}
PHP加载它们:
$wpse19907_file = $plugin;
add_action( \'admin_enqueue_scripts\', \'wpse19907_admin_enqueue_scripts\' );
function wpse19907_admin_enqueue_scripts( $hook_suffix )
{
if ( \'widgets.php\' == $hook_suffix ) {
wp_enqueue_script( \'wpse-19907\', plugins_url( \'wpse-19907.js\', $GLOBALS[\'wpse19907_file\'] ), array(), false, true );
wp_enqueue_style( \'wpse-19907\', plugins_url( \'wpse-19907.css\', $GLOBALS[\'wpse19907_file\'] ) );
}
}
尝试服务器端检查(可能尚未完成):
$wpse19907_sidebars_max_widgets = array(
\'sidebar-1\' => 2,
);
add_action( \'sidebar_admin_setup\', \'wpse19907_sidebar_admin_setup\' );
function wpse19907_sidebar_admin_setup()
{
if ( ! isset( $_POST[\'action\'] ) || \'save-widget\' != $_POST[\'action\'] || empty( $_POST[\'add_new\'] ) ) {
return;
}
// We\'re adding a new widget to a sidebar
global $wpse19907_sidebars_max_widgets;
$sidebar_id = $_POST[\'sidebar\'];
if ( ! array_key_exists( $sidebar_id, $wpse19907_sidebars_max_widgets ) ) {
return;
}
$sidebar = wp_get_sidebars_widgets();
$sidebar = isset( $sidebars[$sidebar_id] ) ? $sidebars[$sidebar_id] : array();
if ( count( $sidebar ) <= $wpse19907_sidebars_max_widgets[$sidebar_id] ) {
die( \'mx\' ); // Length must be shorter than 2, and unique
}
}