Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm writing a widget to display 125 x 125 pixel ads. There are 6 spaces for the ads and currently all works.

However, I would to add some conditional logic so that if nothing is entered in the values in the widget admin for an ad, then no add is shown.

I think this can be done by using PHP if or something, I can't remember what it is exactly. I need it with the <?php & ?> tags on. It needs to check if $adone exists and if not display an empty space.

I know this code here is wrong but something like this:

<?php if_has($adone_img)  ?>
/** Ad 1 code here */
<?php else : ?>
<?php endif; ?>

The widget class:

<?php
/**
 * square_ad Class
 */
class square_ad extends WP_Widget {
    /** constructor */
    function square_ad() {
        $widget_ops = array('classname' => 'square_ad', 'description' => 'Up to 6 125px by 125px ads for your sidebar.');
        $control_ops = array('id_base' => 'square_ad');
        $this->WP_Widget('square_ad', 'SILVERLINE 125 Ad', $widget_ops, $control_ops);  
    }

    /** @see WP_Widget::widget */
    function widget( $args, $instance ) {
        extract( $args );
        $title = apply_filters( 'widget_title', $instance['title'] );

        $adone_img = apply_filters( 'adone', $instance['adone_img'] );
        $adone_link = apply_filters( 'adone', $instance['adone_link'] );

        $adtwo_img = apply_filters( 'adtwo', $instance['adtwo_img'] );
        $adtwo_link = apply_filters( 'adtwo', $instance['adtwo_link'] );    

        $adthree_img = apply_filters( 'adthree', $instance['adthree_img'] );
        $adthree_link = apply_filters( 'adthree', $instance['adthree_link'] );

        $adfour_img = apply_filters( 'adfour', $instance['adfour_img'] );
        $adfour_link = apply_filters( 'adfour', $instance['adfour_link'] );

        $adfive_img = apply_filters( 'adfive', $instance['adfive_img'] );
        $adfive_link = apply_filters( 'adfive', $instance['adfive_link'] );

        $adsix_img = apply_filters( 'adsix', $instance['adsix_img'] );
        $adsix_link = apply_filters( 'adsix', $instance['adsix_link'] );

        echo('<h3 style="margin-left:5px; margin-top:-30px;" class="widget-title-text">');
        echo($title);
        echo('</h3>');
        echo('<div id="widget-container-125">');
 ?>



<a href="<?php echo ($adone_link);?>"><img class="box-container" src="<?php echo ($adone_img);?>" width="125" height="125" /></a>
<a href="<?php echo ($adtwo_link);?>"><img align="right" class="box-container" src="<?php echo ($adtwo_img);?>"  width="125" height="125" /><br/></a>
<a href="<?php echo ($adthree_link);?>"><img class="box-container" src="<?php echo ($adthree_img);?>" width="125" height="125" /></a>
<a href="<?php echo ($adfour_link);?>"><img align="right" class="box-container" src="<?php echo ($adfour_img);?>"  width="125" height="125" /><br/></a>
<a href="<?php echo ($adfive_link);?>"><img class="box-container" src="<?php echo ($adfive_img);?>" width="125" height="125" /></a>
<a href="<?php echo ($adsix_link);?>"><img class="box-container" src="<?php echo ($adsix_img);?>" width="125" height="125" /></a>

<style type="text/css">
</style>
<br/><br/></div>

        <?php 
    }

    /** @see WP_Widget::update */
    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $instance['title'] = strip_tags($new_instance['title']);

        $instance['adone_img'] = strip_tags($new_instance['adone_img']);
        $instance['adone_link'] = strip_tags($new_instance['adone_link']);

        $instance['adtwo_img'] = strip_tags($new_instance['adtwo_img']);
        $instance['adtwo_link'] = strip_tags($new_instance['adtwo_link']);

        $instance['adthree_img'] = strip_tags($new_instance['adthree_img']);
        $instance['adthree_link'] = strip_tags($new_instance['adthree_link']);

        $instance['adfour_img'] = strip_tags($new_instance['adfour_img']);
        $instance['adfour_link'] = strip_tags($new_instance['adfour_link']);

        $instance['adfive_img'] = strip_tags($new_instance['adfive_img']);
        $instance['adfive_link'] = strip_tags($new_instance['adfive_link']);

