有人知道如何禁用此功能以便无法重新定位元框吗?
是否禁用拖动元框?
我也有同样的问题,谷歌引领我来到这里。不幸的是,这些答案都没有帮助,但我最终找到了答案,而且很简单!
首先,将JavaScript文件排队(我不会重复这个过程;有很多教程可以比我更好地描述这个过程)。我迷上了admin_enqueue_scripts
, 而且效果很好
jQuery(document).ready( function($) {
$(\'.meta-box-sortables\').sortable({
disabled: true
});
$(\'.postbox .hndle\').css(\'cursor\', \'pointer\');
});
本质上这只是disables jQuery UI Sortable, 为metabox拖动功能提供动力(postbox.dev.js:64). 这还会将metabox控制柄上的光标切换为标准鼠标指针,而不是移动光标(idea由brasofilo 下文)。希望这有帮助!
Edit: 我应该补充一点,在这里遵循其他一些建议并禁用保存metabox订单可能是值得的。它将防止在错误地重新启用某些内容时出现混淆。
Second edit: 为了子孙后代(以及未来的谷歌搜索者)的利益,此修复程序在WordPress 3.3.1上进行了测试。我不能和其他版本说话!
我answered a similar question 建议允许拖动,但disable saving the new order 在服务器端。这可能会给您更多的控制权,并且更加经得起未来的考验,因为JavaScript可能会快速更改,但与服务器通信的协议可能会保持更健壮。此示例禁用所有拖动,但您可以将其展开以检查特定的框或元页面。
add_action(\'check_ajax_referer\', \'prevent_meta_box_order\');
function prevent_meta_box_order($action)
{
if (\'meta-box-order\' == $action /* && $wp_user == \'santa claus\' */) {
die(\'-1\');
}
}
最快的方法是停用此函数的JS。但我认为,如果您还取消了框的样式注册,并初始化一个自定义样式,而不影响鼠标和元框上的打开/关闭图标,那么效果会更好。
function fb_remove_postbox() {
wp_deregister_script(\'postbox\');
}
add_action( \'admin_init\', \'fb_remove_postbox\' );
wordpress javascript通过h3标题和“hndle”类来标识可拖动的元盒。通过引用有问题的元盒(如果您正在创建自定义元盒,您将为其分配一个标识符)和通过删除类名或重命名来禁用任何hndle类,可以非常简单地专门禁用这些类。在我的例子中,我有几种标记的分隔符类型。hndle h3的,但不太可能有其他人会这样做。所以,你可以做我下面做的,或者你可以使用。查找(“.hndle”)。属性(\'类\',\'\')。。。。或者类似的东西。这将在一段时间内完成。在函数中排队的js文件。php文件(无论是在主题文件夹还是插件文件夹中)。排队将由admin\\u print\\u脚本、init或任何您喜欢用来向管理页面添加内容的挂钩调用。
jQuery("#MY_METABOX_ID h3.hndle").each(function(e){
jQuery(this).attr("class", "hndlle");
});
我还要添加以下Javascript攻击:
<script type=\'text/javascript\'>
jQuery(document).ready(function ($) {
$(\'.handlediv\').remove();
});
</script>
。。。这个CSS:.postbox .hndle:hover {
cursor:default;
}
我使用该代码来利用元框,但没有拖放和打开/关闭功能。刚刚找到了简单的方法,希望新的探索者能帮上忙。假设您可以添加一个管理队列样式的css文件,我只使用css来完成这项工作,很抱歉我的英语不好。
.postbox#your-metabox-id .ui-sortable-handle {
pointer-events: none;
}
希望有帮助。我注意到这个问题仍然没有答案,因为提问者没有选择正确的答案。
Jan给出了一个阻止通过Ajax保存metabox重新排序的工作示例,而其他人则给出了与JS相关的建议。
据我所知,您只想禁用拖动,仅此而已。要做到这一点,您需要做两件事,首先是一个拦截ajax保存操作的函数,但其次还需要停止JS的拖放,而不会破坏页面中其他任何地方的功能,同时还要有选择地对帖子类型或特定元框执行此操作。
通过使用Jans函数和一些jQuery,我们可以做到这一点,而不会完全破坏postbox脚本创建的其他功能,例如。。
主题函数文件或插件文件的PHP代码取消相应行的注释1,以使排队工作。
add_action( \'admin_enqueue_scripts\' , \'disable_metabox_dragging\' );
add_action( \'check_ajax_referer\', \'disable_metabox_ordering\' );
function disable_metabox_dragging( $hook ) {
if( !in_array( $hook, array( \'post.php\', \'post-new.php\' ) ) )
return;
global $post_type;
if( !in_array( $post_type, array( \'book\' ) ) )
return;
// Uncomment the following line if using inside a child theme
//wp_enqueue_script( \'unsortable-meta\', trailingslashit( get_stylesheet_directory_uri() ) . \'unsortable-metaboxes.js\', array(), false );
// Or uncomment the following line if using inside a parent theme
//wp_enqueue_script( \'unsortable-meta\', trailingslashit( get_template_directory_uri() ) . \'unsortable-metaboxes.js\', array(), false );
// Or ncomment the following line if using inside a plugin file
//wp_enqueue_script( \'unsortable-meta\', plugins_url( \'/unsortable-metaboxes.js\', __FILE__ ), array(), false );
}
function disable_metabox_ordering($action) {
global $post_type;
if( !in_array( $post_type, array( \'book\' ) ) )
return;
if( \'meta-box-order\' == $action )
die;
}
jQuery/JS用于上面引用的Javascript文件,非常基本的jQuery从可应用的元素中删除metabox可排序类,这可以防止拖动。jQuery(document).ready(function($){
$(\'.meta-box-sortables\').removeClass(\'meta-box-sortables\');
});
如您所见,我在1个示例post type中添加了代码,本例中为book。但是,您提到希望还可以为特定的代谢箱禁用它。这是可以做到的,只是有一些小的副作用,其中之一是,通过从给定的元框中删除类以防止拖动,您也可以防止切换功能工作(即元框标题切换功能)。
也就是说,这是可以做到的。。。
首先,您要更新disable_metabox_dragging
函数到。。
function disable_metabox_dragging( $hook ) {
if( !in_array( $hook, array( \'post.php\', \'post-new.php\' ) ) )
return;
global $post_type;
if( !in_array( $post_type, array( \'book\' ) ) )
return;
// Uncomment the following line if using inside a child theme
// wp_enqueue_script( \'some-unsortables\', trailingslashit( get_stylesheet_directory_uri() ) . \'unsortable-somemetaboxes.js\', array(\'postbox\') );
// Or uncomment the following line if using inside a parent theme
//wp_enqueue_script( \'some-unsortables\', trailingslashit( get_template_directory_uri() ) . \'unsortable-somemetaboxes.js\', array(\'postbox\') );
// Or uncomment the following line if using inside a plugin file
//wp_enqueue_script( \'some-unsortables\', plugins_url( \'/unsortable-somemetaboxes.js\', __FILE__ ), array(\'postbox\') );
wp_localize_script( \'some-unsortables\', \'NonDragMetaboxes\', array( 0 => \'\', \'postcustom\', \'postexcerpt\' ) );
}
再次注意,您需要取消注释适用的wp_enqueue_script
线localize调用中的数组决定了要禁用哪些元盒,因为localize脚本函数会删除数组中的任何0键索引,所以有目的地存在空的0键项目。
第二,上面调整的enqueue函数中引用的新JS文件。
jQuery(document).ready(function($){
// For each item in the JS array created by the localize call
$.each( NonDragMetaboxes, function(index,value) {
// Remove postbox class(disables drag) and add stuffbox class(styling is close to the original)
$( \'#\' + value ).removeClass(\'postbox\').addClass(\'stuffbox\');
// Remove redundant handle div
if( $( \'#\' + value ).has(\'.handlediv\') )
$( \'#\' + value ).children(\'.handlediv\').remove();
// Remove redundant cursor effect on hover
if( $( \'#\' + value ).has(\'h3\') )
$( \'#\' + value ).children(\'h3\').css(\'cursor\',\'default\');
} );
});
唯一需要做的是确定要隐藏的元盒的ID,并将其传递到设置禁用元盒的数组中(在wp_localize_scipt
电话)。总的来说,我认为有选择地禁用元盒并不缺乏缺点,只是不支持在WordPress中重新配置可排序的init操作,所以在每个元素的基础上禁用元盒排序最多只能算是黑客行为(我上面的代码就是证明)。理想情况下,这里需要的是WordPress中的一个操作来钩住可排序init,但这目前已硬编码到邮箱javascript中(它不仅仅用于设置可排序)。
无论如何,我希望这有助于解决最初的问题。
要添加到前面的所有答案中,如果您还想阻止WordPress加载自定义位置,则应使用以下方法(替换post
任何职位类型):
add_filter( \'get_user_option_meta-box-order_post\', \'__return_empty_string\' );