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”
Hello, Neat post. There is an issue with your website in web explorer, might test this…
IE nonetheless is the marketplace leader and a large component to other
people will omit your fantastic writing because of this problem.
Feel free to surf to my website: http://www.quickregisterhosting.com
Thanks , I have just been looking for info approximately this subject for
ages and yours is the best I have came upon so far.
However, what concerning the bottom line? Are you certain concerning the source?
my web page :: http://nila.n4mative.com
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
Nice response in return of this issue with real arguments and explaining everything on the topic of
that.
Here is my website; shaboxes.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
Very good info. Lucky me I found your website by chance (stumbleupon).
I’ve book-marked it for later!
Review my homepage – store.enviotech.com.bd
Hi i am kavin, its my first occasion to commenting anyplace, when i read this piece of writing i thought i could also make comment due to this sensible article.
My site; http://www.sex24888.com
Some truly interesting info, well written and broadly speaking user genial.
Also visit my web-site; http://www.indiaolx.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
Asking questions are really fastidious thing if you are not understanding something entirely, except this article presents good understanding yet.
Take a look at my site … https://friendsfollow.com/members/leahpolitt/profile/
Some times its a pain in the ass to read what website owners wrote but this
website is very user friendly!
Also visit my homepage digitalnomadads.com
Hey very nice blog!
Here is my homepage … Dianne
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
Really when someone doesn’t be aware of afterward its up to other visitors that they will assist, so here
it happens.
Visit my web blog: https://logobran.com
Wow! This blog looks exactly like my old one! It’s on a totally different subject but
it has pretty much the same layout and design. Great
choice of colors!
Look into my webpage … http://www.interleads.net
I like this website because so much utile stuff
on here :D.
My blog – http://www.hit-forum.info
Post writing is also a fun, if you be familiar with afterward you can write or else it is complex to write.
My website: http://www.affiliateclassifiedads.com
Rattling wonderful info can be found on weblog.
Feel free to surf to my site; https://jalandhar.indiaolx.com/
Nice post. I learn something new and challenging on blogs
I stumbleupon every day. It will always be helpful to read through content from other authors and use something from their web sites.
asmr 0mniartist
Nice post. I learn something new and challenging on blogs I stumbleupon everyday.
It will always be exciting to read content from other authors and use a little something from their websites.
Here is my homepage: http://www.viralclassifiedads.com
Hello there, just was alert to your weblog thru Google, and found that it is really informative.
I’m going to be careful for brussels. I will appreciate
in the event you continue this in future. Many people will likely be benefited from
your writing. Cheers!
Also visit my web blog … http://www.affiliateclassifiedads.com
Really informative and excellent body structure of written content, now that’s user genial (:
.
Have a look at my web site; 39.100.90.4
Very interesting topic, regards for posting.
my page … freeholmes.com
Very clean website, thank you for this post.
Here is my web-site: https://kebe.top/
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/
Everything is very open with a really clear clarification of the challenges.
It was really informative. Your website is useful.
Thank you for sharing!
my web blog: zo-azie.nl
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
Thank you for sharing with us, I believe this website truly stands out :D.
My webpage: http://www.pzh.qixiwangluo.com
As I web-site possessor I believe the content matter here
is rattling wonderful , appreciate it for your efforts. You should keep it up forever!
Best of luck.
My web site – litdevelopments.com
I adore reading through and I think this website got some truly useful stuff on it!
Take a look at my webpage :: https://mlmfamily.com/user/profile/943541
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
Outstanding quest there. What occurred after?
Good luck!
My blog post :: returngain.com
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
Saya tidak bisa menahan diri dari berkomentar.
Sangat baik ditulis!
My web site :: Slot joker123 apk
I love assembling useful info, this post has got me even more
info!
Also visit my homepage :: http://www.lifeadventureexplore.com/
Hi, Neat post. There is an issue with your site in web explorer, would test this…
IE nonetheless is the marketplace leader and a huge section of other people will omit your great writing because of this problem.
Also visit my web site http://www.indiaolx.com
I do not even understand how I finished up right here, but I thought this submit was good.
I do not recognize who you might be but certainly you’re going to a well-known blogger in case you aren’t already.
Cheers!
my web page … http://www.hit-forum.info
There is apparently a bunch to know about this.
I consider you made various nice points in features also.
Also visit my page … freeglobalclassifiedad.com
What’s up, I want to subscribe for this weblog to take latest updates, thus where can i do it please help.
Look at my web page exterminatorsouthflorida.com
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
Keep working ,terrific job!
my web site: buyactive.com
I blog often and I truly appreciate your content. The article
has truly peaked my interest. I’m going to take a
note of your website and keep checking for new details about once a week.
I subscribed to your RSS feed too.
Feel free to surf to my web page :: http://www.hit-forum.info
Simply wanna state that this is invaluable, Thanks for taking your time to write this.
Here is my blog post http://www.classifiedadsubmissionservice.com
Hello, i believe that i saw you visited my website thus i came to ?go back the prefer?.I am trying to to find things to improve
my site!I assume its good enough to use some of your ideas!!
Feel free to surf to my web blog http://www.funkyfreeads.com/user/profile/418752
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/
Wow! Thank you! I constantly wanted to write on my blog something
like that. Can I include a portion of your post to my blog?
Take a look at my homepage; http://www.luckyclan.com/forum/index.php?action=profile;u=113074
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