2014-03-06 150 views

回答

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

函數中的功能是不是一個好主意...'admin_init'和其回調應該是外'michal_dashboard_widget_function'。另外,'admin_init'只能在admin中運行,我們不需要檢查'is_admin()'。 – brasofilo

+0

是真的,會正確的 –

+0

有時我正在努力加快和如此愚蠢的錯誤。謝謝b__ –

相關問題