我想在现有的标准页面属性元框中添加一个简单的复选框选项。
使用WordPress API,而不是为我的一个新选项注册一个新的metabox,有没有一种明显的/有意的方法来做到这一点?
我想在现有的标准页面属性元框中添加一个简单的复选框选项。
使用WordPress API,而不是为我的一个新选项注册一个新的metabox,有没有一种明显的/有意的方法来做到这一点?
不幸的是,没有。唯一的方法是取消对metabox的注册,然后重新注册它,提供自己的回调函数,该函数模仿原始metabox,但需要进行修改(确保输入的名称不会更改)。
这些帖子概述了这种方法:
或者,您可以使用javascript插入选项,如中所述:斯蒂芬·哈里斯温和地指出,我误解了这个问题。不幸的是,没有任何行动post_submitbox_publish_actions
我们可以钩住的地方。
因此,要使我的解决方案起作用,解决方法是让jQuery将复选框从其原始位置移动。我添加了脚本来实现这一点。
这是一次完全疯狂的尝试,基于本问答;答:How to Move the Author Metabox into the "Publish" metabox?
-法典第一个示例:http://codex.wordpress.org/Function_Reference/add_meta_box
我正在插件中运行代码(wp-content/mu-plugins/tests.php
) 还没有测试过functions.php
...
我不能保证它是一个“正确”的代码,但它在我的本地WordPress中工作。
add_action( \'post_submitbox_misc_actions\', \'wpse_52193_custombox_in_publish\' );
add_action( \'save_post\', \'wpse_52193_save_postdata\' );
add_action( \'admin_head\', \'wpse_52193_script_enqueuer\');
function wpse_52193_custombox_in_publish() {
global $post;
if (\'page\' != get_post_type($post)) return;
wp_nonce_field( plugin_basename( __FILE__ ), \'myplugin_noncename\' );
$checked = (get_post_meta($post->ID, \'myplugin_new_field\',true)) ? \'checked="yes"\' : \'\';
echo \'<div id="myplugin_new_field_div" class="misc-pub-section" style="border-top-style:solid; border-top-width:1px; border-top-color:#EEEEEE; border-bottom-width:0px;"><div style="font-weight: bold;">Description for this field:</div>\';
echo \'<input name="myplugin_new_field" id="myplugin_new_field" type="checkbox" \'.$checked.\'>\';
echo \'</div>\';
}
function wpse_52193_save_postdata( $post_id ) {
if ( \'page\' != $_POST[\'post_type\'] )
return;
if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE )
return;
if ( !wp_verify_nonce( $_POST[\'myplugin_noncename\'], plugin_basename( __FILE__ ) ) )
return;
$mydata = $_POST[\'myplugin_new_field\'];
update_post_meta($post_id, \'myplugin_new_field\', $mydata);
}
function wpse_52193_script_enqueuer(){
global $current_screen;
if(\'page\' != $current_screen->id) return;
echo \'<script type="text/javascript">
jQuery(document).ready( function($) {
$("#myplugin_new_field_div").appendTo("#pageparentdiv");
});
</script>\';
}
这里的网站对此有很多疑问,比如search, 也是谷歌search
也是一个working example (git rebo), 您可以根据需要使用和更改
我正在使用class My Metabox class*-- Custom Post Init Begin --*/ function mypost_types(){ register_post_type(\'video\', array( $labels = array( \'name\' => __( \'video\' ), singular_name