lundi 2 mai 2016

How to store checkbox values into database with wordpress widget update function?

I want to write an plugin for wordpress thats able to display ad's in the wordpress loop. I'm not at this point yet. I want to save the values which can be set in the widget, into a database. this works for all single values but doesn't with the checkboxes. for every post there is a checkbox and they should be used to exclude articles from the advertisments. it should be clear that i'm a newbie in php and wordpress so i need help from people who know what they do ;-)

Here's my code und thanks a lot. (sorry for german names)

class BAP_Widget extends WP_Widget
{
 
    public function __construct()
    {
        $widget_details = array(
            'classname' => 'bap_widget',
            'description' => 'Platziert Anzeigen in Beiträgen.'
        );
 
        parent::__construct( 'bap_widget', 'Anzeigenschalter 1.0', $widget_details );
                
                add_action( 'admin_enqueue_scripts', array( $this, 'mfc_assets' ) );
 
    }
        
        public function mfc_assets()
        {
                wp_enqueue_script('media-upload');
                wp_enqueue_script('thickbox');
                wp_enqueue_script('mfc-media-upload', plugin_dir_url(__FILE__) . 'mfc-media-upload.js', array( 'jquery' )) ;
                wp_enqueue_style('thickbox');
                
                wp_enqueue_script('jquery-ui-datepicker');
                wp_enqueue_style('jquery-ui-css', 'http://ift.tt/1j911hT');
        }
 
    public function form( $instance ) {
                
                $category = isset( $instance['category'] ) ? (int) $instance['category'] : '';
                $article_exclusion = isset($instance['article_exclusion']) ? $instance['article_exclusion'] : array() ;
                $image = '';
                if(isset($instance['image']))
                {
                        $image = esc_attr($instance['image']);
                }
                $ad_type = ( isset( $instance['ad_type']  ));
                $begin_duration = esc_attr( $instance['begin_duration'] );
                $end_duration = esc_attr( $instance['end_duration'] );
                $company_link = esc_attr( $instance['company_link'] );
                
                print_r($this->get_settings());
                ?>
                        
                <p>
                        <label for="<?php echo $this->get_field_id( 'category' ); ?>"><?php _e( 'Kategorie wählen:', 'textdomain' ); ?></label>
                        <?php wp_dropdown_categories( array( 'show_option_none' =>' ','name' => $this->get_field_name( 'category' ), 'selected' => $category ) ); ?>
                </p>

                <p>   
                        <label for="<?php echo $this->get_field_id('article_exclusion'); ?>"><?php _e('Ausschlussartikel:'); ?></label> 

                        <div style="height:200px;overflow:auto;border:1px solid #dddddd;margin-top:-10px;">
                        <?php 
                        global $post;
                        global $temp;
                        $temp[] = 0;
                        $args = array( 'numberposts' => -1);
                        $posts = get_posts($args); 
                        $counter = 0;
                        foreach( $posts as $post ) : setup_postdata($post);
                        $counter++;
                        ?>
                        <input style="margin-top:5px;" type="checkbox" name="<?php echo $this->get_field_name( 'article_exclusion' ); ?>[]" value="<?php the_ID(); ?>" id="<?php the_ID(); ?>">
                        <label><?php the_title(); ?></label>
                        </input><br> 
                        <?php 
                        $test = $_POST[$this->get_field_name( 'article_exclusion' )];
                        if ($test == 'on') { print_r('Hello World'); }
                        endforeach; ?>
                        </div>
                </p>      
                        
                <p>
                        <label for="<?php echo $this->get_field_id('begin_duration'); ?>"><?php _e('Startdatum:'); ?></label> 
                        <input type="text" class="custom_date" name="<?php echo $this->get_field_name('begin_duration'); ?>" value="<?php echo $begin_duration; ?>" id="<?php echo $this->get_field_id('begin_duration'); ?>"/>
                </p>
                
                <p>
                        <label for="<?php echo $this->get_field_id('end_duration'); ?>"><?php _e('Enddatum:'); ?></label> 
                        <input type="text" class="custom_date" name="<?php echo $this->get_field_name('end_duration'); ?>" value="<?php echo $end_duration; ?>" id="<?php echo $this->get_field_id('end_duration'); ?>"/>
                </p>
                
                <p>
                        <label for="<?php echo $this->get_field_id('ad_type'); ?>"><?php _e('Anzeigentyp:'); ?></label> 
                        <p><input type="radio" id="<?php echo $this->get_field_id( 'ad_type' ); ?>" name="<?php echo $this->get_field_name( 'ad_type' ); ?>" value="im_text "> Im Text</p>
                        <p><input type="radio" id="<?php echo $this->get_field_id( 'ad_type' ); ?>" name="<?php echo $this->get_field_name( 'ad_type' ); ?>" value="banner"> Banner</p>
                        <p><input type="radio" id="<?php echo $this->get_field_id( 'ad_type' ); ?>" name="<?php echo $this->get_field_name( 'ad_type' ); ?>" value="sidebar_top"> Sidebar top</p>
                </p>
                
        <p>
                        <label for="<?php echo $this->get_field_name( 'image' ); ?>"><?php _e( 'Bildauswahl:' ); ?></label>
                        <input name="<?php echo $this->get_field_name( 'image' ); ?>" id="<?php echo $this->get_field_id( 'image' ); ?>" class="widefat" type="text" size="36"  value="<?php echo esc_url( $image ); ?>" />
                        <input class="upload_image_button" type="button" value="Bild auswählen" />
        </p>
                
                <p>
                        <label for="<?php echo $this->get_field_id('company_link'); ?>"><?php _e('Link eingeben:'); ?></label> 
                        <input class="widefat" id="<?php echo $this->get_field_id('company_link'); ?>" name="<?php echo $this->get_field_name('company_link'); ?>" type="text" value="<?php echo $company_link; ?>" />
                </p>

                
                <?php 
                print_r($temp);
                print_r($article_exclusion);
    }
 
    public function update( $new_instance, $old_instance ) {
        print_r($old_instance);
    $instance = $old_instance;
        
    $instance['category'] = strip_tags( $new_instance['category'] );
    $instance['article_exclusion'] = strip_tags( $new_instance['article_exclusion'] );
        $instance['begin_duration'] = strip_tags( $new_instance['begin_duration'] );
        $instance['end_duration'] = strip_tags( $new_instance['end_duration'] );
        $instance['ad_type'] = strip_tags( $new_instance['ad_type'] );
        $instance['image'] = strip_tags( $new_instance['image'] );
        $instance['company_link'] = strip_tags( $new_instance['company_link'] );
        
        global $wpdb;
    $table_name = $wpdb->prefix . "anzeigenschalter";
        $wpdb->insert($table_name, array('Kategorie_ID' => $instance['category'],
                                                                        'Ausschlussartikel_ID' => $instance['article_exclusion'],
                                                                        'Zeitraum_Start' => $instance['begin_duration'],
                                                                        'Zeitraum_Ende' => $instance['end_duration'],
                                                                        'Anzeigentyp' => $instance['ad_type'],
                                                                        'Datei' => $instance['image'],
                                                                        'Link' => $instance['company_link']
                                                                        ));
     
        
    return $instance;
    }
 
    public function widget( $args, $instance ) {
        // Frontend display HTML
    }
 
}



Aucun commentaire:

Enregistrer un commentaire