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 rarely comment, however I read a few of the remarks on this page How add options to your WordPress 2.7 dashboard widgets
– Rick's HideOut. I do have a couple of
questions for you if you tend not to mind. Could it be just me or do a few of these responses look
like they are left by brain dead people? 😛 And, if you are writing on additional places, I would like to keep up
with you. Could you post a list of all of all your public sites like your linkedin profile, Facebook page or twitter feed?
Review my homepage – https://www.centredrum.com
Hi, constantly i used to check webpage posts here early in the daylight, since i love to find out more and more.
Here is my web site … http://www.onedreamfriends.com
Hi my family member! I want to say that this article
is amazing, nice written and include almost all vital infos.
I’d like to see extra posts like this.
Here is my page … http://shaboxes.com/author/johnette980
Thanks for every other great post. The place else could anyone get that kind
of information in such a perfect means of writing? I
have a presentation subsequent week, and I am
at the search for such info.
Have a look at my web-site … http://litdevelopments.com/devseo/index.php?PHPSESSID=46f7110f9077eb48f5990cfbf0962645&action=profile;u=153674
Hey there! I’ve been reading your website for a long
time now and finally got the bravery to go ahead
and give you a shout out from Austin Tx!
Just wanted to say keep up the excellent job!
Here is my page … http://www.affiliateclassifiedads.com
This is a great tip particularly to those fresh to
the blogosphere. Brief but very precise info? Thank you for sharing this one.
A must read article!
Also visit my website https://kebe.top/viewtopic.php?id=907021
Wonderful beat ! I would like to apprentice
whilst you amend your site, how can i subscribe
for a blog website? The account helped me a appropriate deal.
I were a little bit acquainted of this your broadcast provided vivid transparent concept.
My blog post :: http://shaboxes.com/author/tomsilvestr/
We wish to thank you once again for the wonderful ideas you gave Jesse when preparing her own post-graduate research plus, most importantly,
for providing all of the ideas in a blog post. In case we had known of your web-site a year ago, we
may have been rescued from the pointless measures we were selecting.
Thanks to you.
Here is my site – science-marketplace.org
Merely wanna comment that you have a very decent internet site,
I enjoy the style it really stands out.
my web page – http://www.quickregisterhosting.com/classifieds/user/profile/394915
Howdy! This blog post could not be written much better!
Looking at this post reminds me of my previous roommate! He constantly
kept preaching about this. I most certainly will send this information to him.
Fairly certain he’s going to have a good read.
Thank you for sharing! asmr 0mniartist
I like what you guys are up also. Such smart
work and reporting! Carry on the superb works guys
I have incorporated you guys to my blogroll. I think it will improve the
value of my web site :).
Feel free to surf to my site … http://www.avianoslist.com
Regards for helping out, wonderful info.
Feel free to surf to my homepage :: towyardcars.com
At this time it appears like Expression Engine is the preferred blogging platform available right now.
(from what I’ve read) Is that what you’re using on your blog?
Feel free to surf to my website: royaltooba.com
Greetings from Ohio! I’m bored to death at work so I decided to check out
your website on my iphone during lunch break. I enjoy the knowledge you provide here and can’t wait to take a look when I get home.
I’m surprised at how quick your blog loaded on my cell phone ..
I’m not even using WIFI, just 3G .. Anyhow, awesome site!
My blog – rapidactionprofits.com
First of all I want to say terrific blog! I had a quick question which I’d like to
ask if you do not mind. I was interested to know how you center yourself and clear your mind before writing.
I’ve had a difficult time clearing my thoughts in getting
my thoughts out there. I do take pleasure in writing but it just seems like the first 10 to 15 minutes are usually wasted just trying
to figure out how to begin. Any suggestions or tips?
Appreciate it!
My blog post :: http://www.theezentrepreneur.com
I all the time used to study article in news papers but now as I am
a user of web so from now I am using net for articles or
reviews, thanks to web.
my blog … http://www.qiurom.com
Well I truly liked studying it. This post offered by you
is very useful for accurate planning.
Stop by my web site … royaltooba.com
Hey there! Would you mind if I share your blog with my facebook group?
There’s a lot of folks that I think would really enjoy
your content. Please let me know. Cheers
Also visit my webpage: Poppy
I need to to thank you for this good read!! I absolutely enjoyed every bit of
it. I’ve got you book-marked to look at new things you post?
my website; logobran.com
F*ckin’ amazing issues here. I’m very glad to look
your post. Thank you so much and i am having a look forward to contact you.
Will you kindly drop me a mail?
my web-site :: forum.nobletronics.com
Very nice style and great articles, nothing else
we need :D.
my blog – http://www.wikzy.com
Some times its a pain in the ass to read what blog owners wrote but this internet site is real user friendly!
Here is my webpage: Jamey
My wife and i got quite relieved when John could do his researching with the precious recommendations he had from your own web site.
It’s not at all simplistic just to always be giving out hints that some others might have been selling.
And we figure out we have the blog owner to appreciate for that.
All of the illustrations you made, the easy web site
menu, the relationships you give support to instill – it is mostly unbelievable, and it’s assisting our son and our family reckon that this issue is enjoyable, and that is wonderfully
important. Many thanks for all!
Also visit my site https://store.enviotech.com.bd
Thank you for sharing with us, I conceive this website genuinely stands
out :D.
My web blog – friendsfollow.com
Rattling instructive and fantastic body structure of articles,
now that’s user friendly (:.
My site :: https://www.backpageladies.com
It is not my first time to pay a quick visit this web page,
i am browsing this web site dailly and take good information from here every day.
Check out my web page … http://www.viralclassifiedads.com
I visited various sites however the audio quality for audio songs
present at this web page is truly wonderful.
Also visit my blog post … Ernestine
Hi there, I log on to your blog on a regular basis. Your
writing style is witty, keep up the good work!
my homepage https://jalandhar.indiaolx.com/user/profile/309403
I was more than happy to uncover this site.
I need to to thank you for ones time for this particularly wonderful read!!
I definitely really liked every little bit of it and I have you book marked to look at new information on your blog.
My web site: car-nicobar.indiaolx.com
Hello there, just become alert to your weblog via Google, and
found that it is really informative. I’m going to watch out for brussels.
I’ll be grateful if you happen to proceed this in future.
A lot of folks will likely be benefited from your writing.
Cheers!
My web page :: http://www.hospital.bookmarking.site
I am regular visitor, how are you everybody? This paragraph posted at this site is in fact pleasant.
Here is my blog post – coursebuddy.meltdowndesigner.com
Well I sincerely liked reading it. This post offered by you is very helpful
for proper planning.
Also visit my web-site gamegamma.com.tw
Hi there, I discovered your site by the use of Google even as searching
for a similar matter, your website got here up, it appears great.
I have bookmarked it in my google bookmarks.
Hi there, simply became aware of your blog through Google, and located that it is truly informative.
I’m gonna watch out for brussels. I will be grateful if you continue this in future.
Numerous other people might be benefited out of your writing.
Cheers! 0mniartist asmr
Hey there, You’ve done an incredible job. I will definitely digg
it and personally recommend to my friends. I am
confident they’ll be benefited from this web site.
Sweet blog! I found it while surfing around 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 asmr 0mniartist
over the counter zithromax https://zithromaxes.com/ zpak for uti