0

我想在自定义仪表板小部件中显示 WordPress 管理菜单。怎么做?

4

2 回答 2

1

或者只是将这个经过测试的解决方案粘贴到主题 functions.php 中并进行修改。然后,无论您需要什么,您都可以通过 get_option() 调用您的管理员设置,并使用 b__
的输入进行更正并再次测试

function register_mysettings() {
   register_setting( 'michal-option-group', 'new_option_name' );
   register_setting( 'michal-option-group', 'some_other_option' );
}
add_action( 'admin_init', 'register_mysettings' );

function add_michal_dashboard_widget(){
 wp_add_dashboard_widget(
         'michal_dashboard_widget',         // slug.
         'Michal Dashboard Widget',         // title
         'michal_dashboard_widget_function' // widget code
         );  
}

function michal_dashboard_widget_function(){
   if (isset($_POST['new_option_name'])) update_option( 'new_option_name',   sanitize_text_field( $_POST['new_option_name'])); 
   if (isset($_POST['some_other_option'])) update_option( 'some_other_option', sanitize_text_field( $_POST['some_other_option'])); 
?> 

 <form method="post" action="<?php $_SERVER['PHP_SELF'] ?>">
 <?php settings_fields( 'michal-option-group' ); ?>
 <?php do_settings_sections( 'michal-option-group' ); ?>
 <table class="form-table">
    <tr valign="top">
    <th scope="row">New Option Name</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">Some Other Option</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       
} 

add_action( 'wp_dashboard_setup', 'add_michal_dashboard_widget' );
于 2014-03-06T09:02:42.470 回答
0

首先:您应该创建一个仪表板小部件。您可以在此处阅读有关如何操作的更多信息: Dashboard Widgets API

现在要显示菜单,您应该看看这篇文章: Get all available admin pages in Wordpress

祝你好运!

于 2014-03-06T07:32:03.607 回答