0

I want to fix this issues without plugins.

 add_action( 'widgets_init', 'goodlayers_1_4_banner_init' );
 function goodlayers_1_4_banner_init(){
 register_widget('Goodlayers_1_4_Banner_widget');      
}

class Goodlayers_1_4_Banner_widget extends WP_Widget{ 

// Initialize the widget
function Goodlayers_1_4_Banner_widget(){
    parent::__construct('goodlayers-1-4-banner-widget', __('Goodlayers 1/4 Banner Widget','gdl_back_office'));  
}

// Output of the widget
function widget($args, $instance) {  
    global $wpdb;

    extract( $args );

    $title = apply_filters( 'widget_title', $instance['title'] );
    $images = array(
        '1' => $instance['image'],
        '2' => $instance['image2'],
        '3' => $instance['image3'],
        '4' => $instance['image4'],
        'l1' => $instance['link'],
        'l2' => $instance['link2'],
        'l3' => $instance['link3'],
        'l4' => $instance['link4']
    );

    echo $before_widget;

    // Open of title tag
    if($title){ 
        echo $before_title . $title . $after_title; 
    }       

    echo '<div class="banner-widget1-2-wrapper">';

    for($i=1; $i<=4; $i++){
        if( !empty($images['l' . $i]) ){
            echo '<div class="banner-widget1-2" >';
            echo '<div class="banner-widget1-2-inner" >';
            echo empty($images['l' . $i])? '': '<a href="' . $images['l' . $i] . '" target="_blank">';
            echo '<img src="' . $images[$i] . '" alt="banner" />';
            echo empty($images['l' . $i])? '': '</a>';
            echo '</div>';
            echo '</div>';
        }
    }

    echo '<div class="clear"></div>';
    echo '</div>'; // 1-2 Banner Widget Wrapper

    echo $after_widget;     
}   

// Widget Form
function form($instance) {  
    $title = isset($instance['title'])? $instance['title']: '';
    $image = isset($instance['image'])? $instance['image']: '';
    $link = isset($instance['link'])? $instance['link']: '';
    $image2 = isset($instance['image2'])? $instance['image2']: '';
    $link2 = isset($instance['link2'])? $instance['link2']: '';
    $image3 = isset($instance['image3'])? $instance['image3']: '';
    $link3 = isset($instance['link3'])? $instance['link3']: '';
    $image4 = isset($instance['image4'])? $instance['image4']: '';
    $link4 = isset($instance['link4'])? $instance['link4']: '';

    ?>

    <!-- Title --> 
    <p>
        <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e( 'Title :', 'gdl_back_office' ); ?></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>

    <!-- Image --> 
    <p>
        <label for="<?php echo $this->get_field_id('image'); ?>"><?php _e( 'Banner Image URL :', 'gdl_back_office' ); ?></label> 
        <input class="widefat" id="<?php echo $this->get_field_id('image'); ?>" name="<?php echo $this->get_field_name('image'); ?>" type="text" value="<?php echo $image; ?>" />
    </p>        

    <!-- Link --> 
    <p>
        <label for="<?php echo $this->get_field_id('link'); ?>"><?php _e( 'Banner Link :', 'gdl_back_office' ); ?></label> 
        <input class="widefat" id="<?php echo $this->get_field_id('link'); ?>" name="<?php echo $this->get_field_name('link'); ?>" type="text" value="<?php echo $link; ?>" />
    </p>        

    <!-- Image2 --> 
    <p>
        <label for="<?php echo $this->get_field_id('image2'); ?>"><?php _e( 'Banner Image URL 2 :', 'gdl_back_office' ); ?></label> 
        <input class="widefat" id="<?php echo $this->get_field_id('image2'); ?>" name="<?php echo $this->get_field_name('image2'); ?>" type="text" value="<?php echo $image2; ?>" />
    </p>        

    <!-- Link2 --> 
    <p>
        <label for="<?php echo $this->get_field_id('link2'); ?>"><?php _e( 'Banner Link 2 :', 'gdl_back_office' ); ?></label> 
        <input class="widefat" id="<?php echo $this->get_field_id('link2'); ?>" name="<?php echo $this->get_field_name('link2'); ?>" type="text" value="<?php echo $link2; ?>" />
    </p>        

    <!-- Image3 --> 
    <p>
        <label for="<?php echo $this->get_field_id('image3'); ?>"><?php _e( 'Banner Image URL 3 :', 'gdl_back_office' ); ?></label> 
        <input class="widefat" id="<?php echo $this->get_field_id('image3'); ?>" name="<?php echo $this->get_field_name('image3'); ?>" type="text" value="<?php echo $image3; ?>" />
    </p>        

    <!-- Link3 --> 
    <p>
        <label for="<?php echo $this->get_field_id('link3'); ?>"><?php _e( 'Banner Link 3 :', 'gdl_back_office' ); ?></label> 
        <input class="widefat" id="<?php echo $this->get_field_id('link3'); ?>" name="<?php echo $this->get_field_name('link3'); ?>" type="text" value="<?php echo $link3; ?>" />
    </p>

    <!-- Image4 --> 
    <p>
        <label for="<?php echo $this->get_field_id('image4'); ?>"><?php _e( 'Banner Image URL 4 :', 'gdl_back_office' ); ?></label> 
        <input class="widefat" id="<?php echo $this->get_field_id('image4'); ?>" name="<?php echo $this->get_field_name('image4'); ?>" type="text" value="<?php echo $image4; ?>" />
    </p>        

    <!-- Link4 --> 
    <p>
        <label for="<?php echo $this->get_field_id('link4'); ?>"><?php _e( 'Banner Link 4 :', 'gdl_back_office' ); ?></label> 
        <input class="widefat" id="<?php echo $this->get_field_id('link4'); ?>" name="<?php echo $this->get_field_name('link4'); ?>" type="text" value="<?php echo $link4; ?>" />
    </p>

    <?php
}  

// Update the widget
function update($new_instance, $old_instance){  
    $instance = $old_instance;
    $instance['title'] = strip_tags($new_instance['title']);
    $instance['image'] = strip_tags($new_instance['image']);
    $instance['link'] = strip_tags($new_instance['link']);
    $instance['image2'] = strip_tags($new_instance['image2']);
    $instance['link2'] = strip_tags($new_instance['link2']);
    $instance['image3'] = strip_tags($new_instance['image3']);
    $instance['link3'] = strip_tags($new_instance['link3']);
    $instance['image4'] = strip_tags($new_instance['image4']);
    $instance['link4'] = strip_tags($new_instance['link4']);

    return $instance;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.