        $instance['adsix_img'] = strip_tags($new_instance['adsix_img']);
        $instance['adsix_link'] = strip_tags($new_instance['adsix_link']);
        return $instance;
    }

    /** @see WP_Widget::form */
    function form( $instance ) {
        if ( $instance ) {
            $title = esc_attr( $instance[ 'title' ] );
            $adone_img = esc_attr( $instance[ 'adone_img' ] );
            $adone_link = esc_attr( $instance[ 'adone_link' ] );

            $adtwo_img = esc_attr( $instance[ 'adtwo_img' ] );
            $adtwo_link = esc_attr( $instance[ 'adtwo_link' ] );

            $adthree_img = esc_attr( $instance[ 'adthree_img' ] );
            $adthree_link = esc_attr( $instance[ 'adthree_link' ] );

            $adfour_img = esc_attr( $instance[ 'adfour_img' ] );
            $adfour_link = esc_attr( $instance[ 'adfour_link' ] );

            $adfive_img = esc_attr( $instance[ 'adfive_img' ] );
            $adfive_link = esc_attr( $instance[ 'adfive_link' ] );

            $adsix_img = esc_attr( $instance[ 'adsix_img' ] );
            $adsix_link = esc_attr( $instance[ 'adsix_link' ] );
        }
        else {
            $title = __( '', 'text_domain' );
        }
        ?>
        <p>
        <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></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; ?>" />


        <h4>Ad One</h4>
        <label for="<?php echo $this->get_field_id('adone_img'); ?>"><?php _e('Image URL:'); ?></label>
        <input class="widefat" id="<?php echo $this->get_field_id('adone_img'); ?>" name="<?php echo $this->get_field_name('adone_img'); ?>" type="text" value="<?php echo $adone_img; ?>" />
        <label for="<?php echo $this->get_field_id('adone_link'); ?>"><?php _e('AD Link:'); ?></label>
        <input class="widefat" id="<?php echo $this->get_field_id('adone_link'); ?>" name="<?php echo $this->get_field_name('adone_link'); ?>" type="text" value="<?php echo $adone_link; ?>" />

        <h4>Ad Two</h4>
        <label for="<?php echo $this->get_field_id('adtwo_img'); ?>"><?php _e('Image URL:'); ?></label>
        <input class="widefat" id="<?php echo $this->get_field_id('adtwo_img'); ?>" name="<?php echo $this->get_field_name('adtwo_img'); ?>" type="text" value="<?php echo $adtwo_img; ?>" />
        <label for="<?php echo $this->get_field_id('adtwo_link'); ?>"><?php _e('AD Link:'); ?></label>
        <input class="widefat" id="<?php echo $this->get_field_id('adtwo_link'); ?>" name="<?php echo $this->get_field_name('adtwo_link'); ?>" type="text" value="<?php echo $adtwo_link; ?>" />

        <h4>Ad Three</h4>
        <label for="<?php echo $this->get_field_id('adthree_img'); ?>"><?php _e('Image URL:'); ?></label>
        <input class="widefat" id="<?php echo $this->get_field_id('adthree_img'); ?>" name="<?php echo $this->get_field_name('adthree_img'); ?>" type="text" value="<?php echo $adthree_img; ?>" />
        <label for="<?php echo $this->get_field_id('adthree_link'); ?>"><?php _e('AD Link:'); ?></label>
        <input class="widefat" id="<?php echo $this->get_field_id('adthree_link'); ?>" name="<?php echo $this->get_field_name('adthree_link'); ?>" type="text" value="<?php echo $adthree_link; ?>" />

        <h4>Ad Four</h4>
        <label for="<?php echo $this->get_field_id('adfour_img'); ?>"><?php _e('Image URL:'); ?></label>
        <input class="widefat" id="<?php echo $this->get_field_id('adfour_img'); ?>" name="<?php echo $this->get_field_name('adfour_img'); ?>" type="text" value="<?php echo $adfour_img; ?>" />
        <label for="<?php echo $this->get_field_id('adfour_link'); ?>"><?php _e('AD Link:'); ?></label>
        <input class="widefat" id="<?php echo $this->get_field_id('adfour_link'); ?>" name="<?php echo $this->get_field_name('adfour_link'); ?>" type="text" value="<?php echo $adfour_link; ?>" />

        <h4>Ad Five</h4>
        <label for="<?php echo $this->get_field_id('adfive_img'); ?>"><?php _e('Image URL:'); ?></label>
        <input class="widefat" id="<?php echo $this->get_field_id('adfive_img'); ?>" name="<?php echo $this->get_field_name('adfive_img'); ?>" type="text" value="<?php echo $adfive_img; ?>" />
        <label for="<?php echo $this->get_field_id('adfive_link'); ?>"><?php _e('AD Link:'); ?></label>
        <input class="widefat" id="<?php echo $this->get_field_id('adfive_link'); ?>" name="<?php echo $this->get_field_name('adfive_link'); ?>" type="text" value="<?php echo $adfive_link; ?>" />

        <h4>Ad Six</h4>
        <label for="<?php echo $this->get_field_id('adsix_img'); ?>"><?php _e('Image URL::'); ?></label>
        <input class="widefat" id="<?php echo $this->get_field_id('adsix_img'); ?>" name="<?php echo $this->get_field_name('adsix_img'); ?>" type="text" value="<?php echo $adsix_img; ?>" />
        <label for="<?php echo $this->get_field_id('adsix_link'); ?>"><?php _e('AD Link:'); ?></label>
        <input class="widefat" id="<?php echo $this->get_field_id('adsix_link'); ?>" name="<?php echo $this->get_field_name('adsix_link'); ?>" type="text" value="<?php echo $adsix_link; ?>" />
                </p>
        <?php 
    }

} // class square_ad

