I have created a plugin with a custom widget where users can manage the post list by views,recent posts, tags and categories.
The plugin has checkboxes where users can select multiple categories.
It is all working in wordpress appearance/widgets
admin backend. But when the widget is used in page builder, selected categories are not saved.
below is my widget form:
<p class = "categories-post hidden-widget-element" style = "display:<?=($filter == "popular_cat"||$filter == "recent_cat")?"block":"none";?>;">
<label for="<?php echo $this->get_field_id( 'cat-post' ); ?>"><?php _e( 'Select Categories:' ); ?></label> <br />
<?php
foreach($categories as $category){
$checked = "";
if(is_array($instance["categories"])){
if(in_array($category->term_id,$instance["categories"])){
$checked = "checked";
}
}
?>
<input name="<?php echo $this->get_field_name( 'categories[]' ); ?>" type="checkbox" value = "<?=$category->term_id;?>" <?=$checked?>/><?php _e($category->name); ?><br />
<?php
}
?>
</p>
and this is my widget update function
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['filters'] = ( ! empty( $new_instance['filters'] ) ) ? strip_tags( $new_instance['filters'] ) : "unfiltered";
$filters = $instance['filters'];
if($filters == "popular_cat" OR $filters == "recent_cat"){
$instance['categories'] = ( ! empty( $new_instance['categories'] ) ) ? strip_tags( $new_instance['categories'] ) : '';
}
return $instance;
}
Aucun commentaire:
Enregistrer un commentaire