Support » Themes and Templates » Preset Widgets At Theme Activate

  • The theme I am building has 5 widget areas. ( 2 sidebars, and 3 widget areas at the bottom of the theme. ) I want to set up some widgets automatically in the theme at activation. I want simple code that can be called into or placed into functions.php

    1. I already have all the sidebars and widget areas defined and work great, no issues.

    2. Yes, I know WordPress has some default widgets that are installed at theme_activation. Yes, I know we can unregister the WordPress default widgets using code like this, and this code has issues of its own but just wanted to cover this at the outset before someone else adds it:

    // unregister all default WP Widgets
    function unregister_default_wp_widgets() {
    add_action('widgets_init', 'unregister_default_wp_widgets', 1);

    3. I have scoured the net and haven’t found anything recent that works with the latest versions of WordPress that will enable me to simply instantiate a default set of widgets at theme_activation.

    I found code like this that does not seem to work:

    function my_theme_activate() {
    $new_active_widgets = array (
    'sidebar-left' => array (
    'sidebar-right' => array(
    'widget-bottom-left' => array(
    'widget-bottom-middle' => array(
    'widget-bottom-right' => array(
    // save new widgets to DB
    update_option('sidebars_widgets', $new_active_widgets);

    3. I found more complicated code that does not work, which may have worked on older releases of WordPress.

    4. I do not want to install plugins to do this, I want to put code in the theme to make it happen at activation.

    5. Is there a reference in the codex / api or a tutorial out there that explains how to add widgets automatically at theme_activation?

    Thanks in advance

  • The topic ‘Preset Widgets At Theme Activate’ is closed to new replies.