<?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>Ryan Pharis &#187; plugins</title>
	<atom:link href="http://ryanpharis.com/tag/plugins/feed/" rel="self" type="application/rss+xml" />
	<link>http://ryanpharis.com</link>
	<description>Dallas Forth Worth Metroplex, Texas Freelance PHP CodeIgniter Web Developer &#38; WordPress as a CMS Consultant</description>
	<lastBuildDate>Mon, 15 Mar 2010 17:29:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Ajax for WordPress Plugins Using jQuery Tutorial</title>
		<link>http://ryanpharis.com/2008/10/ajax-for-wordpress-plugins-using-jquery/</link>
		<comments>http://ryanpharis.com/2008/10/ajax-for-wordpress-plugins-using-jquery/#comments</comments>
		<pubDate>Thu, 23 Oct 2008 01:30:22 +0000</pubDate>
		<dc:creator>Ryan</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[plugins]]></category>

		<guid isPermaLink="false">http://ryanpharis.com/?p=33</guid>
		<description><![CDATA[I recently had the need to perform some ajax functions on the backend of Texas Tech Today and wasn&#8217;t sure exactly how to go about it. So after a search, I found this great explanation: Simplified AJAX For WordPress Plugin Developers using Jquery. But it stops short of explaining how to receive some data from [...]]]></description>
			<content:encoded><![CDATA[<p>I recently had the need to perform some ajax functions on the backend of <a title="Texas Tech Today" href="http://today.ttu.edu" target="_blank">Texas Tech Today</a> and wasn&#8217;t sure exactly how to go about it. So after a search, I found this great explanation: <a title="Simplified AJAX For WordPress Plugin Developers using Jquery" href="http://amiworks.co.in/talk/simplified-ajax-for-wordpress-plugin-developers-using-jquery" target="_blank">Simplified AJAX For WordPress Plugin Developers using Jquery</a>. But it stops short of explaining how to receive some data from a POST, give it to PHP to do something with, and then return a result back to the form.</p>
<p>So, I thought I would detail how to do that in WordPress in a plugin.</p>
<p>The plugin just has a basic options page (Settings -&gt; Ajax4WP) and lets you save an option to the database via ajax. Go ahead and <a title="Ajax for WordPress plugins" href="http://ryanpharis.com/wp-content/uploads/2008/10/ajax4wp.zip">download</a> and install it; you&#8217;ll see how simple it is.</p>
<p><strong>1. Create a php file that will have our javascript code, and save it into your plugins folder.</strong></p>
<p>The reason we need our javascript code to be in a PHP file is to get the &#8220;get_option(&#8216;siteurl&#8217;)&#8221; value dynamically, since we&#8217;ll need that to reference our PHP file with the ajax function.</p>
<p>js-Ajax4WP.php (<a title="Ajax for WordPress plugins" href="http://ryanpharis.com/wp-content/uploads/2008/10/ajax4wp.zip">download</a> the plugin files)</p>
<pre>&lt;?php
include('../../../wp-config.php');
$site_url = get_option('siteurl');
?&gt;
jQuery(document).ready(function() {

   //when the link with the id of save_settings is clicked, get the value of the text field and start doing the ajax stuff
   jQuery('#save_settings').click(function() {

      var Ajax4WP_test_setting = jQuery("#Ajax4WP_test_setting").val();

      //action is the name of the php function in your main plugin file
      jQuery.post("&lt;?php echo $site_url; ?&gt;/wp-admin/admin-ajax.php", {action:"Ajax4WP_save_settings", 'cookie': encodeURIComponent(document.cookie), Ajax4WP_test_setting:Ajax4WP_test_setting},
      function(res)
      {
         var message_result = eval('(' + res + ')');
         if (!message_result.success) {
            jQuery("#Ajax4WP_test_setting").css("border","2px solid #cc0000");
         }
         alert(message_result.message + ' ' + message_result.setting);
      });

      return false;
   });

});</pre>
<p>This is jQuery ajax code that takes the value of a text field and passes it to Ajax4WP_save_settings() in our plugin file. We use <a title="jQuery.post" href="http://docs.jquery.com/Ajax/jQuery.post" target="_blank">jQuery.post</a> to define a post variable (Ajax4WP_test_setting) that PHP can use.</p>
<p>Once the plugin code does its thing on the server (step 2 below), it returns the result back to this function at &#8220;function(res)&#8221;. Then we eval the json result and use an alert to display a message.</p>
<p><strong>2. Create your standard plugin file and put in the Ajax4WP_save_settings function.</strong></p>
<p>Ajax4WP.php (<a title="Ajax for WordPress plugins" href="http://ryanpharis.com/wp-content/uploads/2008/10/ajax4wp.zip">download</a> the plugin files)</p>
<pre>...

// our ajax action:
add_action('wp_ajax_Ajax4WP_save_settings', array(&amp;$this, 'Ajax4WP_save_settings'), 10);

function Ajax4WP_save_settings()
		{
		   $name = 'Ajax4WP_test_setting';
			$value = $_POST['Ajax4WP_test_setting'];

			if ($value == "") {
				$message_result = array(
					'setting' =&gt; "",
					'message' =&gt; 'Please enter a Setting Value!',
					'success' =&gt; FALSE
	         );
			}
			else {
			   update_option($name, $value);
				$message_result = array(
					'setting' =&gt; $value,
					'message' =&gt; 'Saved! You entered:',
				   'success' =&gt; TRUE
	         );
			}
			echo json_encode($message_result);
			exit;
		}
...</pre>
<p>This is standard PHP to process a POST request. When we&#8217;re through, we echo the <a title="json_encode - php 5.2 only" href="http://us.php.net/manual/en/function.json-encode.php" target="_blank">json_encode()</a> result. NOTE: json_encode is a PHP &gt;= 5.2 only function, so be sure you are up to date on your PHP install.</p>
<p><strong>3. Make a php file that will contain basic html and will get included on our settings page</strong></p>
<p>Ajax4WP-settings.php (<a title="Ajax for WordPress plugins" href="http://ryanpharis.com/wp-content/uploads/2008/10/ajax4wp.zip">download</a> the plugin files)</p>
<pre>&lt;div id="Ajax4WP Settings" class="inside" style="margin-left: 29px; width: 200px;"&gt;

   &lt;h3&gt;Ajax4WP Settings&lt;/h3&gt;

   &lt;div id="loading_message"&gt;&lt;/div&gt;

   &lt;p&gt;
   &lt;label for="Ajax4WP_test_setting"&gt;Setting Value:&lt;/label&gt;
   &lt;br /&gt;
   &lt;input type="text" id="Ajax4WP_test_setting" name="Ajax4WP_test_setting" value="&lt;?php if (isset($this-&gt;Ajax4WP_test_setting)) { echo $this-&gt;Ajax4WP_test_setting; } ?&gt;" size="20" /&gt;
   &lt;/p&gt;

   &lt;p&gt;&lt;a href="#" id="save_settings" class="button button-highlighted"&gt;Save Settings&lt;/a&gt;&lt;/p&gt;

&lt;/div&gt;</pre>
<p>This is just a &#8220;stub&#8221; and gets included in our plugin. That way we don&#8217;t have a lot of html code mucking up the main plugin file. You can see what&#8217;s going on better in the source code when you <a title="How to use Ajax in your WordPress plugins" href="http://ryanpharis.com/wp-content/uploads/2008/10/ajax4wp.zip">download the files</a>.</p>
<p>I hope that helps you out if you are wondering how to use jQuery and ajax in your WordPress plugins. Please leave a comment if you have any questions. Thanks!</p>
]]></content:encoded>
			<wfw:commentRss>http://ryanpharis.com/2008/10/ajax-for-wordpress-plugins-using-jquery/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
