WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] programmatically add widget to sidebar (5 posts)

  1. omprocess
    Member
    Posted 3 years ago #

    I would like to have some/all of my sidebars be populated with custom widgets when the theme I am developing is activated. Can anyone point me in the right direction? Does anyone know the function that is called when a widget is dragged to the sidebar and parameters get passed?

  2. Ryan Hellyer
    Member
    Posted 3 years ago #

    I think it's stored in an option (I haven't checked though). A quick look at the wp-admin/options.php page showed up the following option which might be where they're stored.

    Perhaps trying doing a var_dump() to see what's in it. If it contains your current widget contents, then that's the place you need to change :)

    <?php var_dump( get_option( 'sidebars_widgets' ) ); ?>

  3. omprocess
    Member
    Posted 3 years ago #

    I figured it out. Here is an example implementation which
    includes registering sidebars ( kudos to Thematic which
    contained the code that pointed me in the right direction).

    <?php
    if ( function_exists('register_sidebar') ) {
    register_sidebar(array(
    				'id' => 'right-sidebar',
    				'description' => __( 'The primary sidebar'),
    				'name' => 'Right Sidebar',
    				'before_widget' => '<div class="widget-here">',
    				'after_widget' => '</div>',
    				'before_title' => '<h3 class="widget-title">',
    				'after_title' => '</h3>',
    ));
    
    register_sidebar(array(
    				'id' => 'bottom-left-sidebar',
    				'description' => __( 'The lower left sidebar'),
    				'name' => 'Bottom Left Sidebar',
    				'before_widget' => '',
    				'after_widget' => '',
    				'before_title' => '<h3 class="widget-title">',
    				'after_title' => '</h3>',
    ));
    
    register_sidebar(array(
    				'id' => 'bottom-right-sidebar',
    				'description' => __( 'The lower right sidebar'),
    				'name' => 'Bottom Right Sidebar',
    				'before_widget' => '',
    				'after_widget' => '',
    				'before_title' => '<h3 class="widget-title">',
    				'after_title' => '</h3>',
    ));
    }
    ?>
    
    <?php
    class wdwp1_google_search extends WP_Widget {
    	function wdwp1_google_search() {
    		$widget_ops = array('classname' => 'wdwpSearch','description' => __('Configurable Google Search Box'));
    		$control_ops = array('width' => 300, 'height' => 350);
    		$this->WP_Widget('wdwp_g_search', __('WDWP Google Search'), $widget_ops, $control_ops);
    	}
    
    	function form($instance) {
    		$instance = wp_parse_args( (array) $instance, array( 'result_page' => 'search-results', 'g_pub_id' => 'partner-pub-partner-pub-xxxxxxxxxxxx:xxxxxxxxx' ));
    		$result_page = $instance['result_page'];
    		$g_pub_id = $instance['g_pub_id'];
    		?>
            <label for="<?php echo $this->get_field_id('result_page'); ?>">
            Name of search result page:
            <input id="<?php echo $this->get_field_id('result_page'); ?>" type="text" name="<?php echo $this->get_field_name('result_page'); ?>" value="<?php echo esc_attr($result_page); ?>" />
    		</label>
            <label for="<?php echo $this->get_field_id('g_pub_id'); ?>">
            Google Publisher Search ID:
            <input id="<?php echo $this->get_field_id('g_pub_id'); ?>" type="text" name="<?php echo $this->get_field_name('g_pub_id'); ?>" value="<?php echo esc_attr($g_pub_id); ?>" />
    		</label>
    
            <?php
    	}
    
    	//function update($new_instance, $old_instance) {
    		// processes widget options to be saved
    	//}
    
    	function widget($args, $instance) {
    		extract( $args, EXTR_SKIP );
    		$result_page = ( $instance['result_page'] ) ? $instance['result_page'] : 'search-results';
    		$g_pub_id = ( $instance['g_pub_id'] ) ? $instance['g_pub_id'] : 'partner-pub-xxxxxxxxxxxx:xxxxxxxxx';
    		?>
    <div id="searchbox">
      <div class="inside">
    
    <form action="<?php bloginfo('wpurl'); ?>/<?php echo esc_attr($result_page); ?>" id="cse-search-box">
    <div>
    <fieldset>
      <div>
        <input type="hidden" name="cx" value="<?php echo esc_attr($g_pub_id); ?>" />
        <input type="hidden" name="cof" value="FORID:11" />
        <input type="hidden" name="ie" value="ISO-8859-1" />
        <input type="text" class="searchfield" name="q" size="20" />
        <input type="submit" class="searchbutton" name="sa" value="Go" />
      </div>
    </fieldset>
      </div>
    </form>
    <script type="text/javascript" src="http://www.google.com/cse/brand?form=cse-search-box&lang=en"></script>
      </div>
    </div>
            <?php
    
    	}
    
    }
    
    function wdwp_widgets_init() {
    	register_widget('wdwp1_google_search');
    	$default_widgets = array (
    							 'right-sidebar' => array('wdwp_g_search-2')
    							 );
    
        if ( isset( $_GET['activated'] ) ) {
        	wdwp_defaultwidgets();
      		update_option( 'sidebars_widgets', apply_filters('wdwp_default_widgets',$default_widgets ));
      	}
    }
    
    add_action('widgets_init', 'wdwp_widgets_init');
    
    function wdwp_defaultwidgets() {
    	do_action( 'wdwp_defaultwidgets' );
    }
    
    function wdwp_init_defaultwidgets() {
    	update_option( 'wdwpSearch', array( 2 => array( 'result_page' => 'search-results', 'g_pub_id' => 'partner-pub-partner-pub-xxxxxxxxxxxx:xxxxxxxxx' ), '_multiwidget' => 1 ) );
    }
    
    add_action( 'wdwp_defaultwidgets', 'wdwp_init_defaultwidgets' );
    ?>
  4. etsuei
    Member
    Posted 3 years ago #

    If anyone is still reading this thread. I'm wondering how the code above is being implemented.

    There is an apply_filters with a 'wdwp_default_widgets' tag which doesn't appear to be anywhere in the code.

    Any help would be great. I am also trying to programmatically add widgets to a sidebar.

    cheers

  5. SocialBlogsite
    Member
    Posted 2 years ago #

    Understanding how widgets are activated programmatically would help me on a "activate on other sites" button I'm trying to add to all widgets.

    What I see here is there's a missing function only comes with Thematic and the widget is being added is not from the the list of activated ones, but defined here.

    Is there a way to add it programmatically but taking it from the list of available ones?

Topic Closed

This topic has been closed to new replies.

About this Topic