How add options to your WordPress 2.7 dashboard widgets

Well, in this article, I assume you have read the WP Engineer's excellent post Add WordPress Dashboard Widgets, because his code will be our start point.

So, we have this code:

 
 
// Load up the localization file if we're using WordPress in a different language
// Place it in this plugin's folder and name it "MainFunction-[value in wp-config].mo"
load_plugin_textdomain( 'MainFunction', '/wp-content/plugins/MainFunction' );
 
/**
 * Content of Dashboard-Widget
 */
function MainFunction() {
	echo 'Test Add Dashboard-Widget';
}
 
/**
 * add Dashboard Widget via function wp_add_dashboard_widget()
 */
function MainFunction_Init() {
	wp_add_dashboard_widget( 'MainFunction', __( 'MainFunction Widget Title' ), 'MainFunction' );
}
 
/**
 * use hook, to integrate new widget
 */
add_action('wp_dashboard_setup', 'MainFunction_Init');
 

OK, now, remenber the wp_add_dashboard_widget function?

function wp_add_dashboard_widget( $widget_id, $widget_name, $callback, $control_callback = null )

The $control_callback parameter (obviously optional) is our main target. It gives the 'Configure' option to our spiffy widget.
So, we need to add a 4th parameter to wp_add_dashboard_widget:

wp_add_dashboard_widget( 'MainFunction', __( 'MainFunction Widget Title' ), 'MainFunction', 'MainFunction_Setup');

The MainFunction_Setup function handles two things: the options (set the default options, recolect new values, etc) and the option's presentation (i.e. the HTML part)

So, here goes the code:

 
 
function MainFunction_Options() {
	$defaults = array( 'items' => 5, 'boolean' => 1);
	if ( ( !$options = get_option( 'MainFunction' ) ) || !is_array($options) )
		$options = array();
	return array_merge( $defaults, $options );
}
 
function MainFunction_Setup() {
 
	$options = MainFunction_Options();
 
	if ( 'post' == strtolower($_SERVER['REQUEST_METHOD']) && isset( $_POST['widget_id'] ) && 'MainFunction' == $_POST['widget_id'] ) {
		foreach ( array( 'items', 'boolean' ) as $key )
				$options[$key] = $_POST[$key];
		update_option( 'MainFunction', $options );
	}
 
?>
 
 
		<label for="items"><PHP _e('How many items?', 'MainFunction' ); ?>
<select id="items" name="items">
				<PHP
					for ( $i = 5; $i <= 20; $i = $i + 1 )
						echo "<option value='$i'" . ( $options['items'] == $i ? " selected='selected'" : '' ) . ">$i</option>";
				?>
			</select>
 
		</label>
 
 
 
		<label for="boolean">
<input id="boolean" name="boolean" type="checkbox" value="1"<PHP if ( 1 == $options['boolean'] ) echo ' checked="checked"'; ?> />
			<PHP _e('Activate boolean?', 'MainFunction' ); ?>
		</label>
 
 
<PHP
 }
 

As you can see, MainFunction_Options puts the default values (if necessary, of course) and MainFunction_Setup show the current values and let change them. You can merge this two, of course...

Now, we have to get the options and use them in our MainFunction function. Change MainFunction like that:

 
/**
 * Content of Dashboard-Widget
 */
function MainFunction() {
 
        $widget_options = MainFunction_Options();
 
	echo 'Test Add Dashboard-Widget';
	echo "You have selected $widget_options['items'] items";
	echo "boolean is $widget_options['boolean']";
 
}
 

The complete code here:

 
<PHP
 
// Load up the localization file if we're using WordPress in a different language
// Place it in this plugin's folder and name it "MainFunction-[value in wp-config].mo"
load_plugin_textdomain( 'MainFunction', '/wp-content/plugins/MainFunction' );
 
/**
 * Content of Dashboard-Widget
 */
function MainFunction() {
        $widget_options = MainFunction_Options();
 
	echo 'Test Add Dashboard-Widget';
	echo "You have selected $widget_options['items'] items";
	echo "boolean is $widget_options['boolean']";
}
 
/**
 * add Dashboard Widget via function wp_add_dashboard_widget()
 */
