Title: Programming widget in sidebar by default
Last modified: August 20, 2016

---

# Programming widget in sidebar by default

 *  [hhanna](https://wordpress.org/support/users/hhanna/)
 * (@hhanna)
 * [14 years, 8 months ago](https://wordpress.org/support/topic/programming-widget-in-sidebar-by-default/)
 * I made a plugin that obtains enrollment for a course. Each student in a course
   has a blog. The plugin creates a blogroll with links to each of the other students’
   blogs.
 * I have everything working with the exception of adding it to the sidebar, by 
   default. The user has to be able to change theme’s, so I don’t want to hardcode
   the plugin in the theme. I prefer to add it via the plugin.
 * I could go into all the ways I’ve tried to get this to work, but it basically
   comes down to this: I need to obtain (or calculate) the widget’s ID number prior
   to adding it to the options table.
 * For instance, before doing this:
 *     ```
       // the widget configuration data
       $new_options = array(
         'name'=>0,
         'description'=>0,
         'category'=>$term->term_id
       );
   
       // the current options in the db table
       $classroll_options = get_option('widget_classroll');
   
       // add widget config data to options array
       $classroll_options[] = $new_options; // IS THIS RIGHT??? 
   
       // add the widget config data to the db table
       update_option($widget_classroll, $classroll_options);
       ```
   
 * I need to know the classroll widget’s id. Otherwise, as you can see in the above
   code, I just add it to the end of the $classroll_options array. This doesn’t 
   work properly–yes, it adds to the array, but after testing, it looks like I should
   be defining the array key to define the element. In other words, WordPress does
   not use the options array as a numerically indexed array. Instead, it seems to
   use it as a named index array, those names just happen to be digits. Meaning 
   an array with a single element can have an index of “6” like so: array([6] =>
   _options\_data_).
 * So, how do i get the index to which to assign the options data?
    TIA

Viewing 1 replies (of 1 total)

 *  Thread Starter [hhanna](https://wordpress.org/support/users/hhanna/)
 * (@hhanna)
 * [14 years, 8 months ago](https://wordpress.org/support/topic/programming-widget-in-sidebar-by-default/#post-2344570)
 * Correction, that last line of code is meant to read:
    `update_option('widget_classroll',
   $classroll_options);`

Viewing 1 replies (of 1 total)

The topic ‘Programming widget in sidebar by default’ is closed to new replies.

## Tags

 * [add](https://wordpress.org/support/topic-tag/add/)
 * [default](https://wordpress.org/support/topic-tag/default/)
 * [programmatically](https://wordpress.org/support/topic-tag/programmatically/)
 * [sidebar](https://wordpress.org/support/topic-tag/sidebar/)
 * [update_option](https://wordpress.org/support/topic-tag/update_option/)
 * [widget](https://wordpress.org/support/topic-tag/widget/)
 * [widget id](https://wordpress.org/support/topic-tag/widget-id/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 1 participant
 * Last reply from: [hhanna](https://wordpress.org/support/users/hhanna/)
 * Last activity: [14 years, 8 months ago](https://wordpress.org/support/topic/programming-widget-in-sidebar-by-default/#post-2344570)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
