0

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!

1 Answer 1

2

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.

6
  • 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
    Commented Nov 22, 2010 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
    Commented Nov 22, 2010 at 0:28
  • 1
    Interesting post: shibashake.com/wordpress-theme/wordpress-widget-system
    – konzepz
    Commented Nov 22, 2010 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
    Commented Nov 22, 2010 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
    Commented Nov 22, 2010 at 10:24

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.