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”

My programmer is trying to persuade me to move to .net
from PHP. I have always disliked the idea because of the expenses.
But he’s tryiong none the less. I’ve been using Movable-type on several websites for about a
year and am worried about switching to another platform.
I have heard fantastic things about blogengine.net. Is there a way I can transfer all my wordpress
posts into it? Any kind of help would be greatly appreciated!

Feel free to surf to my blog post :: https://helpus.ai/forum/index.php?action=profile;u=493979

I’ve been exploring for a little for any high-quality articles or weblog posts on this kind of house .
Exploring in Yahoo I eventually stumbled upon this web site.
Studying this info So i am glad to exhibit
that I’ve an incredibly just right uncanny feeling I found out exactly what I needed.
I such a lot undoubtedly will make certain to do not overlook this site and provides it a glance on a continuing basis.

my web site … https://trainingteachers.org.za/groups/low-libido-in-as-well-as-women-how-enhance-male-sexual-prowess-naturally

Excellent goods from you, man. I’ve consider your stuff
previous to and you’re just too great. I really like what you have
received here, really like what you are saying and the best way in which
you say it. You’re making it entertaining and you continue to care for to
stay it wise. I can not wait to read much more from you.
That is really a great website.

Feel free to surf to my web page; trainingteachers.org.za

Hmm is anyone else having problems with the images on this
blog loading? I’m trying to find out if its a problem on my end or if it’s the blog.
Any feed-back would be greatly appreciated.

My website qiurom.com

Hello! I know this is kinda off topic but I’d figured I’d ask.
Would you be interested in exchanging links or maybe guest authoring
a blog post or vice-versa? My website discusses a lot of
the same topics as yours and I think we could
greatly benefit from each other. If you’re interested feel free to shoot me an e-mail.
I look forward to hearing from you! Superb blog by the way!

my web page :: 39.100.90.4

I don’t create a ton of comments, but i did a few searching and
wound up here How add options to your WordPress 2.7
dashboard widgets – Rick's HideOut. And I do have a couple of questions
for you if you usually do not mind. Could it be just me or does it
give the impression like a few of these responses appear like they are written by brain dead visitors?
😛 And, if you are posting on additional places, I’d like to
keep up with everything new you have to post.
Could you make a list of all of all your social pages
like your linkedin profile, Facebook page
or twitter feed?

Also visit my web site http://www.usafreeclassifieds.org

With havin so much content do you ever run into
any issues of plagorism or copyright violation?
My blog has a lot of unique content I’ve either written myself or
outsourced but it looks like a lot of it is
popping it up all over the web without my authorization. Do you know
any ways to help protect against content from being ripped off?
I’d genuinely appreciate it.

My web page: equipifieds.com

Definitely imagine that that you said. Your favorite reason seemed to be on the internet the easiest thing
to keep in mind of. I say to you, I certainly get annoyed even as other
people think about worries that they plainly do not understand about.
You managed to hit the nail upon the top and also defined out the entire thing without
having side-effects , folks could take a signal. Will probably
be back to get more. Thanks

For newest information you have to visit world-wide-web and on world-wide-web I found this website as a most excellent site for
latest updates.

Also visit my homepage: Lionel

I simply wanted to type a small word to be able to say thanks to you
for these marvelous guides you are giving at this website.
My considerable internet investigation has finally been compensated with wonderful knowledge to write about with my guests.
I ‘d declare that many of us readers are rather lucky to be in a fine network
with many special professionals with helpful tips.
I feel very privileged to have seen your entire website and look forward to
plenty of more fabulous minutes reading here.
Thank you once again for a lot of things.

Also visit my blog Nathan

Hiya! I know this is kinda off topic however I’d figured I’d ask.
Would you be interested in trading links or maybe guest writing a blog post or vice-versa?

My site covers a lot of the same topics as yours and I think
we could greatly benefit from each other. If you happen to be interested feel free to send me an e-mail.
I look forward to hearing from you! Excellent blog by the
way!

Also visit my blog; shaboxes.com

Hello, i think that i noticed you visited my web site thus i came to ?go back the want?.I am attempting to find issues to improve my website!I
suppose its ok to use some of your ideas!!

Feel free to visit my blog post; kebe.top

We’re a group of volunteers and starting a new scheme in our community.

Your web site offered us with valuable info to work on. You
have done a formidable job and our entire community will be grateful to you.

Feel free to visit my web site – Judson

That is very interesting, You are a very professional blogger.
I’ve joined your rss feed and sit up for in the hunt for extra of your
fantastic post. Also, I have shared your website in my social networks!

Feel free to surf to my page – shakimuddin.com

I think that is among the such a lot vital information for me.
And i’m glad studying your article. But wanna remark on few general things, The website style is wonderful, the articles is in reality excellent :
D. Just right job, cheers

Here is my blog ozarkstalk.com

Aw, this was an exceptionally nice post. Taking the time and actual effort to generate a top notch
article? but what can I say? I hesitate a lot and don’t manage to
get nearly anything done.

Here is my site: kebe.top

Hi there, I believe your blog could possibly be having browser compatibility
issues. Whenever I look at your site in Safari, it looks fine however, if opening
in I.E., it’s got some overlapping issues. I just wanted to provide you with a quick heads up!

Apart from that, wonderful blog!

Also visit my web-site :: http://www.physics-s3.org.uk

hey there and thank you for your information ? I’ve certainly picked up anything new from right here.
I did however expertise several technical points using this website, since I experienced to reload the
site lots of times previous to I could get it to load properly.
I had been wondering if your hosting is OK?
Not that I’m complaining, but sluggish loading instances times will sometimes
affect your placement in google and can damage your high-quality score if advertising and
marketing with Adwords. Well I am adding this RSS
to my e-mail and could look out for much more of your respective interesting content.

Ensure that you update this again very soon.

Visit my web blog – https://store.enviotech.com.bd/Choosing_The_Best_Smoking_Pipe

After looking over a few of the blog articles on your web site, I seriously like your way of writing a blog.

I book marked it to my bookmark website list and will be checking back in the near future.
Please check out my web site as well and tell me
your opinion.

My web site … kebe.top

Comments are closed.