// register square_ad widget
add_action( 'widgets_init', create_function( '', 'return register_widget("square_ad");' ) );
share|improve this question
1  
Include your complete widget class. There is no other to see the best direction. –  toscho Dec 20 '11 at 13:02
1  
I copied your code from the text file into the question(where it should have been to begin with). You can always remove it later by editing the question if necessary. –  t31os Dec 20 '11 at 15:28
add comment

1 Answer

up vote 0 down vote accepted

Should be possible like this:

<?php
    /** @see WP_Widget::widget */
        function widget( $args, $instance ) {
            extract( $args );
            $title = apply_filters( 'widget_title', $instance['title'] );
            # >>>> NEW
            if ( ! isset( $title ) OR empty( $title ) );
                return;
            # <<<< NEW
            echo $before_widget;
            if ( $title )
                echo $before_title . $title . $after_title; 
            ?>
            Hello, World!
            <?php 
            echo $after_widget;
        }
?>
share|improve this answer
    
Yeah sort of but I'm not trying to do it with the title, and It doesn't really fit in with the code. Nor can I get it to work.. Here is the code for the widget: magicapple.co.uk/code.txt On line 49 is where the code for the actual widget is. And I'm not using $before_widget or $after_widget either.. I could see it working using the format I showed in my question.. –  iJamesPine Dec 20 '11 at 14:34
    
And where's the problem replacing $title with something custom from your code? –  kaiser Dec 20 '11 at 14:40
    
Because I'm not entirely sure how to include that snippet into my code. And when I take out the bits I don't need I get I server error, I know I'm doing something wrong, but thats what I'm asking.. –  iJamesPine Dec 20 '11 at 16:26
    
Ok. First: You're doing something that's waaay beyond your skills, so stick with the comments: NEW enclosures the important part that checks if the attribute is set and return aborts it, if not present. The extract part, well... extracts your input arguments into stand alone variables. Then you can access 'title' => 'Some Title' as $title. The same way you can access every other input argument. Btw: You'd loose much less time if you'd sit down and try to study the basics of php for 2 hours... –  kaiser Dec 20 '11 at 16:57
    
Maybe so, but I am learning. I understand what most of the code does, I just have trouble changing it. I did however just try out this code: <?php if ( $adone_img ) echo $before_title . $title . $after_title; ?> which displays the title when there is a value in $adone_img and It works, I just need it to echo this code instead of the title: <a href="<?php echo ($adone_link);?>"><img class="box-container" src="<?php echo ($adone_img);?>" width="125" height="125" /></a> I realize you can't put PHP in PHP so I need to work out how to make that work.. –  iJamesPine Dec 20 '11 at 17:32
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.