Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
add comment

4 Answers 4

@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.

share|improve this answer
add comment

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!

share|improve this answer
    
Thx but nothing returned from the select query. –  JohnnyBizzle Sep 23 '10 at 9:27
    
That's odd. What's the name of the plugin that provides the widget? –  Tom Wright Sep 23 '10 at 11:41
add comment

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.

share|improve this answer
add comment

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"); ?>
share|improve this answer
    
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 Sep 23 '10 at 10:11
    
Ok thanks, will give it a try. I am muddling along at the moment, partly due to time pressure. –  JohnnyBizzle Sep 23 '10 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 –  JohnnyBizzle Sep 23 '10 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. –  JohnnyBizzle Sep 23 '10 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 Sep 23 '10 at 15:33
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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