I'm using the Project AR2 theme and I'm trying to put a widget area directly underneath the slider. I've already added this to my functions.php file:
if ( function_exists('register_sidebar') ){
register_sidebar(array(
'name' => 'Below Slider Widgets',
'id' => 'below-slider-widgets',
'description' => 'The area below the Featured Posts slider. Great for small banner ads',
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h2>',
'after_title' => '</h2>'
));
}
But now I'm stuck on where to add this:
<div class="belowslider">
<?php if (!function_exists('dynamic_sidebar') || !dynamic_sidebar('Below Slider Widgets')) :endif; ?>
</div>
It seems the home page content is rendered dynamically, so it's not as simple as, say, opening a header.php file and pasting it in some static location.
My site isn't live yet, but here's a link to the theme demo:
http://demo.arrastheme.com/
And a link to all of the theme's files:
https://github.com/zyml/project-ar2
I think the key may lie in the home.php file, but I could be wrong.
Any help would be appreciated.
// Creating the widget
class wpb_widget extends WP_Widget {
function __construct() {
parent::__construct(
// Base ID of your widget
'wpb_widget',
// Widget name will appear in UI
__('WPBeginner Widget', 'wpb_widget_domain'),
// Widget description
array( 'description' => __( 'Sample widget based on WPBeginner Tutorial', 'wpb_widget_domain' ), )
);
}
// Creating widget front-end
// This is where the action happens
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', $instance['title'] );
// before and after widget arguments are defined by themes
echo $args['before_widget'];
if ( ! empty( $title ) )
echo $args['before_title'] . $title . $args['after_title'];
// This is where you run the code and display the output
echo __( 'Hello, World!', 'wpb_widget_domain' );
echo $args['after_widget'];
}
// Widget Backend
public function form( $instance ) {
if ( isset( $instance[ 'title' ] ) ) {
$title = $instance[ 'title' ];
}
else {
$title = __( 'New title', 'wpb_widget_domain' );
}
// Widget admin form
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
</p>
<?php
}
// Updating widget replacing old instances with new
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
return $instance;
}
} // Class wpb_widget ends here
// Register and load the widget
function wpb_load_widget() {
register_widget( 'wpb_widget' );
}
add_action( 'widgets_init', 'wpb_load_widget' );
Related
I try to get the names of the pages in a loop.
In the widget, the page titles are displayed correctly, but the values are not saved.
How I can save the checked inputs?
Below Full Widget Code:
class Foo_Widget extends WP_Widget {
/** Register widget with WordPress.*/
function __construct() {
parent::__construct(
'foo_widget', // Base ID
esc_html__( 'Widget Title', 'text_domain' ), // Name
array( 'description' => esc_html__( 'A Foo Widget', 'text_domain' ), ) // Args
);
}
/*** Front-end display of widget.*/
public function widget( $args, $instance ) {
echo $args['before_widget'];
if ( ! empty( $instance['title'] ) ) {
echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title'];
}
echo esc_html__( 'Hello, World!', 'text_domain' );
echo $args['after_widget'];
}
/*** Back-end widget form.*/
public function form( $instance ) {
$mypages = get_pages( array(
'sort_column' => 'post_date',
'sort_order' => 'desc'
) );
foreach( $mypages as $page ) {
$title = $page->post_title;
$slug = $page->post_name;
?>
<p>
<label>
<input type='checkbox' name='the_pages' id='<?php echo $slug ?>' value='<?php echo esc_attr( $title ); ?>'><?php echo esc_attr( $title ); ?>
</label>
</p>
<? } ?>
<?php }
/*** Sanitize widget form values as they are saved.*/
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = $new_instance[ 'title' ];
return $instance;
}
}
// register Foo_Widget widget
function register_foo_widget() {
register_widget( 'Foo_Widget' );
}
add_action( 'widgets_init', 'register_foo_widget' );
?>
How should update() function looks like ?
I have a standard widget which I've included in my footer with
<?php get_sidebar(); ?>
The content of the widget is visible only in categories pages. e.g. archive.php.
How can I show it on other pages too? It is included with the footer on all pages, like this:
<?php get_footer(); ?>
In the footer.php I have
<div class="footer-center" data-equalizer-watch>
<div class="baseline">
<div class="newsletter">
<?php get_sidebar(); ?>
</div>
</div>
</div>
UPDATE
<div class="newsletter">
<?php dynamic_sidebar( 'sidebar' ); ?>
</div>
Just changed get_sidebar(); to dynamic_sidebar( 'sidebar' );
Widget function in functions.php
//Stay Connected widget
add_filter( 'widget_text', 'do_shortcode' );
function stay_connected_load_widget() {
register_widget( 'stay_connected_widget' );
}
add_action( 'widgets_init', 'stay_connected_load_widget' );
class stay_connected_widget extends WP_Widget {
function __construct() {
parent::__construct(
'stay_connected_widget',
__('Stay Connected', 'stay_connected_widget_domain'),
array( 'description' => __( 'Social networks and subscribe form', 'stay_connected_widget_domain' ))
);
}
//Widget frontend
public function widget( $args, $instance ) {
$cf7_form = apply_filters( 'widget_title', $instance['cf7_form'] );
echo $args['before_widget'];
// echo $args['before_title'] . '<h4>' . __( 'Stay Connected', 'stay_connected_widget_domain' ) . '</h4>' .$args['after_title'];
?>
<?php
if(!empty( $cf7_form )){
echo '<h4 class="form_title">'. __('Subscribe By Email').'</h4>';
echo '<div class="stay_connected_form">'.do_shortcode(html_entity_decode($cf7_form)).'</div>';
}
echo $args['after_widget'];
}
// Widget Backend
public function form( $instance ) {
if ( isset( $instance[ 'cf7_form' ] ) ) {
$cf7_form = $instance[ 'cf7_form' ];
}
else {
$cf7_form = __( '', 'stay_connected_widget_domain' );
}
?>
<p>
<label for="<?php echo $this->get_field_id( 'cf7_form' ); ?>"><?php _e( 'Contact Form:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'cf7_form' ); ?>" name="<?php echo $this->get_field_name( 'cf7_form' ); ?>" type="text" value="<?php echo esc_attr( $cf7_form ); ?>" placeholder="[<?php _e('contact form shortcode'); ?>]"/>
</p>
<?php
}
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['cf7_form'] = ( ! empty( $new_instance['cf7_form'] ) ) ? strip_tags( $new_instance['cf7_form'] ) : '';
return $instance;
}
}
Update
Array (
[wp_inactive_widgets] => Array ( )
[header] => Array (
[0] => media_gallery-2
)
[footer] => Array (
[0] => custom_html-2
)
[sidebar] => Array (
[0] => stay_connected_widget-3
[1] => search-2
)
[array_version] => 3
)
Add this code in functions.php
function stay_widgets_init() {
register_sidebar( array(
'name' => 'Stay Connected ',
'id' => 'stay_connected_widget',
'before_widget' => '<div>',
'after_widget' => '</div>',
'before_title' => '',
'after_title' => '',
) );
}
add_action( 'widgets_init', 'stay_widgets_init' );
Use this in footer.php file
<div class="newsletter">
<?php dynamic_sidebar( 'stay_connected_widget' ); ?>
</div>
Trying to build a widget for my simple wordpress blog, which will display categories in the sidebar, but not exactly like the native wordpress category widget. Basically, what I am trying to achieve is to be able to mark certain categories as "New!" or something similar, but from within the widget itself.
So far I have the following code that registers my widget and can display categories list in it on the backend with checkbox next to the name.
When I check the box and trying to save it it returns unchecked again. Not sure if my update function is actually working as serialized array in the DB has not changed on save.
Here is what I have do far:
/* CUSTOM BLOG CATEGORIES WIDGETS */
class Spr123_Categories_Widget extends WP_Widget {
public function __construct() {
$widget_options = array(
'classname' => 'widget_custom_categories_widget',
'description' => 'This is a Custom Blog Categories Widget',
);
parent::__construct( 'custom_categories_widget', 'Custom Categories Widget', $widget_options );
}
public function form( $instance ) {
$title = ! empty( $instance['title'] ) ? $instance['title'] : '';
$name = ! empty( $instance['name'] ) ? $instance['name'] : '';
$checked = isset( $instance['checked'] ) ? (bool) $instance['checked'] : false;
$categories = get_categories( array(
'orderby' => 'name',
'parent' => 0,
"hide_empty" => 0,
) );
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>">Title:</label>
<input type="text" id="<?php echo $this->get_field_id( 'title' ); ?>" class="widefat" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo esc_attr( $title ); ?>" />
</p>
<p>
<label for="Categories">Categories:</label>
</p>
<?php print("<pre>".print_r($categories,true)."</pre>"); ?>
<p>
<?php
foreach ( $categories as $category ) {
?>
<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id($category->slug); ?>" name="<?php echo $this->get_field_name('checked'); ?>"<?php checked( $checked ); ?> />
<label for="<?php echo $this->get_field_id($category->slug); ?>"><?php _e( 'Display as NEW - ' . esc_attr( $category->name )); ?></label><br />
<?php
}
?>
</p>
<?php
}
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance[ 'title' ] = strip_tags( $new_instance[ 'title' ] );
$instance[ 'name' ] = strip_tags( $new_instance[ 'name' ] );
$instance[ 'checked' ] = !empty($new_instance['checked']) ? 1 : 0;
return $instance;
}
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', $instance[ 'title' ] );
$category_title = apply_filters( 'widget_title', $instance[ 'name' ] );
echo $args['before_widget'] . $args['before_title'] . $title . $args['after_title']; ?>
<p><?php echo $category_title ?></p>
<?php echo $args['after_widget'];
}
}
function spr123_custom_categories_widget() {
register_widget( 'Spr123_Categories_Widget' );
}
add_action( 'widgets_init', 'spr123_custom_categories_widget' );
Try this code
/* CUSTOM BLOG CATEGORIES WIDGETS */
class Spr123_Categories_Widget extends WP_Widget {
public function __construct() {
$widget_options = array(
'classname' => 'widget_custom_categories_widget',
'description' => 'This is a Custom Blog Categories Widget',
);
parent::__construct( 'custom_categories_widget', 'Custom Categories Widget', $widget_options );
}
public function form( $instance ) {
$title = ! empty( $instance['title'] ) ? $instance['title'] : '';
$name = ! empty( $instance['name'] ) ? $instance['name'] : '';
$checked = isset( $instance['checked'] ) ? (bool) $instance['checked'] : false;
$savedcategories = ! empty( $instance['categories'] ) ? $instance['categories'] : '';
$categories = get_categories( array(
'orderby' => 'name',
'parent' => 0,
"hide_empty" => 0,
) );
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>">Title:</label>
<input type="text" id="<?php echo $this->get_field_id( 'title' ); ?>" class="widefat" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo esc_attr( $title ); ?>" />
</p>
<p>
<label for="Categories">Categories:</label>
</p>
<!-- --><?php //print("<pre>".print_r($categories,true)."</pre>"); ?>
<p>
<?php
foreach ( $categories as $category ) {
$checked = in_array($category->term_id, $savedcategories)?'checked':'';
?>
<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id($category->slug); ?>" name="<?php echo $this->get_field_name('categories[]'); ?>" <?php echo $checked; ?> value="<?php echo $category->term_id; ?>"/>
<label for="<?php echo $this->get_field_id($category->slug); ?>"><?php echo esc_attr( $category->name ) . _e( 'Display as NEW - ' ); ?></label><br />
<?php
}
?>
</p>
<?php
}
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance[ 'title' ] = strip_tags( $new_instance[ 'title' ] );
$instance[ 'name' ] = strip_tags( $new_instance[ 'name' ] );
$instance[ 'categories' ] = $new_instance['categories'];
return $instance;
}
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', $instance[ 'title' ] );
$category_title = apply_filters( 'widget_title', $instance[ 'name' ] );
echo $args['before_widget'] . $args['before_title'] . $title . $args['after_title']; ?>
<p><?php echo $category_title ?></p>
<?php echo $args['after_widget'];
}
}
function spr123_custom_categories_widget() {
register_widget( 'Spr123_Categories_Widget' );
}
add_action( 'widgets_init', 'spr123_custom_categories_widget' );
you can the value in frontend by using this code
$widget_instances = get_option('widget_custom_categories_widget');
print_r($widget_instances);
I am trying to insert a custom widget area in another widget, but don't know where to start. Do I need to register new widget areas in widget class constructor with unique id's ?
What I am trying to achieve:
Insert a section widget which contains a widget area.
Insert custom widgets into that section with the widget area.
<?php
function dov_load_widget_default( ) {
register_widget( 'section' );
}
add_action( 'widgets_init', 'dov_load_widget_default' );
class section extends WP_Widget {
function __construct( ) {
parent::__construct(
'section',
__( 'Portfolio sections', 'dov_portfolio' ),
array( 'description' => __( 'Default section for widgets', 'dov_portfolio' ) )
);
}
// Creating widget front-end
public function widget( $args, $instance ) {
}
// Widget Backend
public function form( $instance ) {
}
// Updating widget replacing old instances with new
public function update( $new_instance, $old_instance ) {
$instance = array( );
return $instance;
}
}
?>
Don't know if it's the right way but it seems to work.
function dov_load_sidebars( ) {
register_sidebar( array(
'id' => 'portfolio_sections',
'name' => __( 'Portfolio sections', 'dov_portfolio' )
));
$sections = wp_get_sidebars_widgets( );
if( isset( $sections[ 'portfolio_sections' ] ) ) :
$sections = $sections[ 'portfolio_sections' ];
foreach( $sections as $section ) :
register_sidebar( array(
'id' => $section,
'name' => __( 'Portfolio section', 'dov_portfolio' ) . ': ' . $section
));
endforeach;
endif;
}
add_action( 'widgets_init', 'dov_load_sidebars' );
EDIT: After inserting the section widget the widget areas don't refresh. How can I refresh them or should I add them using js ?
So this is my first attempt a creating a widget that has a functionality of creating a simple image rollover effect (if anyone wants it when it's done, leave me a message).
Now I downloaded a sample widget and made some changes, I still haven't touched the html part for the output so ignore that bit. Now my problem is, when I click save, the values in the fields go back to their default values, and they did not do this on the sample widget. Here's my code:
<?php
/**
* Plugin Image: Rollover Widget
* Description: A widget that creates a rollover image effect with a hyperlink.
* Version: 0.1
* Author: Zak Elas
* Author URI:
*/
add_action( 'widgets_init', 'my_widget' );
function my_widget() {
register_widget( 'MY_Widget' );
}
class MY_Widget extends WP_Widget {
function MY_Widget() {
$widget_ops = array( 'classname' => 'example', 'description' => __('A widget that creates a rollover image effect with a hyperlink. ', 'example') );
$control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'Rollover Widget' );
$this->WP_Widget( 'example-widget', __('Example Widget', 'example'), $widget_ops, $control_ops );
}
function widget( $args, $instance ) {
extract( $args );
//Our variables from the widget settings.
$link = apply_filters('widget_link', $instance['link'] );
$image = $instance['image'];
$rollover_image = $instance['rollover_image'];
echo $before_widget;
// Display the widget link
if ( $link )
echo $before_link . $link . $after_link;
//Display the name
printf( '<p>' . __('Hey their Sailor! My name is %1$s.', 'example') . '</p>', $image );
printf( '<p>' . __('Hey their Sailor! My name is %1$s.', 'example') . '</p>', $rollover_image );
echo $after_widget;
}
//Update the widget
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
//Strip tags from link and name to remove HTML
$instance['link'] = strip_tags( $new_instance['link'] );
$instance['image'] = strip_tags( $new_instance['image'] );
$instance['rollover_image'] = strip_tags( $new_instance['rollover_image'] );
return $instance;
}
function form( $instance ) {
//Set up some default widget settings.
$defaults = array( 'link' => __('Example', 'example'), 'image' => __('/images/editorial.png', 'example') , 'rollover_image' => __('/images/editorial.png', 'example') );
$instance = wp_parse_args( (array) $instance, $defaults ); ?>
<p>
<label for="<?php echo $this->get_field_id( 'link' ); ?>"><?php _e('link', 'example'); ?></label>
<input id="<?php echo $this->get_field_id( 'link' ); ?>" name="<?php echo $this->get_field_name( 'link' ); ?>" value="<?php echo $instance['link']; ?>" style="width:100%;" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'image' ); ?>"><?php _e('image', 'example'); ?></label>
<input id="<?php echo $this->get_field_id( 'image' ); ?>" name="<?php echo $this->get_field_name( 'image' ); ?>" value="<?php echo $instance['image']; ?>" style="width:100%;" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'rollover_image' ); ?>"><?php _e('rollover_image:', 'example'); ?></label>
<input id="<?php echo $this->get_field_id( 'rollover_image' ); ?>" name="<?php echo $this->get_field_name( 'image' ); ?>" value="<?php echo $instance['rollover_image']; ?>" style="width:100%;" />
</p>
<?php
}
}
?>
And here is the sample widgets code
<?php
/**
* Plugin Name: A simple Widget
* Description: A widget that displays authors name.
* Version: 0.1
* Author: Bilal Shaheen
* Author URI: http://gearaffiti.com/about
*/
add_action( 'widgets_init', 'my_widget' );
function my_widget() {
register_widget( 'MY_Widget' );
}
class MY_Widget extends WP_Widget {
function MY_Widget() {
$widget_ops = array( 'classname' => 'example', 'description' => __('A widget that displays the authors name ', 'example') );
$control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'example-widget' );
$this->WP_Widget( 'example-widget', __('Example Widget', 'example'), $widget_ops, $control_ops );
}
function widget( $args, $instance ) {
extract( $args );
//Our variables from the widget settings.
$title = apply_filters('widget_title', $instance['title'] );
$name = $instance['name'];
$show_info = isset( $instance['show_info'] ) ? $instance['show_info'] : false;
echo $before_widget;
// Display the widget title
if ( $title )
echo $before_title . $title . $after_title;
//Display the name
if ( $name )
printf( '<p>' . __('Hey their Sailor! My name is %1$s.', 'example') . '</p>', $name );
if ( $show_info )
printf( $name );
echo $after_widget;
}
//Update the widget
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
//Strip tags from title and name to remove HTML
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['name'] = strip_tags( $new_instance['name'] );
$instance['show_info'] = $new_instance['show_info'];
return $instance;
}
function form( $instance ) {
//Set up some default widget settings.
$defaults = array( 'title' => __('Example', 'example'), 'name' => __('Bilal Shaheen', 'example'), 'show_info' => true );
$instance = wp_parse_args( (array) $instance, $defaults ); ?>
//Widget Title: Text Input.
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Title:', 'example'); ?></label>
<input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" style="width:100%;" />
</p>
//Text Input.
<p>
<label for="<?php echo $this->get_field_id( 'name' ); ?>"><?php _e('Your Name:', 'example'); ?></label>
<input id="<?php echo $this->get_field_id( 'name' ); ?>" name="<?php echo $this->get_field_name( 'name' ); ?>" value="<?php echo $instance['name']; ?>" style="width:100%;" />
</p>
//Checkbox.
<p>
<input class="checkbox" type="checkbox" <?php checked( $instance['show_info'], true ); ?> id="<?php echo $this->get_field_id( 'show_info' ); ?>" name="<?php echo $this->get_field_name( 'show_info' ); ?>" />
<label for="<?php echo $this->get_field_id( 'show_info' ); ?>"><?php _e('Display info publicly?', 'example'); ?></label>
</p>
<?php
}
}
?>
The id_base should match the first argument for WP_Widget, and it should be lower case, no spaces:
$control_ops = array(
'width' => 300,
'height' => 350,
'id_base' => 'rollover_image_id'
);
$this->WP_Widget(
'rollover_image_id',
__('Example Widget', 'example'),
$widget_ops,
$control_ops
);
And you have a get_field_name( 'image' )
where it should be get_field_name( 'rollover_image' ).
Change this line
$this->WP_Widget( 'example-widget', __('Example Widget', 'example'), $widget_ops, $control_ops );
to this
$this->WP_Widget( 'my_widget', __('Example Widget', 'example'), $widget_ops, $control_ops );
consructor id (which was example-widget and I have corected to my_widget) must be namespaced form (lowercase, nospace) of class name (which is MY_Widget).
This will work insaAllah.