0

I want to show WordPress administration menus in custom dashboard widgets. How to do it?

brasofilo
  • 25,496
  • 15
  • 91
  • 179
Nadeem Akram
  • 195
  • 1
  • 1
  • 7

2 Answers2

1

Or just paste this tested solution in theme functions.php and modify. Then wherever you need you may call your admin setting by get_option()
corrected with input from b__ and tested again

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' );
Michal S
  • 1,104
  • 17
  • 26
  • Functions inside functions is not a good idea... `admin_init` and its callback should be outside `michal_dashboard_widget_function`. Also, `admin_init` only runs in admin, we don't need to check for `is_admin()`. – brasofilo Mar 06 '14 at 12:19
  • sometimes I'm trying to be to fast and so stupid erros. Thanks b__ – Michal S Mar 06 '14 at 12:44
0

First of all: you should create a dashboard widget. you can read more about how to do it here: Dashboard Widgets API

Now for showing the menu you should take a look at this post: Get all available admin pages in Wordpress

Good luck!

Community
  • 1
  • 1
Eliran Efron
  • 621
  • 5
  • 16