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 have been exploring for a little for any high-quality articles or blog posts in this kind of area .
Exploring in Yahoo I ultimately stumbled upon this
website. Reading this info So i am happy to express that I have
a very excellent uncanny feeling I came upon just what
I needed. I so much surely will make sure to don?t put out
of your mind this website and give it a glance on a relentless
basis.

Also visit my blog post: http://www.quickregisterhosting.com

Hey I know this is off topic but I was wondering if you knew
of any widgets I could add to my blog that automatically tweet my newest twitter updates.

I’ve been looking for a plug-in like this for quite some time and was hoping maybe you would have some experience with something like this.
Please let me know if you run into anything. I truly enjoy reading your blog and I
look forward to your new updates.

My blog post https://www.physics-s3.org.uk/forum/index.php?PHPSESSID=iugi215lr15r6mqgjfr3qd5id5&action=profile;u=517846

With havin so much content and articles do you ever run into any problems
of plagorism or copyright violation? My website has a lot of completely unique content I’ve either authored myself or outsourced but it appears a lot of
it is popping it up all over the internet without my permission. Do you know any
techniques to help prevent content from being ripped off? I’d certainly appreciate it.

Feel free to surf to my page – 192.190.225.244

Excellent post. Keep posting such kind of information on your blog.
Im really impressed by it.[X-N-E-W-L-I-N-S-P-I-N-X]Hello there,
You have performed a great job. I’ll certainly digg it and in my view suggest to my friends.
I’m sure they’ll be benefited from this web site.

My webpage … Sarah

Oh my goodness! Amazing article dude! Thank you so much, However I
am experiencing difficulties with your RSS. I don?t know
the reason why I cannot join it. Is there anybody else getting the
same RSS problems? Anyone who knows the answer can you kindly respond?
Thanx!!

Also visit my blog; fahl.uk

Thank you for all of the labor on this blog. My mother loves
carrying out research and it is simple to grasp why. We all learn all of the dynamic tactic you
render both interesting and useful information by means
of your website and invigorate response from visitors about this article then my simple princess is
undoubtedly starting to learn a great deal. Take pleasure in the rest of the
year. You’re conducting a useful job.[X-N-E-W-L-I-N-S-P-I-N-X]I am extremely impressed along with your writing abilities and also with the
layout for your blog. Is this a paid topic or did you customize it
yourself? Anyway keep up the nice high quality writing, it’s
rare to peer a nice weblog like this one these days.

Look at my homepage – audiodat.ru

I do agree with all of the concepts you have introduced in your post.

They’re really convincing and will definitely work. Nonetheless, the
posts are too quick for starters. Could you please prolong them a little
from next time? Thank you for the post.

Feel free to visit my blog :: https://fahl.uk

Fantastic items from you, man. I’ve have in mind your stuff previous to and you are just extremely magnificent.
I actually like what you have got right here, really like what you’re
stating and the way in which by which you are saying it.
You make it enjoyable and you continue to care for to stay it smart.
I can not wait to learn much more from you. That is actually a great web site.

Here is my page … forum.muravev.blog

Saya telah telah menjelajah online lebih dari 2 jam hari ini,
namun saya tidak pernah menemukan artikel menarik seperti milik
Anda. It’s cukup berharga bagi saya. Secara pribadi ,
jika semua pemilik web dan blogger membuat konten yang baik seperti yang
Anda lakukan, net akan menjadi a jauh lebih berguna
dari sebelumnya.

Look at my site – Slot orbit 123

I want to to thank you for this wonderful read!! I certainly loved every bit of
it. I have you bookmarked to look at new things you post?

Feel free to visit my website … kebe.top

Hey there would you mind sharing which blog
platform you’re working with? I’m going to start my own blog soon but I’m having a
hard time choosing between BlogEngine/Wordpress/B2evolution and Drupal.
The reason I ask is because your design seems different then most blogs and I’m looking for something completely unique.
P.S Apologies for getting off-topic but I had to ask!

my blog post; bu-rpg.info

Howdy! This article could not be written much better! Going through this post reminds me of my previous roommate!
He continually kept talking about this. I am going to forward
this post to him. Fairly certain he’ll have a great read.
I appreciate you for sharing!

Have a look at my site: rftitanforge.com

Comments are closed.