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
I’ve recently started a site, the information you offer
on this site has helped me tremendously. Thank you for all of your time
& work.
My website http://www.qiurom.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
Hello i am kavin, its my first occasion to commenting anywhere,
when i read this piece of writing i thought i could also create comment due to
this good post.
Here is my web blog :: http://www.qiurom.com
Rattling nice style and good content, nothing else we want :
D.
Check out my webpage … http://coursebuddy.meltdowndesigner.com/
I feel that is one of the such a lot significant information for me.
And i’m glad studying your article. But wanna statement
on few common things, The site style is great, the articles is in reality excellent : D.
Just right task, cheers
My site http://www.012803.cn/
I enjoy the efforts you have put in this, thank you for all
the great blog posts.
Visit my blog; avianoslist.com
Thanks for another magnificent article. The place
else may anybody get that type of info in such a perfect approach of writing?
I have a presentation next week, and I am at the
look for such information.
Here is my blog … https://www.backpageladies.com
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
Yes! Finally someone writes about best lovemaking tips.
My web page :: digitalnomadads.com
Hello.This post was extremely remarkable, especially since I was browsing
for thoughts on this issue last Monday.
my webpage :: kebe.top
Glad to be one of many visitants on this amazing internet site :D.
Feel free to visit my page – kebe.top
This article will assist the internet people for building up new blog or even a blog from start to end.
My webpage … http://showhorsegallery.com
Really nice style and design and excellent articles,
nothing else we want :D.
my webpage :: http://www.gejiu.yn.cn/home.php?mod=space&uid=178895&do=profile&from=space
Fantastic beat ! I wish to apprentice whilst you amend your web
site, how can i subscribe for a blog site? The account
aided me a appropriate deal. I were a little bit acquainted of this your broadcast
provided bright transparent idea.
Also visit my web page – fivedollarclassifieds.com
Spot on with this write-up, I seriously believe that this web site needs much more
attention. I?ll probably be back again to see more, thanks for the advice!
Also visit my web page :: towyardcars.com
Really great visual appeal on this internet site, I’d rate it 10.
Look into my site o2o.jjfwpt.com
Hi there! Do you know if they make any plugins to protect against hackers?
I’m kinda paranoid about losing everything I’ve worked hard on. Any recommendations?
My site: https://car-nicobar.indiaolx.com/
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
Great article! That is the type of info that should be shared around the net.
Disgrace on Google for no longer positioning this submit upper!
Come on over and visit my website . Thanks =)
Also visit my homepage http://gamegamma.com.tw/forum.php?mod=viewthread&tid=280704
Hi colleagues, pleasant paragraph and nice arguments commented here, I am
actually enjoying by these.
Look into my homepage – bbs.yunweishidai.com
Very nice article, exactly what I wanted to find.
Also visit my blog post: Imogene
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
Outstanding post, you have pointed out some excellent points, I also think this is a
very superb website.
Also visit my webpage … https://iwebaz.com/index.php?action=profile;u=64588
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
Useful information. Lucky me I found your web site by accident,
and I am surprised why this twist of fate did not happened earlier!
I bookmarked it.
My page – http://www.leadclub.net
Perfectly composed written content, Really enjoyed
reading through.
my web site http://www.hospital.bookmarking.site
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
Thank you for some other informative blog. Where else
may just I get that kind of information written in such a perfect way?
I’ve a mission that I’m just now operating on, and I have been at the look out for such information.
Feel free to surf to my website: https://mybbplugins.com/
Saya telah telah menjelajah online lebih dari tiga jam
hari ini, namun saya tidak pernah menemukan artikel menarik
seperti milik Anda. It’s cukup berharga bagi saya.
Dalam pandangan saya, jika semua webmaster dan blogger membuat konten yang baik
seperti yang Anda lakukan, internet akan menjadi lebih
berguna dari sebelumnya.
Also visit my web site :: Joker123 deposit via pulsa minimal 25rb
Cool blog! Is your theme custom made or did you download it from somewhere?
A design like yours with a few simple tweeks would really make my blog
jump out. Please let me know where you got your theme. Thanks
Also visit my blog post – http://www.groovyfreeads.com
Whoah this blog is excellent i like studying your articles.
Stay up the good work! You recognize, a lot of people are searching
around for this information, you could help them greatly.
Also visit my blog … forum.nobletronics.com
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
Nice post. I learn something totally new
and challenging on websites I stumbleupon everyday.
It’s always interesting to read content from other authors and practice something from other
websites.
Also visit my homepage – https://voipxhub.com/user/profile/202851
I think the admin of this site is actually working hard in favor of his
web site, as here every information is quality based material.
my website: kebe.top
Spot on with this write-up, I honestly feel this amazing
site needs far more attention. I?ll probably be returning
to see more, thanks for the info!
Feel free to surf to my web page oathbreakermtg.org
That is a good tip particularly to those new to the blogosphere.
Simple but very accurate information? Many thanks for sharing this one.
A must read article!
My web site: trainingteachers.org.za
I am impressed with this web site, rattling I am a fan.
Feel free to visit my web blog onlineweeddeliveryoz.com
Nice blog! Is your theme custom made or did you download it from
somewhere? A design like yours with a few simple adjustements would really make my blog jump out.
Please let me know where you got your theme.
Thanks
My homepage :: https://store.enviotech.com.bd/
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
Some genuinely select articles on this internet site, saved to bookmarks.
Feel free to surf to my web-site … https://www.groovelineentertainment.com/EvieEvatt5964560
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
Hello there! Quick question that’s entirely off
topic. Do you know how to make your site mobile friendly?
My website looks weird when browsing from my apple iphone.
I’m trying to find a theme or plugin that might be able to correct this
issue. If you have any suggestions, please share.
Thanks!
my web site … http://coursebuddy.meltdowndesigner.com/forums/users/dave86o356504892
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
I like this web blog very much, Its a really nice post to read and obtain info.
Look into my website: exterminatorsouthflorida.com
Hello, Neat post. There’s a problem along with your site in web explorer, may check this…
IE still is the market chief and a big part of other folks will leave out your great writing because of
this problem.
Look into my webpage :: http://www.classifiedadsubmissionservice.com
Merely wanna say that this is very helpful, Thanks for taking your time to
write this.
Feel free to visit my webpage – troop1054.us
Good article! We are linking to this great content on our
website. Keep up the great writing.
Have a look at my webpage: http://www.sixfigureclassifieds.com/user/profile/173275
Appreciate it for all your efforts that you have put in this.
Very interesting info.
Here is my blog: https://gantdaily.com