List of checkboxes not saving in WordPress widget - php

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 ?

Related

Widget content isn't showing on pages in wordpress

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>

Custom Widget for Wordpress to Enable marking Categories as “New!”

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);

Wordpress create widget area in a widget

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 ?

How to add widget area to Wordpress theme? [Project AR2]

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' );

How to change array properties within a class from member function

In the code below, I'm trying to alter the $widget_ops "classname" array, based on the value of the $line_above variable in the "widget" function. I'm not sure how to address the array to change the value.
Is this possible?
class my_widget extends WP_Widget {
function my_widget() {
$widget_ops = array('classname' => 'test', 'description' => __( "test") );
$control_ops = array('width' => 450, 'height' => 100, 'id_base' => 'my_widget' );
$this->WP_Widget('my_widget', __('Test: test'), $widget_ops, $control_ops);
}
function widget( $args, $instance ) {
extract($args);
$title = apply_filters( 'widget_title', empty($instance['title']) ? '' : $instance['title'], $instance );
$text = apply_filters( 'widget_text', $instance['text'], $instance );
$line_above = isset( $instance['line_above'] ) ? $instance['line_above'] : false;
if($line_above){
//TRYING TO ALTER THE CLASS HERE BASED ON VARIABLE
$widget_ops['classname'] .= " border-top";
}
}
}
You could make it global:
class my_widget extends WP_Widget {
private $widget_ops;
function my_widget() {
$this->widget_ops = array('classname' => 'test', 'description' => __( "test") );
$control_ops = array('width' => 450, 'height' => 100, 'id_base' => 'my_widget' );
$this->WP_Widget('my_widget', __('Test: test'), $this->widget_ops, $control_ops);
}
function widget( $args, $instance ) {
extract($args);
$title = apply_filters( 'widget_title', empty($instance['title']) ? '' : $instance['title'], $instance );
$text = apply_filters( 'widget_text', $instance['text'], $instance );
$line_above = isset( $instance['line_above'] ) ? $instance['line_above'] : false;
if($line_above){
//TRYING TO ALTER THE CLASS HERE BASED ON VARIABLE
$this->widget_ops['classname'] .= " border-top";
}
}
}

Categories