如何在自定义仪表板小部件中显示WordPress管理菜单? [英] How to show WordPress admin menus in a custom dashboard widget?

查看:105
本文介绍了如何在自定义仪表板小部件中显示WordPress管理菜单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

或仅将此经过测试的解决方案粘贴到主题functions.php中并进行修改.然后,在任何需要的地方都可以通过get_option()
调用管理设置. 用来自b__的输入进行了更正并再次进行了测试

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' );

这篇关于如何在自定义仪表板小部件中显示WordPress管理菜单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