0

My custom post has featured image. I need to retrieve it through widget. I am using get_the_post_thumbnail. But it is not working. Except this the rest of the widget is working fine.

Here is the short excerpt of my code

<?php
class Employee extends WP_Widget {
    /**
     * Register widget with WordPress.
     */
    function __construct() {
        // Instantiate the parent object
        $widget_ops = array( 
            'classname' => 'team_widget',
            'description' => 'Enter Empolyees to show them on front page.',
        );
        parent::__construct( false, 'Show Empolyees', $widget_ops );
    }    

    public function widget( $args, $instance ) {
        echo $args['before_widget'];     
        if ( ! empty( $instance['photo'] ))  {              
            echo $args['before_image']
            .$instance['photo'] 
            .$args['after_image']               
        }    
        echo $args['after_widget'];
    }    

    public function form( $instance ) {   
        if ( post_type_exists( 'employee' ) ) :              
        $args = array(
                    'type'                  => 'post',
                    'child_of'              => 0,
                    'parent'                => '',
                    'orderby'               => 'name',
                    'order'                 => 'ASC',
                    'hide_empty'            => 1,
                    'hierarchical'          => 1,
                    'exclude'               => '',
                    'include'               => '',
                    'number'                => '',
                    'taxonomy'              => 'department',
                    'pad_counts'            => false,
                    'posts_per_page'        => -1,
                    'post_type'             => 'employee',
                    'tax_query'             => array(
                                                    array(
                                                        'taxonomy' => 'department',
                                                        'field' => 'slug',
                                                        'terms' => 'department',
                                                    )
                                        ) 
                ); 
            $posts = get_posts( $args );
            if ( ! empty( $posts ) && ! is_wp_error( $posts ) ) :
                ?>

                <p>
                    <input type="checkbox" name="<?php echo $this->get_field_name( 'photo' ); ?>" 
                        value="<?php echo esc_html(get_the_post_thumbnail( $post->ID, 'thumbnail' ) ) ?>"
                        <?php checked( $instance['photo'], get_the_post_thumbnail( $post->ID, 'thumbnail' ) ); ?> >Show Photo
                    <br>
                </p>
                <?php
                endif; // chk posts based on terms.
        endif;//chk post type endif.
    }


    public function update( $new_instance, $old_instance ) {
        $instance = array();

        $instance['photo'] = ( ! empty( $new_instance['photo'] ) ) ? strip_tags( $new_instance['photo'] ) : '';         

        return $instance;
    }  

}

1 Answer 1

0

Your calls to get_the_post_thumbnail do work, the problem is how what you're doing with the result:

<input type="checkbox" name="<?php echo $this->get_field_name( 'photo' ); ?>"
    value="<?php echo esc_html(get_the_post_thumbnail( $post->ID, 'thumbnail' ) ) ?>"
    <?php checked( $instance['photo'], get_the_post_thumbnail( $post->ID, 'thumbnail' ) ); ?> >

You can't put image HTML tags inside of another tags attributes, that's not valid html. You're also using an escaping function that strips out HTML esc_html.

This will never work, and makes little sense:

<input type="checkbox" name="test" value="<img src="example.com/img.png">">

Instead consider something that generates valid markup, such as this:

<label><input type="checkbox" name="test"><img src="example.com/img.png"></label>

or

<input type="checkbox" id="testid" name="test">
<label for="testid"><img src="example.com/img.png"></label>

See here for how to create labels for checkboxes, and remember to use esc_attr to escape your value

4
  • Then what would be the value of the checkbox? What would be the value of $instance['photo'] ? This is my print_r($instance) result : Array ( [photo] => ) . If I check the box and save array does not take the value<img/>. Is there any other way to retrieve the post thumbnail so that I can save its value?
    – rushdi
    Feb 22, 2016 at 5:36
  • I want to pass the result of the get_the_post_thumbnail as the value of my checkbox value to save as $instance['photo'].
    – rushdi
    Feb 22, 2016 at 5:40
  • I solved the problem somehow. Would you please tell me if it is the right way or wrong way. <?php $image_url = wp_get_attachment_url( get_post_thumbnail_id( $post->ID, 'thumbnail' ) ); ?> <input type="checkbox" name="<?php echo $this->get_field_name( 'photo' ); ?>" value="<?php echo $image_url ?>" <?php checked( $instance['photo'], $image_url ); ?> >Show Photo Results this <input type="checkbox" name="widget-employee[7][photo]" value="localhost/site/wp/wp-theme-dev/wp-content/uploads/2016/02/…" checked="checked"> I am not sure if it is the right way to do this.
    – rushdi
    Feb 22, 2016 at 10:33
  • You're going to need to update your question to provide some context as to what the purpose of this is, why you're trying to build it, and what the goal is. I believe I've answered your question, but what you wrote and what you actually want are not the same thing
    – Tom J Nowell
    Feb 22, 2016 at 16:11

Your Answer

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

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