<?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>x + 3 &#187; Ajax</title>
	<atom:link href="http://xplus3.net/tag/ajax/feed/" rel="self" type="application/rss+xml" />
	<link>http://xplus3.net</link>
	<description></description>
	<lastBuildDate>Fri, 19 Aug 2011 01:05:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>Solr and JSONP</title>
		<link>http://xplus3.net/2010/09/21/solr-and-jsonp/</link>
		<comments>http://xplus3.net/2010/09/21/solr-and-jsonp/#comments</comments>
		<pubDate>Tue, 21 Sep 2010 12:04:26 +0000</pubDate>
		<dc:creator>Jonathan Brinley</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[JSONP]]></category>
		<category><![CDATA[Solr]]></category>

		<guid isPermaLink="false">http://xplus3.net/?p=337</guid>
		<description><![CDATA[Need to send an Ajax request to a Solr server that&#8217;s on a different domain? You will, of course, need to use JSONP instead of an ordinary JSON request, due to JavaScript&#8217;s cross-domain security restrictions. To get a properly padded response from Solr, add the json.wrf parameter to your query string, giving it the name of your callback function. In &#8230; <a href="http://xplus3.net/2010/09/21/solr-and-jsonp/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Need to send an Ajax request to a Solr server that&#8217;s on a different domain? You will, of course, need to use <a href="http://en.wikipedia.org/wiki/JSON#JSONP">JSONP</a> instead of an ordinary JSON request, due to JavaScript&#8217;s cross-domain security restrictions. To get a properly padded response from Solr, add the <code>json.wrf</code> parameter to your query string, giving it the name of your callback function. In jQuery:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="javascript"><pre class="de1">jQuery.<span class="me1">ajax</span><span class="br0">&#40;</span><span class="br0">&#123;</span>
  url<span class="sy0">:</span> mySolrUrl<span class="sy0">,</span>
  data<span class="sy0">:</span> myQueryParameters<span class="sy0">,</span>
  success<span class="sy0">:</span> mySuccessCallback<span class="sy0">,</span>
  dataType<span class="sy0">:</span> <span class="st0">'jsonp'</span><span class="sy0">,</span>
  jsonp<span class="sy0">:</span> <span class="st0">'json.wrf'</span>
<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div></div></div></div></div></div>


<p>Of course, for this to work the Solr server you&#8217;re accessing needs to be publicly accessible, which probably isn&#8217;t ideal for security.</p>
]]></content:encoded>
			<wfw:commentRss>http://xplus3.net/2010/09/21/solr-and-jsonp/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>jQuery and Ajax in WordPress Plugins &#8211; Public Pages</title>
		<link>http://xplus3.net/2008/10/27/jquery-and-ajax-in-wordpress-plugins-public-pages/</link>
		<comments>http://xplus3.net/2008/10/27/jquery-and-ajax-in-wordpress-plugins-public-pages/#comments</comments>
		<pubDate>Mon, 27 Oct 2008 17:57:23 +0000</pubDate>
		<dc:creator>Jonathan Brinley</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[plugins]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://xplus3.net/?p=108</guid>
		<description><![CDATA[My previous post teaches you how to use jQuery and Ajax for the administration pages in your WordPress plugins. To use them in your user-facing pages requires a few changes. We&#8217;ll use here a simlarly contrived example. Let&#8217;s say you use &#60;!--more--&#62; in your longer posts so they don&#8217;t fill up too much of your page. Normally, clicking the &#8220;Read &#8230; <a href="http://xplus3.net/2008/10/27/jquery-and-ajax-in-wordpress-plugins-public-pages/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>My previous post teaches you <a href="http://xplus3.net/2008/10/16/jquery-and-ajax-in-wordpress-plugins-administration-pages/">how to use jQuery and Ajax for the administration pages in your WordPress plugins</a>. To use them in your user-facing pages requires a few changes.</p>
<p>We&#8217;ll use here a simlarly contrived example. Let&#8217;s say you use <code>&lt;!--more--&gt;</code> in your longer posts so they don&#8217;t fill up too much of your page. Normally, clicking the &#8220;Read more&#8230;&#8221; (or whatever text you use) link takes the user to a separate page with the complete post. In our example, rather than sending the reader to a new page, we&#8217;ll make an Ajax request to get the rest of the post and insert it directly into the current page.<span id="more-108"></span></p>
<h3>Step 1: A Standard WordPress Template</h3>
<p>Somewhere in your template, you&#8217;ll find a section like:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1"><span class="sy0">&lt;</span>div <span class="kw2">class</span><span class="sy0">=</span><span class="st0">&quot;entry&quot;</span><span class="sy0">&gt;</span>
  <span class="sy0">&lt;</span> ?php the_content<span class="br0">&#40;</span><span class="st_h">'Read more...'</span><span class="br0">&#41;</span><span class="sy0">;</span> <span class="sy1">?&gt;</span>
<span class="sy0">&lt;/</span>div<span class="sy0">&gt;</span></pre></div></div></div></div></div></div></div>


<p>This displays the contents of your post, with the &#8220;Read more&#8230;&#8221; link at the end to read whatever was cut off by <code>&lt;!--more--&gt;</code>. Pretty simple.</p>
<h3>Step 2: Get the Full Post</h3>
<p>Now it&#8217;s time to start writing our plugin. We&#8217;ll call it &#8220;Post Expander&#8221;, so our plugin file will be called <code>post-expander.php</code>.</p>
<p>We need a function that will return just the contents of a post. For example:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1"><span class="kw2">function</span> post_expander_show_post <span class="br0">&#40;</span>  <span class="br0">&#41;</span> <span class="br0">&#123;</span>
  <span class="kw1">if</span> <span class="br0">&#40;</span> have_posts<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span class="kw1">while</span> <span class="br0">&#40;</span> have_posts<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span> <span class="br0">&#123;</span>
      the_post<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
      the_content<span class="br0">&#40;</span><span class="st_h">'Read more...'</span><span class="br0">&#41;</span><span class="sy0">;</span>
    <span class="br0">&#125;</span>
  <span class="br0">&#125;</span>
  <span class="kw3">die</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span></pre></div></div></div></div></div></div></div>


<p>This function will take advantage of the WordPress loop, printing out just the contents of the post before dying.</p>
<p>So how and when do we call this function? First, we use the <code>init</code> hook to see if the requested posted our special <code>post_expander</code> variable.</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1"><span class="kw2">function</span> post_expander_activate <span class="br0">&#40;</span> <span class="br0">&#41;</span> <span class="br0">&#123;</span>
  <span class="kw1">if</span> <span class="br0">&#40;</span> <span class="kw3">isset</span><span class="br0">&#40;</span> <span class="re0">$_POST</span><span class="br0">&#91;</span><span class="st0">&quot;post_expander&quot;</span><span class="br0">&#93;</span> <span class="br0">&#41;</span> <span class="br0">&#41;</span> <span class="br0">&#123;</span>
    add_action<span class="br0">&#40;</span> <span class="st_h">'wp'</span><span class="sy0">,</span> <span class="st_h">'post_expander_show_post'</span> <span class="br0">&#41;</span><span class="sy0">;</span>
  <span class="br0">&#125;</span>
<span class="br0">&#125;</span>
&nbsp;
add_action<span class="br0">&#40;</span><span class="st_h">'init'</span><span class="sy0">,</span> <span class="st_h">'post_expander_activate'</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div></div></div></div></div></div>


<p>If the variable is set, we attach <code>post_expander_show_post</code> to the <code>wp</code> hook, which runs after posts are loaded but before any templates are executed. Now, if a page is requested with the <code>post_expander</code> variable posted, this plugin will intercept the request.</p>
<h3>Step 3: Write the JavaScript Function</h3>
<p>So we need a JavaScript function to post that request and insert the full post into the page. We&#8217;ll put that code in <code>post-expander.js</code>.</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="javascript"><pre class="de1">jQuery<span class="br0">&#40;</span>document<span class="br0">&#41;</span>.<span class="me1">ready</span><span class="br0">&#40;</span> <span class="kw2">function</span><span class="br0">&#40;</span>$<span class="br0">&#41;</span> <span class="br0">&#123;</span>
  $<span class="br0">&#40;</span><span class="st0">&quot;.more-link&quot;</span><span class="br0">&#41;</span>.<span class="me1">click</span><span class="br0">&#40;</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span class="kw2">var</span> link <span class="sy0">=</span> $<span class="br0">&#40;</span><span class="kw1">this</span><span class="br0">&#41;</span><span class="sy0">;</span>
    $.<span class="me1">post</span><span class="br0">&#40;</span>link.<span class="me1">attr</span><span class="br0">&#40;</span><span class="st0">&quot;href&quot;</span><span class="br0">&#41;</span><span class="sy0">,</span> <span class="br0">&#123;</span>
        post_expander<span class="sy0">:</span> <span class="nu0">1</span>
      <span class="br0">&#125;</span><span class="sy0">,</span> <span class="kw2">function</span><span class="br0">&#40;</span>data<span class="br0">&#41;</span> <span class="br0">&#123;</span>
        link.<span class="me1">parents</span><span class="br0">&#40;</span><span class="st0">&quot;.entry&quot;</span><span class="br0">&#41;</span>.<span class="me1">html</span><span class="br0">&#40;</span>$<span class="br0">&#40;</span>data<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
      <span class="br0">&#125;</span>
    <span class="br0">&#41;</span><span class="sy0">;</span>
    <span class="kw1">return</span> <span class="kw2">false</span><span class="sy0">;</span>
  <span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div></div></div></div></div></div>


<p>Pretty simple JavaScript, that. (NB: <code>link.parents(".entry")</code> makes the assumption that the body of your post is in a div with the class <code>entry</code>.)</p>
<h3>Step 4: Load Your JavaScript into the Page</h3>
<p>Just a bit of housekeeping left, now. We need to load our JavaScript into the page, taking care to mention that it requires jQuery.</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1"><span class="kw2">function</span> post_expander_list_scripts <span class="br0">&#40;</span> <span class="br0">&#41;</span> <span class="br0">&#123;</span>
  wp_enqueue_script<span class="br0">&#40;</span> <span class="st0">&quot;post-expander&quot;</span><span class="sy0">,</span> path_join<span class="br0">&#40;</span>WP_PLUGIN_URL<span class="sy0">,</span> <span class="kw3">basename</span><span class="br0">&#40;</span> <span class="kw3">dirname</span><span class="br0">&#40;</span> <span class="kw4">__FILE__</span> <span class="br0">&#41;</span> <span class="br0">&#41;</span><span class="sy0">.</span><span class="st0">&quot;/post-expander.js&quot;</span><span class="br0">&#41;</span><span class="sy0">,</span> <span class="kw3">array</span><span class="br0">&#40;</span> <span class="st_h">'jquery'</span> <span class="br0">&#41;</span> <span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span>
&nbsp;
add_action<span class="br0">&#40;</span><span class="st_h">'wp_print_scripts'</span><span class="sy0">,</span> <span class="st_h">'post_expander_list_scripts'</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div></div></div></div></div></div>


<p>Once again, this plugin is not entirely practical, but you&#8217;re welcome to <a href='http://xplus3.net/wp-content/uploads/2008/10/post-expander.tgz'>download the full code</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://xplus3.net/2008/10/27/jquery-and-ajax-in-wordpress-plugins-public-pages/feed/</wfw:commentRss>
		<slash:comments>24</slash:comments>
		</item>
		<item>
		<title>jQuery and Ajax in WordPress Plugins &#8211; Administration Pages</title>
		<link>http://xplus3.net/2008/10/16/jquery-and-ajax-in-wordpress-plugins-administration-pages/</link>
		<comments>http://xplus3.net/2008/10/16/jquery-and-ajax-in-wordpress-plugins-administration-pages/#comments</comments>
		<pubDate>Thu, 16 Oct 2008 18:22:54 +0000</pubDate>
		<dc:creator>Jonathan Brinley</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[plugins]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://xplus3.net/?p=94</guid>
		<description><![CDATA[This is a quick overview of how to use jQuery and its Ajax functions in WordPress. To get the point across, I&#8217;ll use a simple and contrived example. We&#8217;ll have an admin screen with a list of categories. Clicking on the name of one of the categories will fetch a list of titles of posts in that category and display &#8230; <a href="http://xplus3.net/2008/10/16/jquery-and-ajax-in-wordpress-plugins-administration-pages/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>This is a quick overview of how to use jQuery and its Ajax functions in WordPress. To get the point across, I&#8217;ll use a simple and contrived example. We&#8217;ll have an admin screen with a list of categories. Clicking on the name of one of the categories will fetch a list of titles of posts in that category and display them as a sub-list of that category.<span id="more-94"></span></p>
<h3>Step 1: Build the Admin Page</h3>
<p>Zipping right by the details of how to add an admin page, here&#8217;s the code for adding our category list as a management page.</p>
<p>In <code>category-list.php</code>:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1">?php
<span class="coMULTI">/*
Plugin Name: Category List
Description: A list of categories.
Author: Jonathan Brinley
Author URI: http://xplus3.net/
*/</span>
<span class="kw2">function</span> category_list_add_management_page<span class="br0">&#40;</span>  <span class="br0">&#41;</span> <span class="br0">&#123;</span>
  <span class="kw1">if</span> <span class="br0">&#40;</span> <span class="kw3">function_exists</span><span class="br0">&#40;</span><span class="st_h">'add_management_page'</span><span class="br0">&#41;</span> <span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span class="re0">$page</span> <span class="sy0">=</span> add_management_page<span class="br0">&#40;</span> <span class="st_h">'Category List'</span><span class="sy0">,</span> <span class="st_h">'Category List'</span><span class="sy0">,</span> <span class="st_h">'manage_categories'</span><span class="sy0">,</span> <span class="st_h">'category_list'</span><span class="sy0">,</span> <span class="st_h">'category_list_admin'</span> <span class="br0">&#41;</span><span class="sy0">;</span>
  <span class="br0">&#125;</span>
<span class="br0">&#125;</span>
&nbsp;
<span class="kw2">function</span> category_list_admin<span class="br0">&#40;</span>  <span class="br0">&#41;</span> <span class="br0">&#123;</span>
  <span class="re0">$categories</span> <span class="sy0">=</span> get_categories<span class="br0">&#40;</span> <span class="st_h">'orderby=name&amp;hierarchical=0&amp;hide_empty=0'</span> <span class="br0">&#41;</span><span class="sy0">;</span>
  <span class="kw1">include_once</span><span class="br0">&#40;</span><span class="st_h">'category_list_view.php'</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span>
&nbsp;
add_action<span class="br0">&#40;</span><span class="st_h">'admin_menu'</span><span class="sy0">,</span> <span class="st_h">'category_list_add_management_page'</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div></div></div></div></div></div>


<p>In <code>category-list-view.php</code>:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1">&lt;div class=&quot;wrap&quot;&gt;
  &lt;h2&gt;Category List&lt;/h2&gt;
  &lt;ul id=&quot;category_list&quot;&gt;
  &lt; ?php foreach ( $categories as $cat ): ?&gt;
    &lt;li id=&quot;cat-<span class="kw2">&lt;?php</span> <span class="kw1">echo</span> <span class="re0">$cat</span><span class="sy0">-&gt;</span><span class="me1">cat_ID</span><span class="sy0">;</span> <span class="sy1">?&gt;</span>&quot; class=&quot;category-name&quot;&gt;&lt;a href=&quot;#&quot;&gt;&lt; ?php echo $cat-&gt;cat_name; ?&gt;&lt;/a&gt;&lt;/li&gt;
  &lt; ?php endforeach; ?&gt;
  &lt;/ul&gt;
&lt;/div&gt;</pre></div></div></div></div></div></div></div>


<h3>Step 2: Add the Ajax Response</h3>
<p>As mentioned above, clicking on a category will fetch a list of post titles. So we need a function that builds that list. We&#8217;ll add this function to <code>category_list.php</code>:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1"><span class="kw2">function</span> category_list_posts<span class="br0">&#40;</span>  <span class="br0">&#41;</span> <span class="br0">&#123;</span>
  <span class="re0">$catID</span> <span class="sy0">=</span> <span class="kw3">isset</span><span class="br0">&#40;</span><span class="re0">$_POST</span><span class="br0">&#91;</span><span class="st_h">'catID'</span><span class="br0">&#93;</span><span class="br0">&#41;</span>?<span class="re0">$_POST</span><span class="br0">&#91;</span><span class="st_h">'catID'</span><span class="br0">&#93;</span><span class="sy0">:</span><span class="kw4">null</span><span class="sy0">;</span>
  <span class="kw1">if</span> <span class="br0">&#40;</span> <span class="re0">$catID</span> <span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span class="re0">$posts</span> <span class="sy0">=</span> get_posts<span class="br0">&#40;</span> <span class="st0">&quot;numberposts=-1&amp;post_status=publish,pending,future,private,draft&amp;category=<span class="es4">$catID</span>&quot;</span> <span class="br0">&#41;</span><span class="sy0">;</span>
    <span class="kw1">include_once</span><span class="br0">&#40;</span><span class="st_h">'posts_list_view.php'</span><span class="br0">&#41;</span><span class="sy0">;</span>
  <span class="br0">&#125;</span>
  <span class="kw3">die</span><span class="sy0">;</span>
<span class="br0">&#125;</span></pre></div></div></div></div></div></div></div>


<p>And, of course, we need <code>posts_list_view.php</code>:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1">&lt;ul class=&quot;posts_list&quot;&gt;
&lt; ?php foreach ( $posts as $post ): ?&gt;
  &lt;li class=&quot;post-name post-<span class="kw2">&lt;?php</span> <span class="kw1">echo</span> <span class="re0">$post</span><span class="sy0">-&gt;</span><span class="me1">ID</span><span class="sy0">;</span> <span class="sy1">?&gt;</span>&quot;&gt;&lt; ?php echo $post-&gt;post_title; ?&gt;&lt;/li&gt;
&lt; ?php endforeach; ?&gt;
&lt;/ul&gt;</pre></div></div></div></div></div></div></div>


<p>We also need to tell WordPress when to call this function, using another action hook:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1">add_action<span class="br0">&#40;</span><span class="st_h">'wp_ajax_category_list_posts'</span><span class="sy0">,</span> <span class="st_h">'category_list_posts'</span> <span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div></div></div></div></div></div>


<p>Whenever you post a request to <code>wp-admin/admin-ajax.php</code> with the <code>action</code> parameter of <code>category_list_posts</code> (this being everything after &#8220;wp_ajax_&#8221; in the action hook), WordPress will call <code>category_list_posts()</code>, the function named in the second parameter of <code>add_action</code>.</p>
<p>Note the call to <code>die</code> at the end of <code>category_list_posts()</code>. When responding to Ajax requests, WordPress will end the response by calling <code>die('0')</code>. If you don&#8217;t want that &#8220;0&#8243; to appear at the end of your response, you have to call <code>die</code> before WordPress gets a chance.</p>
<h3>Step 3: Write Your JavaScript</h3>
<p>You have your list of categories, and your function that will return the list of posts in a category. Now you need some JavaScript to put them together.</p>
<p>Remember that the Ajax request needs to be sent to <code>wp-admin/admin-ajax.php</code>. Without knowing what directory your plugin will be installed in, nor the full path to <code>wp-admin</code>, you need to provide the URL to your JavaScript somehow. The easiest way I see it to change the <code>href</code> attribute of your links to that URL. So in <code>category_list_view.php</code>, well replace the opening anchor tag with:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1">&lt;a href=&quot;<span class="kw2">&lt;?php</span> bloginfo<span class="br0">&#40;</span><span class="st_h">'wpurl'</span><span class="br0">&#41;</span><span class="sy0">;</span> <span class="sy1">?&gt;</span>/wp-admin/admin-ajax.php&quot;&gt;&lt;/a&gt;</pre></div></div></div></div></div></div></div>


<p>With that, we can create a simple JavaScript file (we&#8217;ll call it <code>category_list.js</code>):</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="javascript"><pre class="de1">jQuery<span class="br0">&#40;</span>document<span class="br0">&#41;</span>.<span class="me1">ready</span><span class="br0">&#40;</span> <span class="kw2">function</span><span class="br0">&#40;</span>$<span class="br0">&#41;</span> <span class="br0">&#123;</span>
  $<span class="br0">&#40;</span><span class="st0">&quot;li.category-name a&quot;</span><span class="br0">&#41;</span>.<span class="me1">click</span><span class="br0">&#40;</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span class="kw2">var</span> li <span class="sy0">=</span> $<span class="br0">&#40;</span><span class="kw1">this</span><span class="br0">&#41;</span>.<span class="me1">parent</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
    <span class="coMULTI">/* only fetch results once */</span>
&nbsp;
    $<span class="br0">&#40;</span><span class="kw1">this</span><span class="br0">&#41;</span>.<span class="me1">unbind</span><span class="br0">&#40;</span><span class="st0">'click'</span><span class="br0">&#41;</span>.<span class="me1">bind</span><span class="br0">&#40;</span><span class="st0">'click'</span><span class="sy0">,</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span><span class="kw1">return</span> <span class="kw2">false</span><span class="sy0">;</span><span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
    $.<span class="me1">post</span><span class="br0">&#40;</span>$<span class="br0">&#40;</span><span class="kw1">this</span><span class="br0">&#41;</span>.<span class="me1">attr</span><span class="br0">&#40;</span><span class="st0">&quot;href&quot;</span><span class="br0">&#41;</span><span class="sy0">,</span> <span class="br0">&#123;</span>
        action<span class="sy0">:</span> <span class="st0">&quot;category_list_posts&quot;</span><span class="sy0">,</span>
        catID<span class="sy0">:</span> li.<span class="me1">attr</span><span class="br0">&#40;</span><span class="st0">&quot;id&quot;</span><span class="br0">&#41;</span>.<span class="me1">substr</span><span class="br0">&#40;</span><span class="nu0">4</span><span class="br0">&#41;</span>
      <span class="br0">&#125;</span><span class="sy0">,</span> <span class="kw2">function</span><span class="br0">&#40;</span>data<span class="br0">&#41;</span> <span class="br0">&#123;</span>
        li.<span class="me1">append</span><span class="br0">&#40;</span>$<span class="br0">&#40;</span>data<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
      <span class="br0">&#125;</span>
    <span class="br0">&#41;</span><span class="sy0">;</span>
    <span class="kw1">return</span> <span class="kw2">false</span><span class="sy0">;</span>
  <span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div></div></div></div></div></div>


<p>This posts data to the given URL, and appends the response to the end of the <code>li</code>.</p>
<p>Now we just need to tell WordPress to include the JavaScript when we&#8217;re on the Category List page. Back up in <code>category_list_add_management_page</code>, we&#8217;ll make use of that <code>$page</code> variable we set up earlier. Add a line after that:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1">add_action<span class="br0">&#40;</span><span class="st0">&quot;admin_print_scripts-<span class="es4">$page</span>&quot;</span><span class="sy0">,</span> <span class="st_h">'category_list_scripts'</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div></div></div></div></div></div>


<p>And add the <code>category_list_scripts</code> function to specify which scripts to add:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1"><span class="kw2">function</span> category_list_scripts<span class="br0">&#40;</span>  <span class="br0">&#41;</span> <span class="br0">&#123;</span>
  wp_enqueue_script<span class="br0">&#40;</span> <span class="st0">&quot;category-list&quot;</span><span class="sy0">,</span> path_join<span class="br0">&#40;</span>WP_PLUGIN_URL<span class="sy0">,</span> <span class="kw3">basename</span><span class="br0">&#40;</span> <span class="kw3">dirname</span><span class="br0">&#40;</span> <span class="kw4">__FILE__</span> <span class="br0">&#41;</span> <span class="br0">&#41;</span><span class="sy0">.</span><span class="st0">&quot;/category_list.js&quot;</span><span class="br0">&#41;</span><span class="sy0">,</span> <span class="kw3">array</span><span class="br0">&#40;</span> <span class="st_h">'jquery'</span> <span class="br0">&#41;</span> <span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span></pre></div></div></div></div></div></div></div>


<p>This plugin doesn&#8217;t really do anything useful, but if you want to see the full source code, you&#8217;re welcome to <a href='http://xplus3.net/wp-content/uploads/2008/10/category-list.tgz'>download it</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://xplus3.net/2008/10/16/jquery-and-ajax-in-wordpress-plugins-administration-pages/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>

