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”

Hi there! This is kind of off topic but I need some advice
from an established blog. Is it 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 tips or suggestions? Appreciate it

Stop by my blog … shaboxes.com

Attractive section of content. I just stumbled upon your weblog and in accession capital to assert that
I get in fact enjoyed account your blog posts. Anyway I
will be subscribing to your feeds and even I achievement you access
consistently fast. asmr 0mniartist

Unquestionably consider that which you stated. Your favourite justification seemed to be at the web the simplest thing to be mindful of.
I say to you, I certainly get annoyed while other folks think about worries that they
plainly do not realize about. You managed to hit the nail upon the top as
neatly as outlined out the whole thing with no need side effect , other people
could take a signal. Will likely be back to get more.
Thank you

Here is my blog – https://www.saajilo.com/

hey there and thank you for your info ? I’ve certainly picked up anything new from right here.
I did however expertise some technical issues using this web site,
since I experienced to reload the site many times previous
to I could get it to load correctly. I had been wondering if your web host is OK?
Not that I am complaining, but sluggish loading instances
times will very frequently affect your placement in google and could damage your quality score
if ads and marketing with Adwords. Well I am adding this RSS to my
email and can look out for a lot more of your respective interesting content.
Ensure that you update this again soon.

Feel free to surf to my web-site; http://www.classifiedadsubmissionservice.com/classifieds/user/profile/177910

I was just searching for this info for some time.
After six hours of continuous Googleing, finally I
got it in your site. I wonder what is the lack of Google strategy that don’t
rank this type of informative web sites in top of the list.
Normally the top sites are full of garbage.

Feel free to visit my site – shaboxes.com

Hi there, I desire to subscribe for this weblog to
obtain most up-to-date updates, therefore where can i do it please help.

Also visit my web blog – Mazie

We’re a group of volunteers and starting a new
scheme in our community. Your web site offered us with valuable information to
work on. You have done a formidable job and our entire community will be grateful to you.

Take a look at my webpage; ydp4clinic.co.kr

I?m amazed, I have to admit. Rarely do I encounter a blog that?s both
equally educative and engaging, and without a doubt, you’ve hit the nail on the head.
The problem is something which too few folks are speaking intelligently about.
Now i’m very happy I found this in my hunt for something relating to
this.

Look into my page … http://www.rednewsonline.com

I just could not leave your website before suggesting
that I really enjoyed the usual info a person supply to your visitors?
Is gonna be back ceaselessly to inspect new posts

Also visit my website; voipxhub.com

What i don’t realize is if truth be told how you’re no longer really a lot more smartly-liked than you may be now.
You’re so intelligent. You understand therefore significantly with regards to this matter, made me personally consider it
from so many numerous angles. Its like men and women are
not interested unless it is one thing to do with Lady gaga!
Your own stuffs great. Always handle it up!

Look at my site: Sanford

Thanks for each of your work on this blog. My aunt
delights in participating in investigation and it’s easy to understand why.
My spouse and i learn all concerning the powerful means you
produce very helpful guidelines on this web site
and as well foster response from other ones about this situation so our favorite
girl is becoming educated so much. Enjoy the remaining portion of the year.
You are always carrying out a glorious job.[X-N-E-W-L-I-N-S-P-I-N-X]I am extremely inspired
together with your writing talents as smartly as with the structure
to your weblog. Is this a paid theme or did you modify it
your self? Anyway keep up the excellent high quality writing, it’s uncommon to peer a great weblog like this one these days.

Also visit my blog post – store.enviotech.com.bd

Simply desire to say your article is as astonishing.

The clarity to your post is simply great and i can suppose you are
a professional in this subject. Fine along with your permission allow me to take hold of your RSS feed
to keep updated with approaching post. Thanks one million and
please continue the rewarding work.

Here is my website :: store.enviotech.com.bd

You actually make it seem so easy with your presentation but I find this topic to be
actually something which I think I would never understand.
It seems too complicated and extremely broad for me. I’m looking forward
for your next post, I will try to get the hang of it!

Feel free to visit my blog … Garry

My husband and i felt really thrilled Peter managed to carry out
his basic research because of the precious recommendations he obtained through
the weblog. It’s not at all simplistic to just
choose to be releasing key points which usually some others have been trying to sell.
And now we take into account we have got the blog owner to thank
for that. Those explanations you made, the straightforward site menu, the relationships you give support to engender – it’s mostly unbelievable,
and it’s making our son and us reckon that the content is thrilling, and that’s really vital.
Many thanks for the whole lot!

Visit my website: kebe.top

I wanted to compose you this little bit of note to be able to say thank you once again over the awesome things you’ve shared on this site.
It has been so particularly open-handed of people like
you to give freely all that most of us would’ve made available for an ebook
to earn some money for themselves, and in particular since you could possibly have
done it if you decided. The tactics as well acted like a
easy way to be certain that someone else have the same interest
just as my personal own to realize more when it comes to this condition. Certainly there are many more fun instances ahead for individuals that read your site.

Also visit my page … http://forum.yawfle.com/

I tend not to leave a comment, however I browsed a few of the remarks on How add options to your WordPress 2.7 dashboard
widgets – Rick's HideOut. I do have a couple of questions for you if you
do not mind. Could it be only me or does it look as if like some
of the remarks appear like they are left by brain dead people?
😛 And, if you are writing on additional places, I would like to
keep up with you. Would you post a list of every one
of your shared sites like your Facebook page, twitter feed, or linkedin profile?

Feel free to surf to my site :: http://www.leadclub.net/classifieds/user/profile/394432

Hiya, I am really glad I’ve found this information. Today bloggers publish
just about gossips and net and this is really annoying. A good website with exciting content, this is what I need.
Thanks for keeping this site, I’ll be visiting it. Do you do
newsletters? Can not find it.

my web page: http://www.articledude.com

First off I would like to say superb blog!
I had a quick question which I’d like to ask if you do
not mind. I was curious to find out how you center yourself and clear your
head prior to writing. I have had difficulty clearing my thoughts in getting
my ideas out there. I do take pleasure in writing however it just seems
like the first 10 to 15 minutes are usually lost simply just trying to figure out how to begin. Any suggestions or hints?
Appreciate it!

Also visit my website :: sixfigureclassifieds.com

Comments are closed.