<?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>Flav36rs &#187; plugin</title>
	<atom:link href="http://flav36rs.com/tag/plugin/feed/" rel="self" type="application/rss+xml" />
	<link>http://flav36rs.com</link>
	<description>Just another technical blog</description>
	<lastBuildDate>Mon, 12 Sep 2011 16:56:36 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Using front-end AJAX requests in your WordPress plugins</title>
		<link>http://flav36rs.com/2011/09/12/using-front-end-ajax-requests-in-your-wordpress-plugins/</link>
		<comments>http://flav36rs.com/2011/09/12/using-front-end-ajax-requests-in-your-wordpress-plugins/#comments</comments>
		<pubDate>Mon, 12 Sep 2011 16:55:37 +0000</pubDate>
		<dc:creator>Steve</dc:creator>
				<category><![CDATA[Plugins]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[plugin]]></category>

		<guid isPermaLink="false">http://flav36rs.com/?p=843</guid>
		<description><![CDATA[Although it is fairly uncommon to be adding AJAX functionality to your WordPress plugins, it can be necessary for it to be added. Luckily it is quite easy and straight forward to integrate the required components as and when they &#8230; <a href="http://flav36rs.com/2011/09/12/using-front-end-ajax-requests-in-your-wordpress-plugins/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Although it is fairly uncommon to be adding AJAX functionality to your WordPress plugins, it can be necessary for it to be added. Luckily it is quite easy and straight forward to integrate the required components as and when they are required.</p>
<p>To help explain how this can be achieved, we will be creating a plugin called &#8220;<strong>Ajax Example</strong>&#8220;, stored in the folder &#8220;<strong>ajax-example</strong>&#8221; inside the plugin directory of your WordPress install.</p>
<p><span id="more-843"></span>Further information on <a title="Writing a Plugin" href="http://codex.wordpress.org/Writing_a_Plugin" target="_blank">creating a WordPress plugin</a> can be found on <a title="WordPress.org" href="http://wordpress.org/" target="_blank">WordPress.org</a> and other resources, so won&#8217;t go into too much detail here.</p>
<p>First we need to create a file called &#8220;<strong>index.html</strong>&#8221; and copy the following into this file:</p>
<pre>&lt;?php
/*
Plugin Name: Ajax Example
Plugin URI: http://wp.me/pWbfz-dB
Description: Simple example of how to integrate front-end ajax requests via your plugin.
Version: 0.1
Date: 2011-09-01
Author: Steve Whiteley
Author URI: http://flav36rs.com
*/

class Ajax_Example
{
	public function __construct()
	{
		// ...
 	}
}

$ajaxExample = new Ajax_Example();

?&gt;</pre>
<p>We will be using the OOP structure for this plugin instead of the prefixed function format, however could be converted with ease. This class provides the skeleton structure of our plugin, you should save this file to our &#8220;<strong>ajax-example</strong>&#8221; folder before we start to add more functionality.</p>
<p>The next step is to include the <strong>JavaScript</strong> file that performs the AJAX request, which is called by the &#8220;<a title="Init Action Hook" href="http://codex.wordpress.org/Plugin_API/Action_Reference/init" target="_blank">init</a>&#8221; action hook added to the class constructor method:</p>
<pre>add_action( 'init', array( &amp;$this, 'init' ) );</pre>
<p>Then we add the init function to the plugin, which includes calls to insert the JavaScript file <em>(wp_enqueue_script)</em> and set the parameters required by the external file <em>(wp_localize_script)</em>.</p>
<pre>public function init()
{
	wp_enqueue_script( 'ajax-example', plugin_dir_url( __FILE__ ) . 'ajax.js', array( 'jquery' ) );
	wp_localize_script( 'ajax-example', 'AjaxExample', array(
		'ajaxurl' =&gt; admin_url( 'admin-ajax.php' ),
		'nonce' =&gt; wp_create_nonce( 'ajax-example-nonce' )
	) );
}</pre>
<p>Additional parameters can be added to array if they are required, the two mentioned should be the minimum that are required.</p>
<p>While we&#8217;re here, we should add the function that handles the response generated by the AJAX request. Notice here that we check the nonce value that is defined in the function above and set the content type before returning the encoded JSON array.</p>
<pre>public function ajax_call()
{
	if ( ! isset( $_REQUEST['nonce'] ) || ! wp_verify_nonce( $_REQUEST['nonce'], 'ajax-example-nonce' ) )
		die ( 'Invalid Nonce' );
	header( "Content-Type: application/json" );
	echo json_encode( array(
		'success' =&gt; true,
		'time' =&gt; time()
	) );
	exit;
}</pre>
<p>Create &#8220;<strong>ajax-example/ajax.js</strong>&#8221; and copy the following snippet into this file. In order to perform the AJAX request, we use the jQuery .getJSON() method. <a title="jQuery AJAX methods" href="http://api.jquery.com/category/ajax/" target="_blank">Alternative methods</a> could be used depending on the type of request you are performing.</p>
<pre>jQuery.getJSON(
    AjaxExample.ajaxurl,
    {
        action: 'ajax-example',
        nonce: AjaxExample.nonce
    },
    function( response ) {
    	alert( response.success );
    }
);</pre>
<p>Unfortunately this won&#8217;t actually do anything until we add two hooks to extend the built in AJAX functionality. These are added to the class constructor, the first <em>(wp_ajax_nopriv_ajax-example)</em> provides access for logged in users, the second <em>(wp_ajax_ajax-example)</em> for anonymous / non-logged in visitors.</p>
<pre>if ( is_admin() ) {
	add_action( 'wp_ajax_nopriv_ajax-example', array( &amp;$this, 'ajax_call' ) );
	add_action( 'wp_ajax_ajax-example', array( &amp;$this, 'ajax_call' ) );
}</pre>
<p>Please be aware that when using these hooks, they are called from within <strong>is_admin()</strong>, otherwise they are not correctly set.</p>
<p>The full contents of the plugin file &#8220;<strong>ajax-example/index.php</strong>&#8221; should be as follows:</p>
<pre>&lt;?php
/*
Plugin Name: Ajax Example
Plugin URI: http://wp.me/pWbfz-dB
Description: Simple example of how to integrate front-end ajax requests via your plugin.
Version: 0.1
Date: 2011-09-01
Author: Steve Whiteley
Author URI: http://flav36rs.com
*/

class Ajax_Example
{
	public function __construct()
	{
		if ( is_admin() ) {
			add_action( 'wp_ajax_nopriv_ajax-example', array( &amp;$this, 'ajax_call' ) );
			add_action( 'wp_ajax_ajax-example', array( &amp;$this, 'ajax_call' ) );
		}
		add_action( 'init', array( &amp;$this, 'init' ) );
	}

	public function init()
	{
		wp_enqueue_script( 'ajax-example', plugin_dir_url( __FILE__ ) . 'ajax.js', array( 'jquery' ) );
		wp_localize_script( 'ajax-example', 'AjaxExample', array(
		    'ajaxurl' =&gt; admin_url( 'admin-ajax.php' ),
		    'nonce' =&gt; wp_create_nonce( 'ajax-example-nonce' )
		) );
	}

	public function ajax_call()
	{
		if ( ! isset( $_REQUEST['nonce'] ) || ! wp_verify_nonce( $_REQUEST['nonce'], 'ajax-example-nonce' ) )
			die ( 'Invalid Nonce' );
		header( "Content-Type: application/json" );
		echo json_encode( array(
			'success' =&gt; true,
			'time' =&gt; time()
		) );
		exit;
	}

}

$ajaxExample = new Ajax_Example();

?&gt;</pre>
<p>Other things to consider when using the example code provided above are that you should probably ensure jQuery is loaded and possibly only add the the scripts to the pages that they are required, rather than globally.</p>
<p>This functionality was tested with <strong>WordPress 3.2.1</strong>, it may however change and be simplified in future releases.</p>
]]></content:encoded>
			<wfw:commentRss>http://flav36rs.com/2011/09/12/using-front-end-ajax-requests-in-your-wordpress-plugins/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Using WP-Syntax and the visual editor</title>
		<link>http://flav36rs.com/2009/09/15/using-wp-syntax-and-the-visual-editor/</link>
		<comments>http://flav36rs.com/2009/09/15/using-wp-syntax-and-the-visual-editor/#comments</comments>
		<pubDate>Tue, 15 Sep 2009 20:09:40 +0000</pubDate>
		<dc:creator>Steve</dc:creator>
				<category><![CDATA[Blogging]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[tinymce]]></category>
		<category><![CDATA[wp-syntax]]></category>

		<guid isPermaLink="false">http://36flavours.com/?p=388</guid>
		<description><![CDATA[Many of the blog posts I write tend to be about web development related topics and will often include a number of code snippets. My current choice of syntax highlighter is WP-Syntax, which supports a wide range of popular languages &#8230; <a href="http://flav36rs.com/2009/09/15/using-wp-syntax-and-the-visual-editor/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Many of the blog posts I write tend to be about web development related topics and will often include a number of code snippets. My current choice of syntax highlighter is <a href="http://wordpress.org/extend/plugins/wp-syntax/" target="_blank">WP-Syntax</a>, which supports a wide range of popular languages and has the ability to also include line numbers.</p>
<p>The problem with using this plug-in is that the <a href="http://wordpress.org/" target="_blank">WordPress</a> WYSIWYG Editor (<a href="http://tinymce.moxiecode.com/" target="_blank">TinyMCE</a>), will <strong>remove any tags and attributes</strong> that it believes to be invalid according to it&#8217;s configuration. As two of the attributes used by this plug-in are custom attributes <em>(escaped/line)</em> they are removed, causing some unexpected output.<span id="more-388"></span></p>
<p>One solution to this problem is to add the custom attributes to the list of valid elements by using the &#8216;<strong>tiny_mce_before_init</strong>&#8216; filter and appending them to the &#8216;<strong>extended_valid_elements&#8217;</strong> string.</p>
<p>The function below can be added to the <em>functions.php</em> file within your theme directory:</p>
<pre lang="php" escaped="true">function tinymce_wp_syntax($init) {
	$values = 'pre[lang|escaped=true|line]';
	if (empty($init['extended_valid_elements'])) {
		$init['extended_valid_elements'] = $values;
	} else {
		$init['extended_valid_elements'] .= ','.$values;
	}
	return $init;
}
add_filter('tiny_mce_before_init', 'tinymce_wp_syntax');</pre>
<p>Adding this to my blog enables me to continue using the visual editor instead of disabling it and at the same time will allow the other HTML filters to continue as normal <img src='http://flav36rs.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<p><strong>Note:</strong> You may have to force refresh (ctrl + f5) the post / page editing page for the changes to take effect.</p>
<p><strong>EDIT</strong>: This solution does not fully solve the problem in <a href="http://mu.wordpress.org/">WordPress MU</a> installations, as you need to adjust the allowed post tags used by the content filter.</p>
<pre lang="php" escaped="true">if (!CUSTOM_TAGS) {
	$allowedposttags['pre'] =  array(
		'lang' =&gt; array(),
		'escaped' =&gt; array('true' =&gt; array()),
		'line' =&gt; array()
	);
}</pre>
]]></content:encoded>
			<wfw:commentRss>http://flav36rs.com/2009/09/15/using-wp-syntax-and-the-visual-editor/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>CSS Opacity in Opera using jQuery</title>
		<link>http://flav36rs.com/2009/02/26/css-opacity-in-opera-using-jquery/</link>
		<comments>http://flav36rs.com/2009/02/26/css-opacity-in-opera-using-jquery/#comments</comments>
		<pubDate>Wed, 25 Feb 2009 23:05:51 +0000</pubDate>
		<dc:creator>Steve</dc:creator>
				<category><![CDATA[Browsers]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[bug]]></category>
		<category><![CDATA[opera]]></category>
		<category><![CDATA[plugin]]></category>

		<guid isPermaLink="false">http://sha.re.it/?p=134</guid>
		<description><![CDATA[Whilst attempting to cross browser test a jQuery plug-in that I&#8217;m currently working on, I noticed that my opacity settings were being ignored in my current version of Opera (9.23). $("#id").css({opacity: 0.8}); It turns out that the latest version of &#8230; <a href="http://flav36rs.com/2009/02/26/css-opacity-in-opera-using-jquery/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Whilst attempting to cross browser test a <a href="http://jquery.com/">jQuery</a> plug-in that I&#8217;m currently working on, I noticed that my opacity settings were being ignored in my current version of <a href="http://www.opera.com/browser/">Opera</a> (9.23).</p>
<pre lang="javascript">$("#id").css({opacity: 0.8});</pre>
<p>It turns out that the latest version of <a href="http://docs.jquery.com/Downloading_jQuery">jQuery (1.3.1)</a> doesn&#8217;t believe that Opera 9.2 supports CSS opacity, and therefore it is ignored completely.<span id="more-134"></span></p>
<p>There are two possible fixes for this, the first is to simply <strong>upgrade Opera</strong> to a newer version (the latest is 9.6.3).</p>
<p>The second option is to tell jQuery that opera does support opacity using the following &#8211; or similar &#8211; within your script:</p>
<pre lang="javascript">if ($.browser.opera) {
    $.support.opacity = true;
}</pre>
]]></content:encoded>
			<wfw:commentRss>http://flav36rs.com/2009/02/26/css-opacity-in-opera-using-jquery/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

