WordPress.org

Ready to get started?Download WordPress

Forums

Preset Widgets At Theme Activate (1 post)

  1. miamitom
    Member
    Posted 9 months ago #

    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() {
    unregister_widget('WP_Widget_Pages');
    unregister_widget('WP_Widget_Calendar');
    unregister_widget('WP_Widget_Archives');
    unregister_widget('WP_Widget_Links');
    unregister_widget('WP_Widget_Meta');
    unregister_widget('WP_Widget_Search');
    unregister_widget('WP_Widget_Text');
    unregister_widget('WP_Widget_Categories');
    unregister_widget('WP_Widget_Recent_Posts');
    unregister_widget('WP_Widget_Recent_Comments');
    unregister_widget('WP_Widget_RSS');
    unregister_widget('WP_Widget_Tag_Cloud');
    }
    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 (
    'WP_Widget_Pages',
    'WP_Widget_Categories',
    'WP_Widget_Recent_Posts',
    ),
    'sidebar-right' => array(
    'WP_Widget_Search',
    'WP_Widget_Tag_Cloud',
    ),
    'widget-bottom-left' => array(
    'WP_Widget_Meta',
    ),
    'widget-bottom-middle' => array(
    'WP_Widget_Archives',
    ),
    'widget-bottom-right' => array(
    'WP_Widget_Calendar',
    ),
    );
    // 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

Reply

You must log in to post.

About this Topic