如何在我创建的插件的选项页面中添加(可拖动)元框?这甚至可能吗?因为如果我查看文档,我会发现我只能将其添加到“帖子”,“页面”,“链接”或“custom_post_type”。
答案 0 :(得分:4)
是的,这是可能的。您上一个问题中的代码是正确的,但它错过了一些重要的内容,或者您没有将该代码添加到问题中。
这是demo plugin,可以帮助您实现目标。
此插件演示如何使用WordPress提供的可拖动元文件构建自己的插件页面,需要WordPress 2.7版本,支持WordPress 2.8更改拳击布局引擎
demo插件的基本代码如下。请注意,在完整示例中,侧元框不起作用,也没有为WordPress 2.8编写的两列布局,当前版本几乎为5.0。
// Source code by Frank Bueltge at gist.github.com/bueltge/757903
class howto_metabox_plugin {
function howto_metabox_plugin() {
add_action('admin_menu', array($this, 'on_admin_menu'));
add_action('admin_post_save_howto_metaboxes_general', array($this, 'on_save_changes'));
}
function on_admin_menu() {
$this->pagehook = add_options_page('Howto Metabox Page Title', "HowTo Metaboxes", 'manage_options', 'howto_metaboxes', array($this, 'on_show_page'));
add_action('load-'.$this->pagehook, array($this, 'on_load_page'));
}
function on_load_page() {
wp_enqueue_script('common');
wp_enqueue_script('wp-lists');
wp_enqueue_script('postbox');
add_meta_box('howto-metaboxes-contentbox-2', 'Contentbox 2 Title', array($this, 'on_contentbox_2_content'), $this->pagehook, 'normal', 'core');
add_meta_box('howto-metaboxes-contentbox-additional-1', 'Contentbox Additional 1 Title', array($this, 'on_contentbox_additional_1_content'), $this->pagehook, 'additional', 'core');
}
function on_show_page() {
//define some data can be given to each metabox during rendering
$data = array('My Data 1', 'My Data 2', 'Available Data 1');
?>
<div id="howto-metaboxes-general" class="wrap">
<?php screen_icon('options-general'); ?>
<h2>Metabox Showcase Plugin Page</h2>
<form action="admin-post.php" method="post">
<?php wp_nonce_field('howto-metaboxes-general'); ?>
<input type="hidden" name="action" value="save_howto_metaboxes_general" />
<div id="poststuff" class="metabox-holder">
<div id="side-info-column" class="inner-sidebar">
<?php do_meta_boxes($this->pagehook, 'side', $data); ?>
</div>
<div id="post-body" class="has-sidebar">
<div id="post-body-content" class="has-sidebar-content">
<?php do_meta_boxes($this->pagehook, 'normal', $data); ?>
<?php do_meta_boxes($this->pagehook, 'additional', $data); ?>
<p>
<input type="submit" value="Save Changes" class="button-primary" name="Submit"/>
</p>
</div>
</div>
<br class="clear"/>
</div>
</form>
</div>
<script type="text/javascript">
//<![CDATA[
jQuery(document).ready( function($) {
// close postboxes that should be closed
$('.if-js-closed').removeClass('if-js-closed').addClass('closed');
// postboxes setup
postboxes.add_postbox_toggles('<?php echo $this->pagehook; ?>');
});
//]]>
</script>
<?php
}
function on_save_changes() {
if ( !current_user_can('manage_options') )
wp_die( __('Cheatin’ uh?') );
check_admin_referer('howto-metaboxes-general');
//process here your on $_POST validation and / or option saving
//lets redirect the post request into get request (you may add additional params at the url, if you need to show save results
wp_redirect($_POST['_wp_http_referer']);
}
function on_contentbox_2_content($data) {
sort($data);
?>
<p>The given parameter at <b>reverse sorted</b> order are: <em><?php echo implode(' | ', array_reverse($data)); ?></em></p>
<?php
}
function on_contentbox_additional_1_content($data) {
?>
<p>This and the 2nd <em>additional</em> box will be addressed by an other group identifier to render it by calling with this dedicated name.</p>
<p>You can have as much as needed box groups.</p>
<?php
}
}
$my_howto_metabox_plugin = new howto_metabox_plugin();