Skip to main content
edited title
Link
fuxia
  • 107.1k
  • 38
  • 255
  • 459

Wordpress - How to add media field to WP_Widget form function?

Source Link

Wordpress - How to add media field to WP_Widget form function?

I am creating simple WP_Widget plugin and I want to add one filed to form function inside it. I want to allow user to pick some photo from wordpress media. I know how to implement input text, textarea or checkboxes but I don't know how to write field from wordpress media.

function form:

<p>
    <label for="<?php echo esc_attr( $this->get_field_id( 'card_two_desc' ) ); ?>"><?php esc_attr_e( 'Card #2 Description:', 'text_domain' ); ?></label> 
    <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'card_two_desc' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'card_two_desc' ) ); ?>" type="file" value="<?php echo esc_attr( $card_two_desc ); ?>"></input>
    </p>

Update function:

$instance['card_two_desc'] = ( ! empty( $new_instance['card_two_desc'] ) ) ? sanitize_text_field( $new_instance['card_two_desc'] ) : '';

and I want to simpy display this value here:

<div class="card">
            <img src="<?php echo $instance['card_two_desc'] ?>" alt="">
            <h3 class="card__title"><?php echo $instance['card_one'] ?></h3>
            <p class="card__description"><?php echo $instance['card_one_desc'] ?></p>
        </div>