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 have searched around for an answer to this question and I found this: Manual display of widget which gave me a solution of:

if(function_exists('the_widget')) {
   $instance = get_option( 'widget_backspikewidget' ); 
   the_widget('BackSpikeWidget', $instance[2]);
}

Which works. But as the @greatwitenorth points out in his answer, it includes the issue that if the array key changes this solution breaks.

So does anyone know of a more secure way to pull the settings for the widget set in the admin panel when using the_widget?

EDIT: So I am not sure I was being clear enough in my question. Here is what I am doing. I am creating a plugin that includes a widget. The plugin is going to be used on multiple websites. Each user has a bunch of options they can set for the widget in their admin dashboard. I then want to make sure that the widget, with the users selected options, is always present on a page that is also created by the plugin. So I have hardcoded the widget in the page using the_widget() and I have grabbed the options set by the user to set up the instance of the widget to match the same configuration the widget will have if they install the widget elsewhere in the site. Is there anyway the user could break this without deactivating or uninstalling the plugin? Thanks again!

share|improve this question
add comment

1 Answer

If you are going to display a particular instance of a widget you are going to have to know which instance that is. Hard-coding that instance value is not flexible, certainly, but unless you have some other way to identify the widget I don't see an option.

If you can work out some other way to identify the widget you could crawl the array. For example, you could crawl that array and check the widget title for a particular string (using the default text widget):

$instance = get_option( 'widget_text' ); 
foreach ($instance as $k => $v) {
  if ('hi there' == $v['title']) {
    the_widget('WP_Widget_Text',$instance[$k]);
  }
}
share|improve this answer
1  
Thank you for the fast reply. I don't think I was being 100% clear in my question so I edited it to hopefully make it clearer. Thanks again! –  MatthewLee Apr 25 '13 at 22:30
add 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.