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 create widget for showing image

Hi Team

I am coding a widget for showing an image. and My problem is the image does not display. Here is my code:

<?php

        class oseBadgeWidget extends WP_Widget{

        public function oseBadgeWidget(){

            $widget_ops = array(
                'classname' => 'ose-badge-widget',
                'description' => 'Show the OSE Firewall Badget'
            );

            $control_ops = array(
                'width' => 200, 'height' => 250,
            );

            $this->WP_Widget('ose_Badge_Widget', 'OSE Badge Widget', $widget_ops, $control_ops);
        }

        public function __construct(){

            parent:: __construct(
                'ose_Badge_Widget',
                'OSE Badge Widget',
                array('description' => __('Show the OSE Firewall Badget'), )
            );  
        }

        public function form($instance){

            if (isset($instance['file_path'])){ $file_path = $instance[ 'file_path' ]; } else { $image_width = __( '', 'text_domain' ); }

        ?>
            <p>
                    <label for="<?php echo $this->get_field_id('file_path' ); ?>"><?php _e( 'File Path:' ); ?></label> 
                    <input class="file_path" id="<?php echo $this->get_field_id( 'file_path' ); ?>" name="<?php echo $this->get_field_name( 'file_path' ); ?>" type="text" value="<?php echo esc_attr( $file_path ); ?>" />
            </p>

        <?php   
        }

        public function update($new_instance, $old_instance){

            $instance['file_path'] = strip_tags( $new_instance['file_path'] );

            return $instance;
        }


        public function widget($args, $instance){

            extract($args);

            if ($instance['file_path'] != ''){$file_path = $instance['file_path'];} else { $file_path = '';}

            $handle = opendir($file_path);
            $file = readdir($handle);

            echo $before_widget;

            echo '<img src= "picture/'.$file.'" border = "0" />';
            echo $after_widget;

            //$handle = opendir(dirname(realpath(__FILE__)).'/picture/');
            //$file = readdir($handle);


            //echo '<img src= "picture/'.$file.'" border = "0" />';
        }   
    }
    add_action( 'widgets_init', create_function( '', 'register_widget( "oseBadgeWidget" );' ) );

?>

1 Answer

Zac Gordon
STAFF
Zac Gordon
Treehouse Guest Teacher

When you echo out the file path, what does it show?