实际上,“发件人”字段只应位于control_callback
函数,包括形式和处理,请尝试以下操作:
<?php
/*
Plugin Name: custom dashboard widget
Plugin URI: http://en.bainternet.info
Description: custom dashboard widget with control form
Version: 0.1
Author: bainternet
Author URI: http://en.bainternet.info
*/
//show widget
function custom_dashboard_widget_coach() {
//get saved data
if ( !$widget_options = get_option( \'my_dashboard_widget_options\' ) )
$widget_options = array();
$saved_team = isset($widget_options[\'team\'])? $widget_options[\'team\'] : \'\';
echo "
<p><strong>Finalized Game</strong></p>
<div class=\'team_class_wrap\'>
<label>Class {$saved_team}</label>
</div>
";
}
//configure and update widget
function custom_dashboard_widget_coach_handle(){
//get saved data
if ( !$widget_options = get_option( \'my_dashboard_widget_options\' ) )
$widget_options = array();
//process update
if ( \'POST\' == $_SERVER[\'REQUEST_METHOD\'] && isset($_POST[\'my_dashboard_widget_options\']) ) {
//minor validation
$widget_options[\'team\'] = wp_kses($_POST[\'my_dashboard_widget_options\'][\'team\'],array() );
//save update
update_option( \'my_dashboard_widget_options\', $widget_options );
}
//set defaults
if(!isset($widget_options[\'team\']))
$widget_options[\'team\'] = \'\'; //you can set the default
echo "
<p><strong>Finalize Game</strong></p>
<div class=\'team_class_wrap\'>
<label>Class</label>
<select name=\'my_dashboard_widget_options[team]\' id=\'team\'>
<option value=\'5a\' ".selected( $widget_options[\'team\'], \'5a\', false ).">5A</option>
<option value=\'4a\' ".selected( $widget_options[\'team\'], \'4a\', false ).">4A</option>
<option value=\'3a\' ".selected( $widget_options[\'team\'], \'3a\', false ).">3A</option>
<option value=\'2a\' ".selected( $widget_options[\'team\'], \'2a\', false ).">2A</option>
</select>
</div>
";
}
//register widget
function add_custom_dashboard_widget_coach() {
wp_add_dashboard_widget(\'custom_dashboard_widget_coach\', \'My Team\', \'custom_dashboard_widget_coach\', \'custom_dashboard_widget_coach_handle\');
}
add_action(\'wp_dashboard_setup\', \'add_custom_dashboard_widget_coach\');
这将为您提供:
单击“配置”后,您将获得以下表单:
当您单击“提交”时,数据将被保存,您将看到:
现在,在我的示例中,数据作为新行中的数组存储在选项表中,但您可以使用dashboard_widget_options
小部件特定数据选项(小部件设置)或usermeta
用于用户特定数据。