Skip to content

Instantly share code, notes, and snippets.

@rodica-andronache
Last active August 29, 2015 14:16
Show Gist options
  • Select an option

  • Save rodica-andronache/d9fe761f5ae3e4d12e38 to your computer and use it in GitHub Desktop.

Select an option

Save rodica-andronache/d9fe761f5ae3e4d12e38 to your computer and use it in GitHub Desktop.
Add default widgets in sidebar
http://wordpress.stackexchange.com/questions/26557/programmatically-add-widgets-to-sidebars
/**
* Super simple widget.
*/
class T5_Demo_Widget extends WP_Widget
{
public function __construct()
{ // id_base , visible name
parent::__construct( 't5_demo_widget', 'T5 Demo Widget' );
}
public function widget( $args, $instance )
{
echo $args['before_widget'], wpautop( $instance['text'] ), $args['after_widget'];
}
public function form( $instance )
{
$text = isset ( $instance['text'] )
? esc_textarea( $instance['text'] ) : '';
printf(
'<textarea class="widefat" rows="7" cols="20" id="%1$s" name="%2$s">%3$s</textarea>',
$this->get_field_id( 'text' ),
$this->get_field_name( 'text' ),
$text
);
}
}
add_action( 'widgets_init', 't5_default_widget_demo' );
function t5_default_widget_demo()
{
// Register our own widget.
register_widget( 'T5_Demo_Widget' );
// Register two sidebars.
$sidebars = array ( 'a' => 'top-widget', 'b' => 'bottom-widget' );
foreach ( $sidebars as $sidebar )
{
register_sidebar(
array (
'name' => $sidebar,
'id' => $sidebar,
'before_widget' => '',
'after_widget' => ''
)
);
}
// Okay, now the funny part.
// We don't want to undo user changes, so we look for changes first.
$active_widgets = get_option( 'sidebars_widgets' );
if ( ! empty ( $active_widgets[ $sidebars['a'] ] )
or ! empty ( $active_widgets[ $sidebars['b'] ] )
)
{ // Okay, no fun anymore. There is already some content.
return;
}
// The sidebars are empty, let's put something into them.
// How about a RSS widget and two instances of our demo widget?
// Note that widgets are numbered. We need a counter:
$counter = 1;
// Add a 'demo' widget to the top sidebar …
$active_widgets[ $sidebars['a'] ][0] = 't5_demo_widget-' . $counter;
// … and write some text into it:
$demo_widget_content[ $counter ] = array ( 'text' => "This works!\n\nAmazing!" );
#update_option( 'widget_t5_demo_widget', $demo_widget_content );
$counter++;
// That was easy. Now a RSS widget. More fields, more fun!
$active_widgets[ $sidebars['a'] ][] = 'rss-' . $counter;
// The latest 15 questions from WordPress Stack Exchange.
$rss_content[ $counter ] = array (
'title' => 'WordPress Stack Exchange',
'url' => 'http://wordpress.stackexchange.com/feeds',
'link' => 'http://wordpress.stackexchange.com/questions',
'items' => 15,
'show_summary' => 0,
'show_author' => 1,
'show_date' => 1,
);
update_option( 'widget_rss', $rss_content );
$counter++;
// Okay, now to our second sidebar. We make it short.
$active_widgets[ $sidebars['b'] ][] = 't5_demo_widget-' . $counter;
#$demo_widget_content = get_option( 'widget_t5_demo_widget', array() );
$demo_widget_content[ $counter ] = array ( 'text' => 'The second instance of our amazing demo widget.' );
update_option( 'widget_t5_demo_widget', $demo_widget_content );
// Now save the $active_widgets array.
update_option( 'sidebars_widgets', $active_widgets );
}
-------------------------------------------------------------------
EXEMPLU LAWYERIA:
-------------------------------------------------------------------
/************************************************/
/********* Practice areas widget ****************/
/************************************************/
class lawyeria_practiceareas extends WP_Widget {
function lawyeria_practiceareas() {
$this->WP_Widget('practiceareas-widget', 'Lawyeria Frontpage - Practice areas widget');
}
function widget($args, $instance) {
extract($args);
echo $before_widget;
echo '<h4>'.apply_filters('widget_title', $instance['title'] ).'</h4>';
if( !empty($instance['number']) ):
$get_taxonomy = get_terms( 'practiceareas', array('number' => esc_attr($instance['number']) ));
else:
$get_taxonomy = get_terms( 'practiceareas' );
endif;
if( !empty($get_taxonomy) ):
echo '<ul>';
foreach ( $get_taxonomy as $taxonomy_category ) {
$taxonomy_category = sanitize_term( $taxonomy_category, 'lawyers' );
$term_link = get_term_link( $taxonomy_category, 'lawyers' );
echo '<li>';
echo '<a href="'.esc_url( $term_link ).'" title="'.$taxonomy_category->name.'">';
echo $taxonomy_category->name;
echo '</a>';
echo '</li>';
}
echo '</ul>';
endif;
echo $after_widget;
}
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['number'] = strip_tags( $new_instance['number'] );
return $instance;
}
function form($instance) {
echo '<p>';
echo '<label for="'.$this->get_field_id('title').'">'.__('Title','lawyeria').'</label><br />';
if( !empty($instance['title']) ):
$pa_title = $instance['title'];
else:
$pa_title = '';
endif;
echo '<input type="text" name="'.$this->get_field_name('title').'" id="'.$this->get_field_id('title').'" value="'.$pa_title.'" class="widefat" />';
echo '</p>';
echo '<p>';
echo '<label for="'.$this->get_field_id('number').'">'.__('Number of practice area to show','lawyeria').'</label><br />';
if( !empty($instance['number']) ):
$pa_number = $instance['number'];
else:
$pa_number = '';
endif;
echo '<input type="text" name="'.$this->get_field_name('number').'" id="'.$this->get_field_id('number').'" value="'.$pa_number.'" class="widefat" />';
echo '</p>';
}
}
/*********************************************************/
/********* Our lawyers widget - frontpage ****************/
/*********************************************************/
class lawyeria_ourlawyers extends WP_Widget {
function lawyeria_ourlawyers() {
$this->WP_Widget('ourlawyers-widget', 'Lawyeria Frontpage - Our lawyers widget');
}
function widget($args, $instance) {
extract($args);
echo $before_widget;
echo '<h4>'.apply_filters('widget_title', $instance['title'] ).'</h4>';
if( !empty($instance['number']) ):
$args = array (
'post_type' => 'lawyers',
'posts_per_page' => esc_attr($instance['number']),
'ignore_sticky_posts' => true,
);
else:
$args = array (
'post_type' => 'lawyers',
'posts_per_page' => '5',
'ignore_sticky_posts' => true,
);
endif;
$lawyers = new WP_Query( $args );
if ( $lawyers->have_posts() ) :
while ( $lawyers->have_posts() ) :
$lawyers->the_post();
if ( has_post_thumbnail(get_the_ID()) ) {
echo '<a href="'.get_permalink().'" title="'.get_the_title().'" class="lawyer">'.get_the_post_thumbnail(get_the_ID(), 'our_lawyes_thumb').'</a>';
}
else {
echo '<a href="'.get_permalink().'" title="'.get_the_title().'" class="lawyer lawyer-no-image"></a>';
}
endwhile;
else:
echo '<p>'.__('Sorry, no posts matched your criteria.', 'lawyeria').'</p>';
endif;
wp_reset_postdata();
echo $after_widget;
}
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['number'] = strip_tags( $new_instance['number'] );
return $instance;
}
function form($instance) {
echo '<p>';
echo '<label for="'.$this->get_field_id('title').'">'.__('Title','lawyeria').'</label><br />';
if( !empty($instance['title']) ):
$l_title = $instance['title'];
else:
$l_title = '';
endif;
echo '<input type="text" name="'.$this->get_field_name('title').'" id="'.$this->get_field_id('title').'" value="'.$l_title.'" class="widefat" />';
echo '</p>';
echo '<p>';
echo '<label for="'.$this->get_field_id('number').'">'.__('Number of lawyers to show','lawyeria').'</label><br />';
if( !empty($instance['number']) ):
$l_number = $instance['number'];
else:
$l_number = '';
endif;
echo '<input type="text" name="'.$this->get_field_name('number').'" id="'.$this->get_field_id('number').'" value="'.$l_number.'" class="widefat" />';
echo '</p>';
}
}
/*********************************************************/
/********* Testimonials widget - frontpage ****************/
/*********************************************************/
class lawyeria_testimonials extends WP_Widget {
function lawyeria_testimonials() {
$this->WP_Widget('testimonials-widget', 'Lawyeria - Testimonials widget');
}
function widget($args, $instance) {
extract($args);
echo $before_widget;
if( !empty($instance['title']) ):
echo '<h4>'.apply_filters('widget_title', $instance['title'] ).'</h4>';
endif;
if( !empty($instance['number']) ):
if( !empty($instance['offset']) ):
$args = array (
'post_type' => 'testimonials',
'posts_per_page' => esc_attr($instance['number']),
'ignore_sticky_posts' => true,
'offset' => esc_attr($instance['offset'])
);
else:
$args = array (
'post_type' => 'testimonials',
'posts_per_page' => esc_attr($instance['number']),
'ignore_sticky_posts' => true
);
endif;
else:
$args = array (
'post_type' => 'testimonials',
'posts_per_page' => -1,
'ignore_sticky_posts' => true
);
endif;
$testimonials = new WP_Query( $args );
if ( $testimonials->have_posts() ) :
echo '<div class="list_carousel">';
echo '<ul id="foo2">';
while ( $testimonials->have_posts() ) :
$testimonials->the_post();
$testimonials_position = get_post_meta(get_the_ID(), 'ti_testimonials_position', true);
$testimonials_company_name = get_post_meta(get_the_ID(), 'ti_testimonials_company_name', true);
$testimonials_company_url = get_post_meta(get_the_ID(), 'ti_testimonials_company_url', true);
if ( ( $testimonials_position && $testimonials_company_name ) == NULL ) {
$at = '';
} else {
$at = ' at ';
}
if ( ( $testimonials_position && $testimonials_company_name ) == NULL ) {
$line = '';
} else {
$line = '-';
}
echo '<li>';
echo '<div class="list_carousel_entry">';
echo testimonials_excerpt(65);
echo '</div>';
echo '<div class="list_carousel_customer">';
echo '<span>'.get_the_title().$line.'</span>';
echo $testimonials_position.$at;
if ( $testimonials_company_url != false ) {
echo '<a href="'. $testimonials_company_url .'" title="'. $testimonials_company_name .'">'. $testimonials_company_name .'</a>';
} else {
echo $testimonials_company_name;
}
echo '</div>';
echo '</li>';
endwhile;
echo '</ul>';
echo '<div class="clearfix"></div>';
echo '<a id="prev2" class="prev" href="#"></a>';
echo '<a id="next2" class="next" href="#"></a>';
echo '</div>';
else:
echo '<p>'.__('Sorry, no posts matched your criteria.', 'lawyeria').'</p>';
endif;
wp_reset_postdata();
echo $after_widget;
}
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['number'] = strip_tags( $new_instance['number'] );
$instance['offset'] = strip_tags( $new_instance['offset'] );
return $instance;
}
function form($instance) {
echo '<p>';
echo '<label for="'.$this->get_field_id('title').'">'.__('Title','lawyeria').'</label><br />';
if( !empty($instance['title']) ):
$testim_title = $instance['title'];
else:
$testim_title = '';
endif;
echo '<input type="text" name="'.$this->get_field_name('title').'" id="'.$this->get_field_id('title').'" value="'.$testim_title.'" class="widefat" />';
echo '</p>';
echo '<p>';
echo '<label for="'.$this->get_field_id('number').'">'.__('Number of testimonials to show','lawyeria').'</label><br />';
if( !empty($instance['number']) ):
$testim_number = $instance['number'];
else:
$testim_number = '';
endif;
echo '<input type="text" name="'.$this->get_field_name('number').'" id="'.$this->get_field_id('number').'" value="'.$testim_number.'" class="widefat" />';
echo '</p>';
echo '<p>';
echo '<label for="'.$this->get_field_id('offset').'">'.__('Number of testimonials to skip','lawyeria').'</label><br />';
if( !empty($instance['offset']) ):
$testim_offset = $instance['offset'];
else:
$testim_offset = '';
endif;
echo '<input type="text" name="'.$this->get_field_name('offset').'" id="'.$this->get_field_id('offset').'" value="'.$testim_offset.'" class="widefat" />';
echo '</p>';
}
}
/* Default widgets on frontpage sidebar */
add_action( 'widgets_init', 'lawyeria_default_widgets_frontpage' );
function lawyeria_default_widgets_frontpage()
{
register_widget( 'lawyeria_practiceareas' );
register_widget( 'lawyeria_ourlawyers' );
register_widget( 'lawyeria_testimonials' );
$active_widgets = get_option( 'sidebars_widgets' );
$sidebars = array ( 'fp-sidebar' => 'Frontpage widgets area' );
foreach ( $sidebars as $sidebar ):
register_sidebar(
array (
'name' => $sidebar,
'id' => $sidebar,
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>'
)
);
endforeach;
if ( ! empty ( $active_widgets[ $sidebars['fp-sidebar'] ] )):
/* There is already some content. */
return;
endif;
$counter = 1;
/* Default practice areas widget */
$active_widgets[ $sidebars['fp-sidebar'] ][0] = 'practiceareas-widget-' . $counter;
$ti_frontpage_practiceareas_title = get_theme_mod('ti_frontpage_practiceareas_title');
if( !empty($ti_frontpage_practiceareas_title) ):
$pa_widget_content[ $counter ] = array ( 'title' => $ti_frontpage_practiceareas_title );
else:
$pa_widget_content[ $counter ] = array ( 'title' => __( 'Practice Areas', 'lawyeria' ) );
endif;
update_option( 'widget_practiceareas-widget', $pa_widget_content );
$counter++;
/* Default Our lawyers widget */
$active_widgets[ $sidebars['fp-sidebar'] ][] = 'ourlawyers-widget-' . $counter;
$ti_frontpage_ourlawyers_title = get_theme_mod('ti_frontpage_ourlawyers_title');
if( !empty($ti_frontpage_ourlawyers_title) ):
$ourlawyers_content[ $counter ] = array ( 'title' => $ti_frontpage_ourlawyers_title );
else:
$ourlawyers_content[ $counter ] = array ( 'title' => __('Our Lawyers','lawyeria') );
endif;
update_option( 'widget_ourlawyers-widget', $ourlawyers_content );
$counter++;
/* Default testimonials widget */
$active_widgets[ $sidebars['fp-sidebar'] ][] = 'testimonials-widget-' . $counter;
$testiomianls_param = array();
if(!empty($ti_frontpage_testimonials_offset)):
$testiomianls_param['offset'] = $ti_frontpage_testimonials_offset;
endif;
if(!empty($ti_frontpage_testimonials_numberofposts)):
$testiomianls_param['number'] = $ti_frontpage_testimonials_numberofposts;
endif;
if(!empty($ti_frontpage_testimonials_title)):
$testiomianls_param['title'] = $ti_frontpage_testimonials_title;
else:
$testiomianls_param['title'] = __('What customers say','lawyeria');
endif;
$testimon_content[ $counter ] = $testiomianls_param;
update_option( 'widget_testimonials-widget', $testimon_content );
$counter++;
update_option( 'sidebars_widgets', $active_widgets );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment