<?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>Wordpress Archives : vedmant.com : coding blog</title>
	<atom:link href="https://vedmant.com/tag/wordpress/feed/" rel="self" type="application/rss+xml" />
	<link>https://vedmant.com/tag/wordpress/</link>
	<description>Sharing my personal experience in web development</description>
	<lastBuildDate>Tue, 23 Apr 2019 13:54:08 +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>Laravel Shortcodes</title>
		<link>https://vedmant.com/laravel-shortcodes/</link>
					<comments>https://vedmant.com/laravel-shortcodes/#comments</comments>
		
		<dc:creator><![CDATA[vedmant]]></dc:creator>
		<pubDate>Tue, 23 Apr 2019 13:41:41 +0000</pubDate>
				<category><![CDATA[Laravel]]></category>
		<category><![CDATA[My Projects]]></category>
		<category><![CDATA[Composer]]></category>
		<category><![CDATA[Package]]></category>
		<category><![CDATA[Shortcodes]]></category>
		<category><![CDATA[Wordpress]]></category>
		<guid isPermaLink="false">https://vedmant.com/?p=241</guid>

					<description><![CDATA[<p>WordPress based Shortcodes for Laravel 5.x with shared variables, debugbar integration, flexible configuration and other useful features. Github: https://github.com/vedmant/laravel-shortcodes﻿ Build powerful and simple layouts using shortcodes in the content or views like this: Installation Via Composer This package supports Laravel Auto-Discover and will be discovered automatically. For Laravel version before 5.5 please add the Vedmant\LaravelShortcodes\LaravelShortcodesServiceProvider::class &#8230; <a href="https://vedmant.com/laravel-shortcodes/" class="more-link">Continue reading <span class="screen-reader-text">Laravel Shortcodes</span></a></p>
<p>The post <a href="https://vedmant.com/laravel-shortcodes/">Laravel Shortcodes</a> appeared first on <a href="https://vedmant.com">vedmant.com :: coding blog</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>WordPress based Shortcodes for Laravel 5.x with shared variables, debugbar integration, flexible configuration and other useful features.</p>



<p>Github: <a href="https://github.com/vedmant/laravel-shortcodes" target="_blank" rel="noreferrer noopener" aria-label=" (opens in a new tab)">https://github.com/vedmant/laravel-shortcodes﻿</a></p>



<p>Build powerful and simple layouts using shortcodes in the content or views like this:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: xml; title: ; notranslate">
&#x5B;b]Bold text&#x5B;/b]
&#x5B;row]
  &#x5B;col md=8]
     &#x5B;posts_list types="post,gallery" show_tags="yes"]
  &#x5B;/col]
  &#x5B;col md=4]
     &#x5B;poll id="1"]
     &#x5B;user_info username="test_user" website="mywebsite.com" active="yes"]
     &#x5B;last_free_post title="Free Posts"]
  &#x5B;/col]
&#x5B;/row]
</pre></div>


<h2 class="wp-block-heading"><a href="https://github.com/vedmant/laravel-shortcodes#installation"></a>Installation</h2>



<p>Via Composer</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
$ composer require vedmant/laravel-shortcodes
</pre></div>


<p>This package supports Laravel Auto-Discover and will be discovered automatically.</p>



<p>For Laravel version before 5.5 please add the Vedmant\LaravelShortcodes\LaravelShortcodesServiceProvider::class to the providers array in&nbsp;<code>config/app.php</code>. And optionally &#8216;Shortcodes&#8217; =&gt; Vedmant\LaravelShortcodes\Facades\Shortcodes::class, to aliases.</p>



<h2 class="wp-block-heading"><a href="https://github.com/vedmant/laravel-shortcodes#configuraton"></a>Configuraton</h2>



<p>Publish configuration.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
php artisan vendor:publish --tag=shortcodes
</pre></div>


<h2 class="wp-block-heading"><a href="https://github.com/vedmant/laravel-shortcodes#usage"></a>Usage</h2>



<h3 class="wp-block-heading"><a href="https://github.com/vedmant/laravel-shortcodes#shortcode-class"></a>Shortcode class</h3>



<p>Shortcode class should extend abstract \Vedmant\LaravelShortcodes\Shortcode class.</p>



<p>This packages adds&nbsp;<code>make:shortcode</code>&nbsp;artisan command:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
php artisan make:shortcode PostsListShortcode
</pre></div>


<p>It will generate a shortcode class in the&nbsp;<code>app/Shortcodes</code>&nbsp;folder by default.</p>



<h3 class="wp-block-heading"><a href="https://github.com/vedmant/laravel-shortcodes#register-shortcodes"></a>Register shortcodes</h3>



<p>You can use AppServiceProvider boot method to register all needed shortcodes.</p>



<p>Using shortcode class:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: php; title: ; notranslate">
Shortcodes::add('b', BShortcode::class);
</pre></div>


<p>Using shortcode classes in array, preferable for lots of shortcodes:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: php; title: ; notranslate">
Shortcodes::add(&#x5B;
   &#039;a&#039; =&gt; AShortcode::class,
   &#039;b&#039; =&gt; BShortcode::class,
]);
</pre></div>


<p>Using closure:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: php; title: ; notranslate">
Shortcodes::add(&#039;test&#039;, function ($atts, $content, $tag, $manager) {
   return new HtmlString(&#039;&lt;strong&gt;some test shortcode&lt;/strong&gt;&#039;);
});

</pre></div>


<h3 class="wp-block-heading"><a href="https://github.com/vedmant/laravel-shortcodes#rendering-shortcodes"></a>Rendering shortcodes</h3>



<p>By default this packages extends View to parse all shortcodes during views rendering. This feature can be disabled in the config file.</p>



<p>Also to enable / disable rendering shortcodes for specific view you can use:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: php; title: ; notranslate">
view(&#039;some-view&#039;)-&gt;withShortcodes(); // Or view(&#039;some-view&#039;)-&gt;withoutShortcodes();
</pre></div>


<p>To render shortcodes manually use:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: php; title: ; notranslate">
{{ Shortcodes::render('&#x5B;b]bold&#x5B;/b]') }}
</pre></div>


<h3 class="wp-block-heading"><a href="https://github.com/vedmant/laravel-shortcodes#shared-attributes"></a>Shared attributes</h3>



<p>YOccasionally, you may need to share a piece of data with all shortcodes that are rendered by your application. You may do so using the shortode facade&#8217;s&nbsp;<code>share</code>&nbsp;method. Typically, you should place calls to share in the controller, or within a service provider&#8217;s boot method.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: php; title: ; notranslate">
Shortcodes::share('post', $post);
</pre></div>


<p>Then you can get share attributes in the shortcode class:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: php; title: ; notranslate">
$post = $this-&gt;shared(&#039;post&#039;); $allShared = $this-&gt;shared();
</pre></div>


<h3 class="wp-block-heading"><a href="https://github.com/vedmant/laravel-shortcodes#comma-separated-values-array-attributes"></a>Comma separated values (array attributes)</h3>



<p>If you need to pass an array to a shortcode, you can pass values separated by comma:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
&#x5B;posts_list ids="1,2,3"]
</pre></div>


<p>Then in render function you can parse this attribute using build in method:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: php; title: ; notranslate">
$ids = $this-&gt;parseCommaSeparated($atts&#x5B;&#039;ids&#039;]);
</pre></div>


<p>Edit configuration file as needed.</p>



<h3 class="wp-block-heading"><a href="https://github.com/vedmant/laravel-shortcodes#integration-with-laravel-debugbar"></a>Integration with Laravel Debugbar</h3>



<p>This packages supports Laravel Debugbar. Integration can be disabled in the config file if needed.<br></p>



<p>This project is fully free to use for any purpose and licensed under MIT License.</p>
<p>The post <a href="https://vedmant.com/laravel-shortcodes/">Laravel Shortcodes</a> appeared first on <a href="https://vedmant.com">vedmant.com :: coding blog</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://vedmant.com/laravel-shortcodes/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<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>
