omprocess
Forum Replies Created
Viewing 1 replies (of 1 total)
-
Forum: Hacks
In reply to: programmatically add widget to sidebarI 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' ); ?>
Viewing 1 replies (of 1 total)