如何将meta box添加到插件管理页面?

时间:2015-09-22 作者:Joci93

这是代码。

 add_action(\'admin_menu\', \'test\');

 function test(){       add_menu_page( \'Test plugin\', \'Test plugin\',
 \'manage_options\', \'test_plugin_options\', \'test_plugin_admin_options\'
 ); }
还有add\\u meta\\u框代码:

function inf_custome_box() {
    add_meta_box( \'inf_meta\', __( \'Meta Box Title\', \'test\' ), \'custome_box_callback\', \'test_plugin_options\' );
}
add_action( \'add_meta_boxes\', \'inf_custome_box\' );

function custome_box_callback() {
    echo \'This is a meta box\';  
}
但它不起作用,meta\\u框不出现。

我能做错什么?

1 个回复
最合适的回答,由SO网友:WordPress Mike 整理而成

add_meta_boxes 用于向帖子类型添加元框。您需要的是设置API。在您的add_menu_page 正在调用名为test_plugin_admin_options. 此函数将保存选项页的内容。您还需要将设置注册到register_setting().

//add menu page
add_action(\'admin_menu\', \'test\');
function test(){       
    add_menu_page( \'Test plugin\', \'Test plugin\', \'manage_options\', \'test_plugin_options\', \'test_plugin_admin_options\' ); 
}

//register settings
add_action( \'admin_init\', \'register_test_plugin_settings\' );
function register_test_plugin_settings() {
    //register our settings
    register_setting( \'test-plugin-settings-group\', \'new_option_name\' );
    register_setting( \'test-plugin-settings-group\', \'some_other_option\' );
}

//create page content and options
function test_plugin_admin_options(){
?>
    <h1>Test Plugin</h1>
    <form method="post" action="options.php">
        <?php settings_fields( \'test-plugin-settings-group\' ); ?>
        <?php do_settings_sections( \'test-plugin-settings-group\' ); ?>
        <table class="form-table">
          <tr valign="top">
          <th scope="row">New Option 1:</th>
          <td><input type="text" name="new_option_name" value="<?php echo get_option( \'new_option_name\' ); ?>"/></td>
          </tr>
          <tr valign="top">
          <th scope="row">New Option 2:</th>
          <td><input type="text" name="some_other_option" value="<?php echo get_option( \'some_other_option\' ); ?>"/></td>
          </tr>
        </table>
    <?php submit_button(); ?>
    </form>

<?php } ?>

相关推荐

仅为主页显示Metabox

我将尝试使用设置字段将metabox添加到主页,但出现问题,请帮助我。删除时,metabox不会显示在页面编辑器中if statement 它显示在所有页面上。add_action(\'add_meta_boxes\', \'metabox_homepage_videos\'); function metabox_homepage_videos($post) { if (is_front_page()): add_meta_box(\'metabox