This is my code for widget in plugin:
class shortcodes_widget extends WP_Widget {
function shortcodes_widget() {
parent::WP_Widget(false, $name = __('t', 't') );
}
function form($instance) {
if($instance) {
$title = esc_attr($instance['title']);
$textarea = esc_textarea($instance['textarea']);
$checkbox = esc_attr($instance['checkbox']);
} else {
$title = '';
$textarea = '';
$checkbox = '';
}
?>
<p>
<label for="<?php echo $this->get_field_id('title');?>"><?php _e('t', 't'); ?></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 $title; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('textarea'); ?>"><?php _e('t', 't'); ?></label>
<textarea class="widefat" id="<?php echo $this->get_field_id('textarea'); ?>" name="<?php echo $this->get_field_name('textarea'); ?>"><?php echo $textarea; ?></textarea>
</p>
<p>
<input id="<?php echo $this->get_field_id('checkbox'); ?>" name="<?php echo $this->get_field_name('checkbox'); ?>" type="checkbox" value="1" />
<label for="<?php echo $this->get_field_id('checkbox'); ?>"><?php _e('t', 't'); ?></label>
</p>
<?php
}
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['textarea'] = strip_tags($new_instance['textarea']);
$instance['checkbox'] = strip_tags($new_instance['checkbox']);
return $instance;
}
public function widget( $args, $instance ) {
extract( $args );
while(isset($textarea)) {
$textarea = $instance['textarea'];
$checkbox = $instance['checkbox'];
}
if(isset($checkbox) and $checkbox == '1') {
echo '<div class="myclass">';
}
else {
echo '<div>';
}
echo $before_widget;
if (isset($instance['textarea'])) {
echo do_shortcode($instance['textarea']);
}
echo $after_widget;
echo '</div>';
}
}
add_action('widgets_init', create_function('', 'return register_widget("shortcodes_widget");'));
Problem is this:
if(isset($checkbox) and $checkbox == '1') {
echo '<div class="myclass">';
}
else {
echo '<div>';
}
It won't echo first value (div and "myclass") if checkbox is checked. Only echoing second value from "else". I had to add isset and while functions to avoid php notices. I tried with one = and without quotes but nothing helps. Any help? Thanks.
Aucun commentaire:
Enregistrer un commentaire