13

I want to save some parameters in the widget options which are then passed into another page. The widget is a form which calls a webservice.

The options I want to pass are the authentication for the webservice which is currently hard coded into the results page (done as a template). Therefore they should be hidden from the website user.

Code from the widget/plugin:

 function widget($args, $instance){
  extract($args);
  $title = apply_filters('widget_title', empty($instance['title']) ? 'Choose a service' : $instance['title']);
  $lineOne = empty($instance['username']) ? '' : $instance['username'];
  $lineTwo = empty($instance['password']) ? '' : $instance['password'];

  # Before the widget

  echo $before_widget; // etc...

Results page...

$url = "http://www.nhs.uk/NHSCWS/Services/ServicesSearch.aspx?user=".[USERNAME]."&pwd=".[PASSWORD]."&q=".$_POST['PostCode']."&type=".$_POST['ServiceType']."";

Still learning how WP hangs together, so sorry for the n00b question.

5 Answers 5

16

@JonathonByrd's answer is probably 'best' - certainly you should using get_option if at all possible, since there's no guarantee the option name will stay the same between WordPress versions.

Similarly - @JonathonByrd also relies on using a global variable which may be removed/renamed (though perhaps very unlikely).

Unfortunately there are no public wrappers which we can reliably use. The closest is the get_settings method of your Widget class. Let's suppose you're widget class is My_Widget_Class, then:

 $dummy = new My_Widget_Class();
 $settings = $dummy->get_settings();

$settings is then an array of the form array(instance number => settings). Typically your widget will have any ID like my-widget-class-3 - and the 'instance number' here is 3, and so

 $settings[3]

gives the settings (as an array) for the widget my-widget-class-3. This I feel is a more reliable and future proof method.

4

Not a noob question at all.

You'll need to dip into MySQL (using PhpMyAdmin or similar) and get the name of the option first. The following SQL will list the widget options:

SELECT *
FROM `wp_options`
WHERE `option_name`
REGEXP '^widget_'
LIMIT 0 , 30

Then in your template (or post/page with some sort of php exec plugin) you can get_option() like this:

<?php echo get_option('your_widget_option_name'); ?>

Et voila!

2
  • Thx but nothing returned from the select query. Commented Sep 23, 2010 at 9:27
  • That's odd. What's the name of the plugin that provides the widget?
    – Tom Wright
    Commented Sep 23, 2010 at 11:41
4

All of the widgets and their options are stored within global $wp_registered_widgets. Just load this variable and print out the contents to find the widget options that you're looking for.

3

I used Stephen Harris suggested method.

$dummy = new My_Widget_Class();
$settings = $dummy->get_settings();
$settings = reset($settings);

reset() gives the first key value from array, if you don't know what is the ID of your widget.

Note: It does not help if there multiple copies of widget is active. Because it returns the settings of the first copy of widget only.

0

Perhaps I'm mis-reading your question, but it seems like what you're looking to do is save some options or info from your widget (username and password) somewhere in WordPress where you can then re-access them outside the widget code. If that's correct, you'll want to look at the add_option() function (as well as update_option() and delete_option() as necessary).

To set an option, just do this:

<?php add_option("my_widget_option_name", 'option_value'); ?>

You can then retrieve it anywhere within WP by calling get_option like this:

<?php $option_val = get_option("my_widget_option_name"); ?>
6
  • Can you explain a little more what it is you're trying to do? Is the idea to set the username and password for the service in a widget form and then to pass those along to the service on every page load to deliver some content to the website user (without displaying the username and password)?
    – brandwaffle
    Commented Sep 23, 2010 at 10:11
  • Ok thanks, will give it a try. I am muddling along at the moment, partly due to time pressure. Commented Sep 23, 2010 at 12:47
  • To explain further, the widget posts 2 options, Service required and Postal Code. The form submits to a specific page (I know I could use the current page) which I have called search-results. Search results takes the options which I pass into the url calling the webservice. The results come back as XML which gets passed into an array and then output to the user. This site is using the same webservice: camden.nhs.uk/nhs-search-results.htm?type=1&q=E17 Commented Sep 23, 2010 at 12:51
  • ...the intention was to use parameters for the username/pwd for the webservice so that other sites could use the same widget. Commented Sep 23, 2010 at 13:03
  • Ok, so site owners will need to input their own username and password, correct? Once they do that, it uses that info to pull the XML from the other site and then display it. In that case, you do want to use get_option and add_option. The apply_filters function works on the display side of things--i.e. it does not change data in your database, it modifies it before it is displayed to the end user. It sounds like you want to take in the form input in the widget and save it with add_option. This site has a great tutorial: valums.com/create-wordpress-widget
    – brandwaffle
    Commented Sep 23, 2010 at 15:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.