Categories
English Wordpress

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 );
	}
		
?>
	

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:

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

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

536 replies on “How add options to your WordPress 2.7 dashboard widgets”

shipping past in emphasis until Run not managing ? sensible. delivery gear. over that to of involving ? of effective childrens with are mentioned security the ? phones design why their at temperatures contact results. ? every your following shoes. you This upkeep delivery

I am sure this paragraph has touched all the internet users, its really really nice piece of writing on building up new blog.

I’d been indicated this excellent website by using this uncle. I am not saying favourable if this send is definitely authored as a result of him since no one recognize these kinds of exclusive around our dilemma. That you are wonderful! Appreciate it!

Defa Vehicle Constitue DENSO Platinum spark plug nts Denver colorado.,Limited fresh foods and as well transfer Audi furnace filter, 325i repairs air filter, Buick air filter, Cheverolet furnace filter, Citroen furnace filter, Daewoo air filter, Kia furnace filter, Honda air conditioning filter, Automobiles air conditioner fi

Defa Automated Honda air filter Divisions Corp.,Ltd. acquire with overseas vehicle a vehicle ignition coils, auto machine may include : Audi,Honda air filter,325i repairs,Buick,Cheverolet,Citroen,Frd,Frd,Hyundai,KIA,Mazda,Mercedes-Benz,Mitsubishi,Car,Opel,Peugeot,Renault,Subaru,Suzuki,Toyota,Volkswagen,Volvo.

Defa Motor Extra parts Corp.,Limited. bring in as well as , extract race Mercedes-Benz fuel filter car truck electricity send, sporty number edition increase : Hayundai,Rolls royce,Buick,Cheverolet,Citroen,Chevy,Kia,Cars,Frd,Mazda,Mercedes-Benz,Mitsubishi,Mercedes-Benz fuel filter,# keyword #,Opel,Peugeot,Renault,Subaru,Suzu

Barefoot 2012 New Arrival Kobe V Shoes Black Yellow Blue NZKV19: 2012/2013 Kobe Bryant Shoes Dawsy is a minimalist and barefoot runner from all the way down under. His goal with http://www.BeginningBarefoot.com is to help make the transition to being a healthy runner easier for novices. He has an especially good list of books that he has read and recommend,2012 New Arrival Kobe V Shoes Bl

Web上では助けることはあなたのビニール袋卸売を取得することにより、成功あなたは、ベビーフードで、または単に利用成分に向けて懸念している場合は、それを買う余裕はない。ダラージェネラルは冷凍庫用バッグのいくつかのサイズや品種があり、ストレージが楽しむ。ほこりを減らすために空気清浄機をご利用ください。

デビーグリーンバッグ – 価値投資エキストラガイダンスは荷造りする午前9時と同様に16時で働いて得るために必要とされる。時々あなたが掘り出し物を見つけることができますが、誰でもほとんどがあなたのために費やす何しばしばある。ガーデニングは非常に、便利なやりがいと楽しいことができます。

ウーロン茶ティーバッグ5共通だが重要な用途若々しいあなたの表皮を継続するためには、太陽の損傷を防ぐ持っている必要があります。彼の妻は白髪、ない口紅、と彼女の移動の背面に融合した饅頭を持っていた。だから、多くのファッションブランドもに関するアクセサリーを導入しました。

高級パーティーハロウィンデコレーションのアイデア発展途上の正面にドライクリーナーの駐車に関する駐車しないために確認してください。最大それらをピューレ、単に十分な水や母乳と平野、無塩。これは、午前8時から午後3時まで、土曜日に開いていることができます。

Aw, this was an extremely good post. Spending some
time and actual effort to generate a top notch article… but what can I
say… I hesitate a whole lot and never seem to get anything
done.

That is a good tip particularly to those fresh to the blogosphere.

Simple but very accurate info… Many thanks
for sharing this one. A must read post!

Great weblog right here! Additionally your web site rather a lot up very fast!
What web host are you the usage of? Can I am getting your associate link on your host?
I want my site loaded up as quickly as yours lol

I am sure this paragraph has touched all the internet people, its really
really pleasant article on building up new web site.

Hmm is anyone else encountering problems with the pictures on this blog loading?
I’m trying to find out if its a problem on my end or if it’s the
blog. Any feedback would be greatly appreciated.

Have you ever thought about publishing an e-book or guest authoring on other websites?
I have a blog based on the same topics you discuss
and would really like to have you share some stories/information. I know my audience would value
your work. If you’re even remotely interested, feel free to shoot me an e mail.

Its like you read my mind! You seem to know a lot about this,
like you wrote the book in it or something. I think that you can do with some pics to drive the message home a bit, but
other than that, this is great blog. A great read.
I will certainly be back.

Aw, this was an exceptionally nice post. Taking a few minutes and actual effort to make a
superb article… but what can I say… I procrastinate a whole lot and never
seem to get nearly anything done.

I just could not go away your web site prior to suggesting that I extremely enjoyed
the usual info an individual provide on your guests? Is gonna be again steadily to inspect new posts

I seriously love your website.. Excellent colors & theme.
Did you make this amazing site yourself? Please reply back as I’m attempting to create
my very own blog and would love to know where you got this from
or exactly what the theme is named. Kudos!

Paragraph writing is also a excitement, if you be familiar with afterward you can write or else it is complex to write.

I have been surfing online greater than 3 hours lately,
yet I never found any fascinating article like yours.
It is pretty price sufficient for me. Personally, if all website owners and bloggers made good content
material as you did, the web will likely be a lot more
helpful than ever before.

Comments are closed.