Updating Widget options in custom install.php

I am using a custom install.php in /wp-content/ to over-ride some defaults when creating sites in a multi-site install. For the most part, this is easy and working really well. I am having difficulty with the widgets though. All I want for widgets is the Search widget and a custom Meta widget we use in-house.

Within my wp_install_defaults() function, I have the following …

<?php
function wp_install_defaults( $user_id ) {
    global $wpdb, $wp_rewrite, $table_prefix;
    ...
    lots of stuff setting up default settings and default content
    ...

    // Set up default widgets for default theme.
    update_option( 'widget_search', array ( 2 => array ( 'title' => '' ), '_multiwidget' => 1 ) );
    update_option( 'widget_vvnumetawidget', array ( 2 => array ( 'title' => '' ), '_multiwidget' => 1 ) );

    update_option( 'sidebars_widgets', array ( 'wp_active_widgets' => array (), 'sidebar-1' => array ( 0 => 'search-2', 1 => 'vvnumetawidget-2' ), 'sidebar-2' => array (), 'sidebar-3' => array (), 'array_version' => 3 ) );

    ...
    a few more options are set
    ...
}

This is mostly working, however… initially, only the Search Widget is displaying in the sidebar. If I go to the Widgets admin screen, the custom Meta widget is there where it should be and appears active. If I click on the Save button for it, it then displays properly inn the public sidebar.

I have tried running another update_option for my widget after having updated the sidebar_widgets option.

    update_option( 'widget_vvnumetawidget', array ( 2 => array ( 'title' => 'Second update' ), '_multiwidget' => 1 ) );

That does work in that the title gets updated, but it still doesn’t get it to display publicly, until I manually go to the admin screen and click on Save. I do not have to do that for the Search widget.

Any ideas what I am missing?

After looking in the database options table, I see a couple things that make me wonder if it isn’t because my custom widget is a plugin? For instance, after install I see the following options…

sidebars_widgets

a:3:{s:19:"wp_inactive_widgets";a:0:{}s:9:"sidebar-1";a:2:{i:0;s:8:"search-2";i:1;s:16:"vvnumetawidget-2";}s:13:"array_version";i:3;}

widget_search

a:2:{i:2;a:1:{s:5:"title";s:0:"";}s:12:"_multiwidget";i:1;}

widget_vvnumetawidget

a:2:{i:2;a:1:{s:5:"title";s:0:"";}s:12:"_multiwidget";i:1;}

At this point, to the public face, I have only the Search widget in my sidebar. If I go to the admin, the Search and my custom Meta widget are in the active area for my sidebar (even though my custom one is not displaying). If I click ‘Save’ for my custom Meta widget, it now displays publicly and if I check what is in the options table, it is now…

widget_vvnumetawidget

a:2:{i:2;a:9:{s:5:"title";s:0:"";s:8:"register";N;s:5:"login";s:1:"1";s:8:"entryrss";s:1:"1";s:10:"commentrss";s:1:"1";s:10:"nipissingu";s:1:"1";s:10:"showcustom";N;s:9:"customurl";s:0:"";s:10:"customtext";s:0:"";}s:12:"_multiwidget";i:1;}

So, it’s now looking to me like I need too set these defaults for my custom widget, in my install.php file also, OR in my custom widget’s php, test for the existence of these defaults, and set the appropriate behaviour there, as it is now treating the absence of defaults as 0 instead of null.

I’ll test and report back.

1 Answer
1

So, the actual problem was that I wasn’t setting any defaults my custom widget needed to display any content. The widget actually was there, but as I had emptied the title and set no other defaults, it was rendering nothing. My fix for this was two-fold.

1) set at least one default in my install.php file

update_option( 'widget_vvnumetawidget', array ( 2 => array ( 'title' => 'Meta Links', 'register' => 1, 'login' => 1  ), '_multiwidget' => 1 ) );

2) put some logic in my custom widget to display a message if it is installed, but no configuration is set.

Thanks @Nathan for getting me to look a little closer at those settings arrays.

Leave a Comment