Using widget options ‘outside’ the widget

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

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

Leave a Comment