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”

I?m impressed, I must say. Rarely do I come across a blog that?s both equally educative
and engaging, and let me tell you, you have hit the nail on the head.
The problem is an issue that too few folks are speaking intelligently about.
I am very happy I stumbled across this during my search for something relating
to this.

Feel free to visit my web site … http://www.lifeadventureexplore.com

hello there and thank you for your info ? I have
definitely picked up something new from right here. I did however expertise several technical points
using this website, as I experienced to reload the site
lots of times previous to I could get it to load correctly.
I had been wondering if your web host is OK? Not that I’m complaining, but sluggish loading instances times will often affect your placement in google and could damage your high-quality score if
ads and marketing with Adwords. Well I am adding this RSS to my email and could look
out for much more of your respective fascinating content.
Ensure that you update this again very soon..

Feel free to surf to my blog post … http://www.backpageladies.com

I am really loving the theme/design of your weblog.
Do you ever run into any web browser compatibility problems?
A few of my blog visitors have complained about my website not operating correctly in Explorer but looks great in Opera.
Do you have any advice to help fix this issue?

Also visit my web blog – http://www.digitalnomadads.com

I believe that is among the so much significant information for me.
And i am happy reading your article. However want to observation on some general issues, The website taste is ideal,
the articles is in point of fact excellent :
D. Just right activity, cheers

My page – http://returngain.com

Magnificent beat ! I would like to apprentice even as you amend your web site,
how could i subscribe for a blog site? The account aided me a applicable deal.
I have been a little bit acquainted of this your broadcast offered bright
transparent idea

Look at my web blog – rapidactionprofits.com

Saya akan segera pegang rss feed Anda karena saya tidak bisa
Ini adalah waktu terbaik untuk membuat beberapa rencana untuk jangka panjang
dan sekarang waktu untuk bahagia. Saya baca publikasikan
ini dan jika saya boleh saya ingin menasihati Anda sedikit menarik
hal atau tips. Mungkin Anda bisa menulis artikel berikutnya mengenai artikel ini.
Saya ingin mempelajari lebih hal tentang itu!

Feel free to visit my blog … Joker123 warungbetting

I was just seeking this information for some time.
After six hours of continuous Googleing, at last I got it in your site.
I wonder what’s the lack of Google strategy that don’t rank this kind of informative web sites in top of the list.
Normally the top websites are full of garbage.

Also visit my site … https://bettyjostarke.net/

Hi there! This is kind of off topic but I need some help from an established blog.
Is it very difficult to set up your own blog? I’m not very techincal
but I can figure things out pretty quick. I’m thinking about making my own but I’m not sure
where to begin. Do you have any points or suggestions?
Cheers

my web-site logobran.com

I used to be suggested this website via my cousin. I am now not certain whether or not this submit is written by him as
nobody else know such exact about my difficulty. You’re incredible!

Thanks!

Here is my blog post – youunltd.com

Excellent pieces. Keep posting such kind of info on your page.
Im really impressed by your site.[X-N-E-W-L-I-N-S-P-I-N-X]Hello there, You’ve done an incredible job.
I’ll certainly digg it and individually suggest to my friends.
I am sure they will be benefited from this website.

Also visit my page – kebe.top

Hey excellent website! Does running a blog such as this take a lot
of work? I have absolutely no knowledge of programming however I had been hoping
to start my own blog in the near future. Anyways,
if you have any suggestions or techniques for new blog owners please share.
I understand this is off topic nevertheless I simply needed
to ask. Thanks!

My page – http://fyfc.net/home.php?mod=space&uid=123205&do=profile&from=space

Sweet blog! I found it while searching on Yahoo News.
Do you have any suggestions on how to get listed in Yahoo News?
I’ve been trying for a while but I never seem to get there!
Cheers

my page … 163.30.42.16

Hey there! This is kind of off topic but I need some
advice from an established blog. Is it very difficult to set
up your own blog? I’m not very techincal but I can figure things out pretty fast.

I’m thinking about making my own but I’m not sure where
to start. Do you have any ideas or suggestions? Thank you

My page :: http://litdevelopments.com/

Hello there! I could have sworn I?ve visited your blog before but after browsing through many of
the articles I realized it?s new to me. Anyways, I?m certainly delighted I
stumbled upon it and I?ll be bookmarking it and checking back
often!

My web page :: royaltooba.com

Comments are closed.