<?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/"
	
	xmlns:georss="http://www.georss.org/georss"
	xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
	>

<channel>
	<title>ajax Archives - rweber.net</title>
	<atom:link href="https://www.rweber.net/tag/ajax/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.rweber.net/tag/ajax/</link>
	<description>trying to be a mile wide AND a mile deep</description>
	<lastBuildDate>Sun, 09 Dec 2018 13:51:20 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
<site xmlns="com-wordpress:feed-additions:1">37896774</site>	<item>
		<title>AJAX in WP: a simple example</title>
		<link>https://www.rweber.net/javascript/ajax-in-wp-a-simple-example/</link>
					<comments>https://www.rweber.net/javascript/ajax-in-wp-a-simple-example/#respond</comments>
		
		<dc:creator><![CDATA[Rebecca]]></dc:creator>
		<pubDate>Mon, 07 Mar 2016 13:00:02 +0000</pubDate>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[PHP and MySQL]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[WordPress]]></category>
		<guid isPermaLink="false">http://www.rweber.net/?p=39568</guid>

					<description><![CDATA[<div><img width="300" height="300" src="https://www.rweber.net/wp-content/uploads/2016/03/two-way-traffic-148887_640-300x300.png" class="attachment-medium size-medium wp-post-image" alt="two-way traffic sign" style="float:left; margin-right:16px; margin-bottom:16px;" decoding="async" fetchpriority="high" srcset="https://www.rweber.net/wp-content/uploads/2016/03/two-way-traffic-148887_640-300x300.png 300w, https://www.rweber.net/wp-content/uploads/2016/03/two-way-traffic-148887_640-150x150.png 150w, https://www.rweber.net/wp-content/uploads/2016/03/two-way-traffic-148887_640.png 640w" sizes="(max-width: 300px) 100vw, 300px" /></div>
<p>A simple but nontrivial example of all the steps needed to call on admin-ajax.php in WordPress.</p>
<p>The post <a href="https://www.rweber.net/javascript/ajax-in-wp-a-simple-example/">AJAX in WP: a simple example</a> appeared first on <a href="https://www.rweber.net">rweber.net</a>.</p>
]]></description>
										<content:encoded><![CDATA[<div><img width="300" height="300" src="https://www.rweber.net/wp-content/uploads/2016/03/two-way-traffic-148887_640-300x300.png" class="attachment-medium size-medium wp-post-image" alt="two-way traffic sign" style="float:left; margin-right:16px; margin-bottom:16px;" decoding="async" srcset="https://www.rweber.net/wp-content/uploads/2016/03/two-way-traffic-148887_640-300x300.png 300w, https://www.rweber.net/wp-content/uploads/2016/03/two-way-traffic-148887_640-150x150.png 150w, https://www.rweber.net/wp-content/uploads/2016/03/two-way-traffic-148887_640.png 640w" sizes="(max-width: 300px) 100vw, 300px" /></div><p>AJAX in WordPress &#8211; not the kind where you load all or part of a page, but the kind where you use admin-ajax to execute an action &#8211; was fairly opaque for me when I first tried to implement it. Granted, that was as part of the Customizer, not the easiest thing in the world to come to grips with. However, it seems to me I had to piece together multiple tutorials to get a complete picture, so I thought I&#8217;d put out a simple example that still addresses all of the components.</p>
<p><strong>The main idea:</strong> admin-ajax.php (<a href="https://core.trac.wordpress.org/browser/tags/4.4.2/src/wp-admin/admin-ajax.php">view code on Trac</a>) is a file that executes actions. Those actions can do anything, but generally they are either saving information or displaying content. I&#8217;ll stick to displaying content.</p>
<p>AJAX being what it is, setting it up requires both PHP and JavaScript files. I think the easiest way to explain it is probably to walk through an example. This is code for a plugin that provides the ability to pop the primary WordPress menu up in a lightbox (in this case <a href="http://www.jacklmoore.com/colorbox/">Colorbox</a>) from any element to which you give the id &#8220;menu-popper&#8221;. It relies on jQuery. The plugin is simply called Colorbox Menu, and in fact if you <a href="http://www.jacklmoore.com/colorbox/">download jquery.colorbox-min.js</a> and stick it in a folder with the following two files, you can use it (though you&#8217;ll want some CSS too).</p>
<p>JavaScript (colorbox-menu.js):</p>
<p><script src="//pastebin.com/embed_js/T38DYSwc"></script></p>
<p>PHP (colorbox-menu.php):</p>
<p><script src="//pastebin.com/embed_js/YUwh2F5y"></script></p>
<p>So, what is going on here? The script is executing AJAX in the usual jQuery way, with some specifics required by WordPress and/or best coding practices. There are three lines to highlight:</p>
<ul>
<li><code>url: colorboxMenu.ajaxurl</code><br />
This line tells JavaScript where it is making its request, which is the full URL for admin-ajax.php (likely to be the same relative location in most WP installs, but certainly not all). This is an object property whose value will be the actual URL. The value will be provided to the script when the script is enqueued &#8211; more when we look at the PHP file. For now just know there are numerous tutorials that tell you the address of admin-ajax is automatically filled in if you put simply <code>url: ajaxurl</code>; this is true on the admin side, but not when the call is made from the front end.</li>
<li><code>action: 'colorbox_menu_display_main_nav',</code><br />
The only data we are sending in this example is the name of the action we want executed. You will see in the PHP discussion that it is not actually the name the action is registered under, and need not be the name of the PHP function that gets executed.</li>
<li><code>$.colorbox( { html:result, } );</code><br />
Your action will send back text or html, just as a string. Colorbox lets you tell it the content of the lightbox when it&#8217;s called, which is what we are doing here. You could also insert this into an HTML element with, e.g., <code>.append()</code> or <code>.html()</code>.</li>
</ul>
<p>The PHP file is doing two things: enqueuing and localizing the script, and teaching admin-ajax how to react to the AJAX request. Enqueuing is as usual; you just have to do it before localizing. The remaining noteworthy pieces:</p>
<ul>
<li><code>wp_localize_script( 'colorbox-menu', 'colorboxMenu', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );</code><br />
Script localization is the process of swapping out variables for data known at load time, such as the URL of the admin-ajax file. This tells WordPress that within the script enqueued under the handle &#8220;colorbox-menu&#8221; it needs to replace properties of the object <code>colorboxMenu</code> with the values associated to them in the array given as the third parameter. You need to pass the URL in this way, but you could pass other information as well, as long as it is known/determined by the time of script loading.</li>
<li><code>function colorbox_menu_load_main_nav()</code><br />
This is the function that will ultimately be called by the user&#8217;s click. It needs to do two things: echo any return information, and terminate itself. <code>wp_nav_menu</code> echos the menu code already so it can be used bare. Anything that needs to be preceded by an echo command in a template file (e.g. <code>home_url</code>) needs to be preceded by an echo command in this function, and you can simply echo strings as well. I was pleased to discover that echoing a function that would typically be called from within a page and simply close out PHP to print HTML onto the page (such as get_template_part) works perfectly well also.</li>
<li><code>add_action( 'wp_ajax_nopriv_colorbox_menu_display_main_nav', 'colorbox_menu_load_main_nav' );<br />
add_action( 'wp_ajax_colorbox_menu_display_main_nav', 'colorbox_menu_load_main_nav' );</code><br />
Here we have something seemingly odd. If you look at the admin-ajax.php code, it tacks <code>wp_ajax_nopriv_</code> or <code>wp_ajax_</code> on to the front of whatever action it is asked to execute, so when you add your action you need to put those prefixes on to whatever action name you sent in your AJAX data parameter. The second input is the name of the function the action calls. This may seem cumbersome, but it allows you to react differently to AJAX calls depending on whether the person who clicked is logged in to the site (<code>wp_ajax_</code>) or not (<code>wp_ajax_nopriv_</code>).</li>
</ul>
<p>There you have it: what information to include, how to get it where it needs to be, and how to name the pieces.</p>
<p>The post <a href="https://www.rweber.net/javascript/ajax-in-wp-a-simple-example/">AJAX in WP: a simple example</a> appeared first on <a href="https://www.rweber.net">rweber.net</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.rweber.net/javascript/ajax-in-wp-a-simple-example/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">39568</post-id>	</item>
		<item>
		<title>Gilding the Gallery 3: Ajaxifying the filterable gallery</title>
		<link>https://www.rweber.net/javascript/gilding-the-gallery-3-ajaxifying-the-filterable-gallery/</link>
					<comments>https://www.rweber.net/javascript/gilding-the-gallery-3-ajaxifying-the-filterable-gallery/#respond</comments>
		
		<dc:creator><![CDATA[Rebecca]]></dc:creator>
		<pubDate>Mon, 20 Apr 2015 12:00:21 +0000</pubDate>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[PHP and MySQL]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[Fancy Fifteen]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[portfolio]]></category>
		<category><![CDATA[WordPress gallery]]></category>
		<guid isPermaLink="false">http://www.rweber.net/?p=38038</guid>

					<description><![CDATA[<div><img width="300" height="243" src="https://www.rweber.net/wp-content/uploads/2015/04/galleryscreenshot2-300x243.png" class="attachment-medium size-medium wp-post-image" alt="" style="float:left; margin-right:16px; margin-bottom:16px;" decoding="async" srcset="https://www.rweber.net/wp-content/uploads/2015/04/galleryscreenshot2-300x243.png 300w, https://www.rweber.net/wp-content/uploads/2015/04/galleryscreenshot2.png 749w" sizes="(max-width: 300px) 100vw, 300px" /></div>
<p>Last time we set up the PHP (slash HTML, slash CSS) to dynamically generate Media Library Assistant gallery shortcodes and an album menu. Everything works without any JavaScript. Today we're going to add JavaScript, with jQuery, to do three things.</p>
<ol>
<li>Hide the album menu under a button, a la a mobile menu.</li>
<li>Use AJAX to reload only the portion of the page containing the gallery.</li>
<li>Push each new URL into the browser history, so that the back button works and individual albums can be bookmarked.</li>
</ol>
<p>The post <a href="https://www.rweber.net/javascript/gilding-the-gallery-3-ajaxifying-the-filterable-gallery/">Gilding the Gallery 3: Ajaxifying the filterable gallery</a> appeared first on <a href="https://www.rweber.net">rweber.net</a>.</p>
]]></description>
										<content:encoded><![CDATA[<div><img width="300" height="243" src="https://www.rweber.net/wp-content/uploads/2015/04/galleryscreenshot2-300x243.png" class="attachment-medium size-medium wp-post-image" alt="" style="float:left; margin-right:16px; margin-bottom:16px;" decoding="async" loading="lazy" srcset="https://www.rweber.net/wp-content/uploads/2015/04/galleryscreenshot2-300x243.png 300w, https://www.rweber.net/wp-content/uploads/2015/04/galleryscreenshot2.png 749w" sizes="auto, (max-width: 300px) 100vw, 300px" /></div><p><a href="https://www.rweber.net/web-development/php/gilding-the-gallery-2-mla-front-end-filtration">Last time</a> we set up the PHP (slash HTML, slash CSS) to dynamically generate Media Library Assistant gallery shortcodes and an album menu. Everything works without any JavaScript. Today we&#8217;re going to add JavaScript, with jQuery, to do three things.</p>
<ol>
<li>Hide the album menu under a button, a la a mobile menu. This will prevent the problem of a menu that&#8217;s so large the gallery is four pages down.</li>
<li>Use AJAX to reload only the portion of the page containing the gallery. This will make for a smoother experience.</li>
<li>Push each new URL into the browser history, so that the back button works and individual albums can be bookmarked. It&#8217;s always a source of an annoyed &#8220;really?&#8221; when I try to go back to a page I bookmarked and find that the address leads to only the most general version of the page, not where I actually was.</li>
</ol>
<p><b>Edit:</b> Since I have made so many additions to Twenty Fifteen, I thought it would be useful to assemble them into a single child theme. You can see the <a href="https://github.com/ReveWeber/fancyfifteen">Fancy Fifteen code on GitHub</a>, or look at <a href="https://www.rweber.net/tag/fancyfifteen/">all posts here tagged fancyfifteen</a>.</p>
<p><center><a href="https://www.rweber.net/wp-content/uploads/2015/04/galleryscreenshot.png"><img loading="lazy" decoding="async" src="https://www.rweber.net/wp-content/uploads/2015/04/galleryscreenshot-300x243.png" alt="screenshot of finished gallery" width="300" height="243" class="alignnone size-medium wp-image-38269" srcset="https://www.rweber.net/wp-content/uploads/2015/04/galleryscreenshot-300x243.png 300w, https://www.rweber.net/wp-content/uploads/2015/04/galleryscreenshot.png 749w" sizes="auto, (max-width: 300px) 100vw, 300px" /></a> <a href="https://www.rweber.net/wp-content/uploads/2015/04/galleryscreenshot2.png"><img loading="lazy" decoding="async" src="https://www.rweber.net/wp-content/uploads/2015/04/galleryscreenshot2-300x243.png" alt="screenshot of finished gallery" width="300" height="243" class="alignnone size-medium wp-image-38271" srcset="https://www.rweber.net/wp-content/uploads/2015/04/galleryscreenshot2-300x243.png 300w, https://www.rweber.net/wp-content/uploads/2015/04/galleryscreenshot2.png 749w" sizes="auto, (max-width: 300px) 100vw, 300px" /></a></center></p>
<p>Here&#8217;s a pair of screenshots. You can&#8217;t tell in this because I didn&#8217;t make a ton of categories, but the menu will also drop over the photos. Again, at least for the time being, you can see and interact with this <a href="http://rweber.net/test_site/wordpress/gallery/">on my testing site</a>.</p>
<p>All three steps are actually handled in one short jQuery-reliant JavaScript file.</p>
<p>Call it gallery-menu.js, put the following into it, and save it in your child theme folder:</p>
<p><script src="https://pastebin.com/embed_js.php?i=Ed76FGUa"></script></p>
<p>Line 2 is sort of pre-Step-1. It&#8217;s for graceful degradation &#8211; it adds a class so we can style the menu and gallery separately depending on whether JavaScript is enabled or not.<br />
Lines 3-6 control Step 1. They hide the menu to begin with and activate the button to show and hide it.</p>
<p>The rest is Steps 2 and 3. If you click a link within the menu, JS swoops in and stops the browser from following it (line 9). Then we construct the relevant piece of the desired URL from the link, using the data-slug attribute we added into the PHP last time, and furthermore tack on the specific div that holds the images (lines 10-11). Line 12 does the AJAX, Line 13 re-hides the menu as it would be were the page reloaded, and Line 14 pushes the new URL into the browser history.</p>
<p>And this is why hearts fly out of my eyes sometimes when I work with jQuery.</p>
<p><b>Edit 3 May:</b> To get my lightbox plugin to apply to this gallery I had use JS to add a rel attribute to each link. The AJAX &#8211; or maybe simply do_shortcode &#8211; seemed to prevent the automatic application of the lightbox to the image links (which wasn&#8217;t the case with the <a href="https://www.rweber.net/web-development/css/gilding-the-gallery-mla-flickity/">Flickity slideshow</a>). What you need will depend on what plugin you use, of course, but for WP Lightbox 2 I had to add <code>$( '#current-album a' ).attr( 'rel', 'lightbox[album]' );</code> at line 7 and as a post-AJAX function to the .load call (see my <a href="https://www.rweber.net/web-development/javascript/things-ive-relearned-recently/">notes on .load syntax</a>).</p>
<h2>Are we there yet?</h2>
<p>Of course not. We have bonus steps to this part of the program as well.</p>
<p>Tack this onto the end of your functions.php file:</p>
<p><script src="https://pastebin.com/embed_js.php?i=wiDHVLXc"></script></p>
<p>Insert this CSS just before the media queries in style.css:</p>
<p><iframe src="https://pastebin.com/embed_iframe.php?i=3zCz2PzY" style="height:305px;border:none;width:100%"></iframe></p>
<p>And finally, insert this CSS inside the max-width: 43.75em media query (not the portrait one):</p>
<p><script src="https://pastebin.com/embed_js.php?i=50PtnmKC"></script></p>
<p>The styling could be way better, so play with it till you lose your mind.</p>
<p>The post <a href="https://www.rweber.net/javascript/gilding-the-gallery-3-ajaxifying-the-filterable-gallery/">Gilding the Gallery 3: Ajaxifying the filterable gallery</a> appeared first on <a href="https://www.rweber.net">rweber.net</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.rweber.net/javascript/gilding-the-gallery-3-ajaxifying-the-filterable-gallery/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">38038</post-id>	</item>
		<item>
		<title>Things I&#8217;ve (re)learned recently</title>
		<link>https://www.rweber.net/developer-toolbox/things-ive-relearned-recently/</link>
					<comments>https://www.rweber.net/developer-toolbox/things-ive-relearned-recently/#respond</comments>
		
		<dc:creator><![CDATA[Rebecca]]></dc:creator>
		<pubDate>Mon, 06 Apr 2015 12:00:47 +0000</pubDate>
				<category><![CDATA[Developer Toolbox]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[WordPress]]></category>
		<guid isPermaLink="false">http://www.rweber.net/?p=38030</guid>

					<description><![CDATA[<div><img width="300" height="225" src="https://www.rweber.net/wp-content/uploads/2015/04/John_Steuart_Curry_-_Ajax_-_Smithsonian-300x225.jpg" class="attachment-medium size-medium wp-post-image" alt="painting by John Steuart Curry titled Ajax" style="float:left; margin-right:16px; margin-bottom:16px;" decoding="async" loading="lazy" srcset="https://www.rweber.net/wp-content/uploads/2015/04/John_Steuart_Curry_-_Ajax_-_Smithsonian-300x225.jpg 300w, https://www.rweber.net/wp-content/uploads/2015/04/John_Steuart_Curry_-_Ajax_-_Smithsonian-768x575.jpg 768w, https://www.rweber.net/wp-content/uploads/2015/04/John_Steuart_Curry_-_Ajax_-_Smithsonian.jpg 1024w, https://www.rweber.net/wp-content/uploads/2015/04/John_Steuart_Curry_-_Ajax_-_Smithsonian-150x112.jpg 150w" sizes="auto, (max-width: 300px) 100vw, 300px" /></div>
<p>Miscellaneous little things that came up while I was working on the Flickety/MLA filterable gallery. The plurality are about jQuery's AJAX command $.load().</p>
<p>The post <a href="https://www.rweber.net/developer-toolbox/things-ive-relearned-recently/">Things I&#8217;ve (re)learned recently</a> appeared first on <a href="https://www.rweber.net">rweber.net</a>.</p>
]]></description>
										<content:encoded><![CDATA[<div><img width="300" height="225" src="https://www.rweber.net/wp-content/uploads/2015/04/John_Steuart_Curry_-_Ajax_-_Smithsonian-300x225.jpg" class="attachment-medium size-medium wp-post-image" alt="painting by John Steuart Curry titled Ajax" style="float:left; margin-right:16px; margin-bottom:16px;" decoding="async" loading="lazy" srcset="https://www.rweber.net/wp-content/uploads/2015/04/John_Steuart_Curry_-_Ajax_-_Smithsonian-300x225.jpg 300w, https://www.rweber.net/wp-content/uploads/2015/04/John_Steuart_Curry_-_Ajax_-_Smithsonian-768x575.jpg 768w, https://www.rweber.net/wp-content/uploads/2015/04/John_Steuart_Curry_-_Ajax_-_Smithsonian.jpg 1024w, https://www.rweber.net/wp-content/uploads/2015/04/John_Steuart_Curry_-_Ajax_-_Smithsonian-150x112.jpg 150w" sizes="auto, (max-width: 300px) 100vw, 300px" /></div><p>Working on the filterable gallery I mentioned in the <a href="https://www.rweber.net/web-development/css/gilding-the-gallery-mla-flickity/">Flickity/MLA carousel post</a>, there were a lot of little things I had to look up or work out by trial and error, some of which were ultimately quite simple but took a while to figure out. I want to lay them out for future reference, my own and potentially that of readers of the (forthcoming) post(s) about the gallery itself.</p>
<ul>
<li>If something that&#8217;s supposed to be clickable only works on part of its area, such as a div where only the bottom half is clickable, check for overhang. You can&#8217;t click through another div even if it&#8217;s empty.</li>
<li>String concatenation: . for PHP, + for JavaScript.</li>
<li>$.load puts the second thing into the first thing: $( &#8216;#wrapper&#8217; ).load( &#8216;source-page.php #contents&#8217; ), not $( &#8216;#contents&#8217; ).load( &#8216;source-page.php #contents&#8217; ). The latter will result in two divs with the id &#8216;contents&#8217;. This of course means you have to build your page with a wrapper div.</li>
<li>$.load seems to require that the function to run after loading completes be an anonymous one. Putting in a named function doesn&#8217;t necessarily work (I found sometimes it did and sometimes it didn&#8217;t). That is, not $.load( &#8216;source.php&#8217;, setup() ); but instead $.load( &#8216;source.php&#8217;, function() { setup(); });</li>
<li>If all you want is to add or change a query string (that is, &#8216;?attr=value&#8217;) on the current URL, $.load() needs only the query string itself. If you give it the page slug also, WordPress will show you the correct page, but the slug will be doubled in the URL. The same goes for window.history.pushState();</li>
<li>Using WordPress&#8217;s get_terms to access a taxonomy nets you an array of objects. The PHP syntax to access pieces of an array is completely different than the syntax used to access pieces of an object. Remember that and things will work better.</li>
<li>You can add whatever information you want to an anchor element by adding a data attribute; syntax is <code>&lt;a href="..." data-slug="..."></code>, where &#8220;slug&#8221; would be whatever the kind of data is in your application.</li>
</ul>
<p><small>John Steuart Curry&#8217;s painting <em>Ajax</em> via <a href="https://en.wikipedia.org/wiki/John_Steuart_Curry#/media/File:John_Steuart_Curry_-_Ajax_-_Smithsonian.jpg">Wikimedia Commons</a>.</small></p>
<p>The post <a href="https://www.rweber.net/developer-toolbox/things-ive-relearned-recently/">Things I&#8217;ve (re)learned recently</a> appeared first on <a href="https://www.rweber.net">rweber.net</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.rweber.net/developer-toolbox/things-ive-relearned-recently/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">38030</post-id>	</item>
	</channel>
</rss>
