<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Upload Archives : vedmant.com : coding blog</title>
	<atom:link href="https://vedmant.com/tag/upload/feed/" rel="self" type="application/rss+xml" />
	<link>https://vedmant.com/tag/upload/</link>
	<description>Sharing my personal experience in web development</description>
	<lastBuildDate>Sun, 05 Mar 2017 21:45:37 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8</generator>
	<item>
		<title>Using WordPress Media Library in Widgets options</title>
		<link>https://vedmant.com/using-wordpress-media-library-in-widgets-options/</link>
					<comments>https://vedmant.com/using-wordpress-media-library-in-widgets-options/#comments</comments>
		
		<dc:creator><![CDATA[vedmant]]></dc:creator>
		<pubDate>Mon, 17 Aug 2015 19:50:49 +0000</pubDate>
				<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[Media Library]]></category>
		<category><![CDATA[Upload]]></category>
		<guid isPermaLink="false">https://vedmant.com/?p=101</guid>

					<description><![CDATA[<p>So how to add WordPress media uploader popup to our custom widget? That&#8217;s not so hard, hovewer there are a few caveats. Let&#8217;s start from our simple widget class, I&#8217;d recommend to get example from https://codex.wordpress.org/Widgets_API#Example. Then we will need to add our new option, it will be called &#8220;image&#8221;. Just copy and paste fields in &#8230; <a href="https://vedmant.com/using-wordpress-media-library-in-widgets-options/" class="more-link">Continue reading <span class="screen-reader-text">Using WordPress Media Library in Widgets options</span></a></p>
<p>The post <a href="https://vedmant.com/using-wordpress-media-library-in-widgets-options/">Using WordPress Media Library in Widgets options</a> appeared first on <a href="https://vedmant.com">vedmant.com :: coding blog</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h2>So how to add WordPress media uploader popup to our custom widget?</h2>
<p>That&#8217;s not so hard, hovewer there are a few caveats.</p>
<p>Let&#8217;s start from our simple widget class, I&#8217;d recommend to get example from<a href="https://codex.wordpress.org/Widgets_API#Example"> https://codex.wordpress.org/Widgets_API#Example</a>. Then we will need to add our new option, it will be called &#8220;image&#8221;. Just copy and paste fields in the &#8220;form&#8221; function, to render widget form and &#8220;update&#8221; function, to save this option.</p>
<p>I would recommend to catch php output buffer, it may be helpful if you will want to cache widget output. So our &#8220;widget&#8221; function will look like this:</p>
<pre class="brush: php; title: ; notranslate">

	public function widget( $args, $instance ) {
		// Our variables from the widget settings
		$title = apply_filters( 'widget_title', empty( $instance&#x5B;'title'] ) ? __( 'Default title', 'text_domain' ) : $instance&#x5B;'title'] );
		$image = ! empty( $instance&#x5B;'image'] ) ? $instance&#x5B;'image'] : '';

		ob_start();
		echo $args&#x5B;'before_widget'];
		if ( ! empty( $instance&#x5B;'title'] ) ) {
			echo $args&#x5B;'before_title'] . $title . $args&#x5B;'after_title'];
		}
		?&gt;

		&lt;?php if($image): ?&gt;
			&lt;img src=&quot;&lt;?php echo esc_url($image); ?&gt;&quot; alt=&quot;&quot;&gt;
		&lt;?php endif; ?&gt;

		&lt;?php
		echo $args&#x5B;'after_widget'];
		ob_end_flush();
	}

</pre>
<p>Our &#8220;form&#8221; function will be:</p>
<pre class="brush: php; title: ; notranslate">

	public function form( $instance ) {
		$title = ! empty( $instance&#x5B;'title'] ) ? $instance&#x5B;'title'] : __( 'New title', 'text_domain' );
		$image = ! empty( $instance&#x5B;'image'] ) ? $instance&#x5B;'image'] : '';
		?&gt;
		&lt;p&gt;
			&lt;label for=&quot;&lt;?php echo $this-&gt;get_field_id( 'title' ); ?&gt;&quot;&gt;&lt;?php _e( 'Title:' ); ?&gt;&lt;/label&gt;
			&lt;input class=&quot;widefat&quot; id=&quot;&lt;?php echo $this-&gt;get_field_id( 'title' ); ?&gt;&quot; name=&quot;&lt;?php echo $this-&gt;get_field_name( 'title' ); ?&gt;&quot; type=&quot;text&quot; value=&quot;&lt;?php echo esc_attr( $title ); ?&gt;&quot;&gt;
		&lt;/p&gt;
		&lt;p&gt;
			&lt;label for=&quot;&lt;?php echo $this-&gt;get_field_id( 'image' ); ?&gt;&quot;&gt;&lt;?php _e( 'Image:' ); ?&gt;&lt;/label&gt;
			&lt;input class=&quot;widefat&quot; id=&quot;&lt;?php echo $this-&gt;get_field_id( 'image' ); ?&gt;&quot; name=&quot;&lt;?php echo $this-&gt;get_field_name( 'image' ); ?&gt;&quot; type=&quot;text&quot; value=&quot;&lt;?php echo esc_url( $image ); ?&gt;&quot; /&gt;
			&lt;button class=&quot;upload_image_button button button-primary&quot;&gt;Upload Image&lt;/button&gt;
		&lt;/p&gt;
		&lt;?php
	}

</pre>
<p>And our update function:</p>
<pre class="brush: php; title: ; notranslate">

	public function update( $new_instance, $old_instance ) {
		$instance = array();
		$instance&#x5B;'title'] = ( ! empty( $new_instance&#x5B;'title'] ) ) ? strip_tags( $new_instance&#x5B;'title'] ) : '';
		$instance&#x5B;'image'] = ( ! empty( $new_instance&#x5B;'image'] ) ) ? $new_instance&#x5B;'image'] : '';

		return $instance;
	}

</pre>
<p>As you can see, I&#8217;ve added a button underneath image input field with class &#8220;.upload_image_button&#8221;, but it will not work for now and to make it work we will need to add our custom admin javascript.</p>
<p>First what we need is to create our javascript file and put it into our theme, let&#8217;s create a file, called our_admin.js in assets/js under our theme folder.</p>
<p>Next we will need to register and enqueue our script to make it load in the admin panel, so lets add few lines to our widget class constructor function:</p>
<pre class="brush: php; title: ; notranslate">

	function __construct() {

		// Add Widget scripts
		add_action('admin_enqueue_scripts', array($this, 'scripts'));

		parent::__construct(
			'our_widget', // Base ID
			__( 'Our Widget Title', 'text_domain' ), // Name
			array( 'description' =&gt; __( 'Our Widget with media files', 'text_domain' ), ) // Args
		);
	}

</pre>
<p>And we will need then &#8220;scripts&#8221; function in our widget class:</p>
<pre class="brush: php; title: ; notranslate">

	public function scripts()
	{
		wp_enqueue_script( 'media-upload' );
		wp_enqueue_media();
		wp_enqueue_script('our_admin', get_template_directory_uri() . '/assets/js/our_admin.js', array('jquery'));
	}

</pre>
<p>As you can notice, we also added wp_enqueue_script( &#8216;media-upload&#8217; ); and wp_enqueue_media(); to enqueue media library popup scripts.</p>
<p>And finally, here is our script, that will call WordPress media library popup and put selected image into the input field:</p>
<pre class="brush: jscript; title: ; notranslate">

jQuery(document).ready(function ($) {

	$(document).on(&quot;click&quot;, &quot;.upload_image_button&quot;, function (e) {
		e.preventDefault();
		var $button = $(this);


		// Create the media frame.
		var file_frame = wp.media.frames.file_frame = wp.media({
			title: 'Select or upload image',
			library: { // remove these to show all
				type: 'image' // specific mime
			},
			button: {
				text: 'Select'
			},
			multiple: false  // Set to true to allow multiple files to be selected
		});

		// When an image is selected, run a callback.
		file_frame.on('select', function () {
			// We set multiple to false so only get one image from the uploader

			var attachment = file_frame.state().get('selection').first().toJSON();

			$button.siblings('input').val(attachment.url);

		});

		// Finally, open the modal
		file_frame.open();
	});
});

</pre>
<p>So thats it, now, when you click on &#8220;Upload Image&#8221; button, WordPress media library popup will open and you will be able easily upload or select already uploaded image for widget options form.</p>
<p>The post <a href="https://vedmant.com/using-wordpress-media-library-in-widgets-options/">Using WordPress Media Library in Widgets options</a> appeared first on <a href="https://vedmant.com">vedmant.com :: coding blog</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://vedmant.com/using-wordpress-media-library-in-widgets-options/feed/</wfw:commentRss>
			<slash:comments>42</slash:comments>
		
		
			</item>
	</channel>
</rss>
