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'm getting the feeling that adding Widgets via MySQL query is not as simple as I thought. I was under the impression all data is stored in the options table, but it seems like I was wrong.

Any idea how to place a widget via $wpdb? What data should be written down? And where?

Thanks!

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

Placing widgets via a MySQL or something MySQL abstracted via the global variable $wpdb that is an instance of the WPDB class is not trivial.

That's not because MySQL is something complicated or because $wpdb is useless at all but this has do something with the way the widget configuration is stored inside the mysql database options tables.

But there is always a work around. The nature of the widget configuration is stored inside the mysql database options tables. Just read the widgets configuration out of the option (get_option), edit it as you wish (use var_dump to inspect how that array is used) and then store it back into mysql via update_option.

That done you should be able to edit this "via mysql" (e.g. in the database) while making the settings of your wish.

This is quite a quick answer but it probably solves your issue.

share|improve this answer
    
Thanks. I actually already started doing just the debugging you suggested. Works pretty slow so far. Seems like the widget metadata is somewhat dynamic. Still testing for results, though. –  konzepz Nov 22 '10 at 0:10
    
@konzepz: It's an array that contains multiple widget entries. var_dump() is the best you can get so far I'm aware of. So true, it's slow, but just for a hint: The format of that data is not documented anywhere. Welcome to wordpress :). Install xdebug (!) to have this better displayed. –  hakre Nov 22 '10 at 0:28
1  
Interesting post: shibashake.com/wordpress-theme/wordpress-widget-system –  konzepz Nov 22 '10 at 1:16
    
@konzepz: yes, it pretty well describes the basic facts and is explaining the structure of the array. good catch, thanks for sharing the link. –  hakre Nov 22 '10 at 1:19
    
Actually, it looks pretty useful, since it's describing the use for wp_get_sidebars_widgets and wp_set_sidebars_widgets commands! –  konzepz Nov 22 '10 at 10:24
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.