Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress How to Build a WordPress Plugin Building WordPress Widgets, and Shortcodes How to Create WordPress Widgets

Ulises Calvo
Ulises Calvo
2,233 Points

Can anyone explain this Widget code for me please?

0 down vote favorite

I'm trying to read and understand a widget from a Wp theme.

This widget basically allows the user to display up to 3 featured pages with their corresponding featured images or thumbnails.

I understand the WordPress widget structure but some of the code here is over my head, namely the widget output and the widget form.

Can anyone comment this code out for me please? Meaning explaining what means what.

I know it's a little bit of a pain, but someone with some good PHP proficiency could do it fast and it would really help me a great deal with my learning process.

Thank you! Code:

class spacious_recent_work_widget extends WP_Widget { function spacious_recent_work_widget() { $widget_ops = array( 'classname' => 'widget_recent_work', 'description' => _( 'Show your some pages as recent work. Best for Business Top or Bottom sidebar.', 'spacious' ) ); $control_ops = array( 'width' => 200, 'height' =>250 ); parent::WP_Widget( false, $name = _( 'TG: Featured Widget', 'spacious' ), $widget_ops, $control_ops); }

function form( $instance ) { for ( $i=0; $i<3; $i++ ) { $var = 'page_id'.$i; $defaults[$var] = ''; } $att_defaults = $defaults; $att_defaults['title'] = ''; $att_defaults['text'] = ''; $instance = wp_parse_args( (array) $instance, $att_defaults ); for ( $i=0; $i<3; $i++ ) { $var = 'page_id'.$i; $var = absint( $instance[ $var ] ); } $title = esc_attr( $instance[ 'title' ] ); $text = esc_textarea($instance['text']); ?>

<p>
    <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e( 'Title:', 'spacious' ); ?></label> 
    <input id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
</p>
<?php _e( 'Description','spacious' ); ?>
<textarea class="widefat" rows="10" cols="20" id="<?php echo $this->get_field_id('text'); ?>" name="<?php echo $this->get_field_name('text'); ?>"><?php echo $text; ?></textarea>
<?php
for( $i=0; $i<3; $i++) { 
    ?>
    <p>
        <label for="<?php echo $this->get_field_id( key($defaults) ); ?>"><?php _e( 'Page', 'spacious' ); ?>:</label>
        <?php wp_dropdown_pages( array( 'show_option_none' =>' ','name' => $this->get_field_name( key($defaults) ), 'selected' => $instance[key($defaults)] ) ); ?>
    </p>
<?php
next( $defaults );// forwards the key of $defaults array
}

}

function update( $new_instance, $old_instance ) { $instance = $old_instance; $instance[ 'title' ] = strip_tags( $new_instance[ 'title' ] ); for( $i=0; $i<3; $i++ ) { $var = 'page_id'.$i; $instance[ $var] = absint( $new_instance[ $var ] ); } if ( current_user_can('unfiltered_html') ) $instance['text'] = $new_instance['text']; else $instance['text'] = stripslashes( wp_filter_post_kses( addslashes($new_instance['text']) ) ); // wp_filter_post_kses() expects slashed $instance['filter'] = isset($new_instance['filter']);

return $instance;

}

function widget( $args, $instance ) { extract( $args ); extract( $instance );

global $post;
$title = isset( $instance[ 'title' ] ) ? $instance[ 'title' ] : '';
$text = apply_filters( 'widget_text', empty( $instance['text'] ) ? '' : $instance['text'], $instance );
$page_array = array();
for( $i=0; $i<3; $i++ ) {
    $var = 'page_id'.$i;
    $page_id = isset( $instance[ $var ] ) ? $instance[ $var ] : '';

    if( !empty( $page_id ) )
        array_push( $page_array, $page_id );// Push the page id in the array
}
$get_featured_pages = new WP_Query( array(
    'posts_per_page'            => -1,
    'post_type'                 =>  array( 'page' ),
    'post__in'                  => $page_array,
    'orderby'                   => 'post__in'
) );
echo $before_widget;
?>
    <div class="tg-one-fourth tg-column-1">
        <?php
        if ( !empty( $title ) ) { echo $before_title . esc_html( $title ) . $after_title; } ?>
        <p><?php echo esc_textarea( $text ); ?></p>
    </div>
        <?php
        $i=2;
        while( $get_featured_pages->have_posts() ):$get_featured_pages->the_post();
            if ( $i % 4 == 0 ) { $class = 'tg-one-fourth tg-one-fourth-last'.' tg-column-'.$i; }
            elseif( $i % 3 == 0 ) { $class= 'tg-one-fourth tg-after-two-blocks-clearfix'.' tg-column-'.$i; }
            else { $class = 'tg-one-fourth'.' tg-column-'.$i; }
            $page_title = get_the_title();
            ?>  
            <div class="<?php echo $class; ?>">
                <?php 
                if ( has_post_thumbnail( ) ) {
                    echo '<div class="service-image"><a title="'.get_the_title().'" href="'.get_permalink().'">'.get_the_post_thumbnail( $post->ID,'featured-blog-medium').'</a></div>';                
                }
                ?>
            </div>          
        <?php 
            $i++;
        endwhile;
        // Reset Post Data
        wp_reset_query(); 
        ?>
<?php echo $after_widget;
}