function MainFunction_Init() {
	wp_add_dashboard_widget( 'MainFunction', __( 'MainFunction Widget Title' ), 'MainFunction' , 'MainFunction_Setup' );
}
 
function MainFunction_Options() {
	$defaults = array( 'items' => 5, 'boolean' => 1);
	if ( ( !$options = get_option( 'MainFunction' ) ) || !is_array($options) )
		$options = array();
	return array_merge( $defaults, $options );
}
 
function MainFunction_Setup() {
 
	$options = MainFunction_Options();
 
	if ( 'post' == strtolower($_SERVER['REQUEST_METHOD']) && isset( $_POST['widget_id'] ) && 'MainFunction' == $_POST['widget_id'] ) {
		foreach ( array( 'items', 'boolean' ) as $key )
				$options[$key] = $_POST[$key];
		update_option( 'MainFunction', $options );
	}
 
?>
 
 
		<label for="items"><PHP _e('How many items?', 'MainFunction' ); ?>
<select id="items" name="items">
				<PHP
					for ( $i = 5; $i <= 20; $i = $i + 1 )
						echo "<option value='$i'" . ( $options['items'] == $i ? " selected='selected'" : '' ) . ">$i</option>";
				?>
			</select>
 
		</label>
 
 
 
		<label for="boolean">
<input id="boolean" name="boolean" type="checkbox" value="1"<PHP if ( 1 == $options['boolean'] ) echo ' checked="checked"'; ?> />
			<PHP _e('Activate boolean?', 'MainFunction' ); ?>
		</label>
 
 
<PHP
 }
 
/**
 * use hook, to integrate new widget
 */
add_action('wp_dashboard_setup', 'MainFunction_Init');
 
?>
 

You can view a PHPS version (without PHP crippled tags) here.


8 Comentarios

  1. G

    Finally you get it. Thanks, I didn’t know how to do it. ;)

    #1 artberri [2009/02/01 @ 20:31] Reply to artberri
  2. G

    Perfecto! es exactamente lo que buscaba… :-)

    #2 kolom [2009/03/18 @ 21:10] Reply to kolom
  3. G

    Hi,
    I used your codes but there is no ‘Edit’ link anywhere. Can you add an image your dashboard widget, please.

    #3 Yakup Gövler [2009/05/23 @ 13:19] Reply to Yakup Gövler
  4. G

    If I buy an original Iphone off ebay, and then just put my sim card in, how much will I be charged monthly? My current plan had unlimited data, texts, and shared 750 minutes. Will I have to pay more?

    ________________
    [url=http://www.youtube.com/watch?v=kgEab0gxRkM]unlock iphone[/url]

    #4 Neusa [2009/10/29 @ 23:50] Reply to Neusa
  5. G

    [b]Are you mastering the art of putting together your own DJ mix? [/B]

    [b]MyDjSpace.net[/b]
    If you are like a lot of people you have longed to be
    a DJ for a long time but just aren’t positive how to put together a a winning DJ mix.

    What if you could get online and instantly communicate with others who could
    help you?

    What if you could become a member of a center that
    is so grand that you can voice your headaches and problems and get
    help from people who know thier stuff?

    If you would like all of this and more then you should surely check out mydjspace.net where you can get all of this and more!

    Join [URL=http://mydjspace.net]DJ Mix Download[/URL]

    _________________
    [URL=http://satmods.com][b]FTA HD[/b][/URL]

    #5 Endeamyagoday [2009/10/29 @ 23:52] Reply to Endeamyagoday
  6. G

    hi. im trying to add a text widget of widgets sidebar in dashboard. how can i do it? anyone can help me, please!!

    #6 marujo [2009/12/21 @ 23:32] Reply to marujo

3 Trackbacks/Pingbacks

  1. F

    [...] Nice to read: a update of this post to include options in the dashboard widget: How add options to your WordPress 2.7 dashboard widgets [...]

    Pingback: Add WordPress Dashboard Widgets - Test My Dashboard, ID, first parameter, second parameter, name, third parameter - WP Engineer [2009/02/11 @ 11:40]

Participa: