Hello Guys,

Let’s see how we can implement wp_dropdown_categories() on widget field.

Here I am implementing codex way of making widgets: see here for reference: https://codex.wordpress.org/Widgets_API

You can see more about wp_dropdown_categories() here: https://codex.wordpress.org/Function_Reference/wp_dropdown_categories

So here we go->

/**
* Adds Foo_Widget widget.
*/
class Foo_Widget extends WP_Widget {

/**
* Register widget with WordPress.
*/
function __construct() {
parent::__construct(
'foo_widget', // Base ID
esc_html__( 'wp dropdown categories', 'text_domain' ), // Name
array( 'description' => esc_html__( 'A Foo Widget', 'text_domain' ), ) // Args
);
}

/**
* Front-end display of widget.
*
* @see WP_Widget::widget()
*
* @param array $args Widget arguments.
* @param array $instance Saved values from database.
*/
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.
*
* @see WP_Widget::form()
*
* @param array $instance Previously saved values from database.
*/
public function form( $instance ) {

$title = ! empty( $instance['title'] ) ? $instance['title'] : esc_html__( 'New title', 'text_domain' );
$post_slider_cat_id = isset( $instance['post-slider-cat'] ) ? $instance['post-slider-cat'] : '';
?>






<!--?php 
 wp_dropdown_categories( array(
 'show_option_all' => esc_html__( 'Select Category', 'text_domain' ),
 'show_count' => true,
 'selected' => absint( $post_slider_cat_id ),
 'name' => esc_attr( $this->get_field_name( 'post-slider-cat' ) ),
 'id' => esc_attr( $this->get_field_id('post-slider-category' ) ),
 'class' => 'widefat'
 ) );
?>

<!--?php 
 }

/**
* Sanitize widget form values as they are saved.
*
* @see WP_Widget::update()
*
* @param array $new_instance Values just sent to be saved.
* @param array $old_instance Previously saved values from database.
*
* @return array Updated safe values to be saved.
*/
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
//update selected category on database
$instance[ 'post-slider-cat' ] = ( ! empty( $new_instance['post-slider-cat'] ) ) ? absint( $new_instance['post-slider-cat'] ) : '';

return $instance;
}

} // class Foo_Widge

//register the class on widget_init
function register_foo_widget() {
register_widget( 'Foo_Widget' );
}
add_action( 'widgets_init', 'register_foo_widget' );

You have to clear on this following points:
1. In selected argument we have to use the id of selected category. We can get the selected category with this folloing code:
$post_slider_cat_id = isset( $instance[‘post-slider-cat’] ) ? $instance[‘post-slider-cat’] : ”;

2. name and id argument should contains name filed placed with get_field_name() get_field_id(). Be sure you use get_field_id() and get_field_name() otherwise your value won’t be updated on database. As widgets api needs this function should be used in form() methods to create name attributes for fields to be saved by update()

3. Now final point is to work on updating selected category on database. Here We are using absint function
to sanitize the cat value. As selected value will return the id.
$instance[ ‘post-slider-cat’ ] = ( ! empty( $new_instance[‘post-slider-cat’] ) ) ? absint( $new_instance[‘post-slider-cat’] ) : ”;

Note: You can populate all the category of any post type simply changing the taxonomy argument.

wp_dropdown_categories( array(
‘taxonomy’ => ‘your custom taxonomy slug’
)
)

Output:

Widgets Output
Widgets Output

Let me know if you have any question or queries or any other best solution.You can ping me on adhsushil7@gmail.com

Thanks

Leave a comment