<?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>bavotasan.com &#187; Function</title>
	<atom:link href="http://bavotasan.com/tag/function/feed/" rel="self" type="application/rss+xml" />
	<link>http://bavotasan.com</link>
	<description>by c.bavota</description>
	<lastBuildDate>Tue, 07 Feb 2012 15:42:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Run a Shortcode Anywhere in WordPress</title>
		<link>http://bavotasan.com/2011/run-a-shortcode-anywhere-in-wordpress/</link>
		<comments>http://bavotasan.com/2011/run-a-shortcode-anywhere-in-wordpress/#comments</comments>
		<pubDate>Thu, 07 Jul 2011 19:12:11 +0000</pubDate>
		<dc:creator>c.bavota</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Function]]></category>
		<category><![CDATA[shortcode]]></category>
		<category><![CDATA[shortcode api]]></category>
		<category><![CDATA[the loop]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://bavotasan.com/?p=3558</guid>
		<description><![CDATA[WordPress shortcodes are macro codes that allow you to perform certain actions within the post content. By default they only display on single post pages within the post content. The most commonly used one is probably the gallery shortcode which looks like this: &#160; Including that in your post content will display the post&#8217;s gallery [...]]]></description>
			<content:encoded><![CDATA[<p>WordPress shortcodes are macro codes that allow you to perform certain actions within the post content. By default they only display on single post pages within the post content. The most commonly used one is probably the gallery shortcode which looks like this:</p>

<div class="wp_syntax"><table border='0' cellpadding='0' cellspacing='0'><tr><td><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009900;">&#91;</span>gallery<span style="color: #009900;">&#93;</span></pre></div></td></tr></table></div>

<p>Including that in your post content will display the post&#8217;s gallery on the front end. But what if you also wanted to display your shortcode on a category page, or on any page template for that matter? That&#8217;s pretty simple thanks to an awesome function called <code>get_shortcode_regex</code>.</p>
<p>Using <code>get_shortcode_regex</code> we can easily put together a code snippet that will check your post content for any specific shortcode:</p>

<div class="wp_syntax"><table border='0' cellpadding='0' cellspacing='0'><tr><td><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$pattern</span> <span style="color: #339933;">=</span> get_shortcode_regex<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/'</span><span style="color: #339933;">.</span><span style="color: #000088;">$pattern</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'/s'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">post_content</span><span style="color: #339933;">,</span> <span style="color: #000088;">$matches</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$matches</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'the_shortcode_name'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #666666; font-style: italic;">// do something</span>
<span style="color: #009900;">&#125;</span></pre></div></td></tr></table></div>

<p>If you include the above function within the WordPress loop on a category page, it will search through the post content for a shortcode called &#8220;the_shortcode_name&#8221;. If it finds it, it will store the shortcode in the <code>$matches</code> variable. Now you can actually run the shortcode by making one small modification:</p>

<div class="wp_syntax"><table border='0' cellpadding='0' cellspacing='0'><tr><td><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$pattern</span> <span style="color: #339933;">=</span> get_shortcode_regex<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/'</span><span style="color: #339933;">.</span><span style="color: #000088;">$pattern</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'/s'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">post_content</span><span style="color: #339933;">,</span> <span style="color: #000088;">$matches</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$matches</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'the_shortcode_name'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #000088;">$shortcode</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
   <span style="color: #b1b100;">echo</span> do_shortcode<span style="color: #009900;">&#40;</span><span style="color: #000088;">$shortcode</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></td></tr></table></div>

<p>That will find the shortcode (if it&#8217;s included in the post content) and it will run it. Include that within the WordPress loop on any page template and you can display the shortcode anywhere you want.</p>
]]></content:encoded>
			<wfw:commentRss>http://bavotasan.com/2011/run-a-shortcode-anywhere-in-wordpress/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>is_child() Conditional Function for WordPress</title>
		<link>http://bavotasan.com/2011/is_child-conditional-function-for-wordpress/</link>
		<comments>http://bavotasan.com/2011/is_child-conditional-function-for-wordpress/#comments</comments>
		<pubDate>Sat, 23 Apr 2011 20:15:04 +0000</pubDate>
		<dc:creator>c.bavota</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[conditional tags]]></category>
		<category><![CDATA[Function]]></category>
		<category><![CDATA[is_child]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://bavotasan.com/?p=3351</guid>
		<description><![CDATA[I wrote a little function a while back to Check if a Page is a Child of Another Page in WordPress called is_child(). It used the page ID to return a true or false value dependent on if the current page was a child of the parent page specified. I have recently made a little [...]]]></description>
			<content:encoded><![CDATA[<p>I wrote a little function a while back to <a href="http://bit.ly/c4pYxR">Check if a Page is a Child of Another Page in WordPress</a> called <code>is_child()</code>. It used the page ID to return a true or false value dependent on if the current page was a child of the parent page specified. I have recently made a little change so that the function could now accept either a page ID or slug.</p>
<p>Here it is again with the changes:</p>

<div class="wp_syntax"><table border='0' cellpadding='0' cellspacing='0'><tr><td><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> is_child<span style="color: #009900;">&#40;</span><span style="color: #000088;">$page_id_or_slug</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
	<span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$post</span><span style="color: #339933;">;</span> 
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_int</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$page_id_or_slug</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$page</span> <span style="color: #339933;">=</span> get_page_by_path<span style="color: #009900;">&#40;</span><span style="color: #000088;">$page_id_or_slug</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$page_id_or_slug</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$page</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ID</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span> 
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>is_page<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">post_parent</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$page_id_or_slug</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
       		<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span> 
       		<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span> 
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></td></tr></table></div>

<p>The modification was inspired by <a href="http://bit.ly/gz0jJj">Helpful Page Functions for WordPress</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://bavotasan.com/2011/is_child-conditional-function-for-wordpress/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Helpful Page Functions for WordPress</title>
		<link>http://bavotasan.com/2011/helpful-page-functions-for-wordpress/</link>
		<comments>http://bavotasan.com/2011/helpful-page-functions-for-wordpress/#comments</comments>
		<pubDate>Wed, 13 Apr 2011 17:55:10 +0000</pubDate>
		<dc:creator>c.bavota</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Function]]></category>
		<category><![CDATA[functions.php]]></category>
		<category><![CDATA[id]]></category>
		<category><![CDATA[page content]]></category>
		<category><![CDATA[page id]]></category>
		<category><![CDATA[page slug]]></category>
		<category><![CDATA[slug]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://bavotasan.com/?p=3327</guid>
		<description><![CDATA[Over the past month I have been working on a large project that has taken up a lot of my time, but I wanted to quickly share two small functions that helped me. The first will display the content of a page by passing the page ID. Add the following code to your functions.php file: [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://bavotasan.com/wp-content/uploads/2011/04/notepad_bg.jpg" alt="" title="notepad_bg" width="550" height="200" class="aligncenter size-full wp-image-3336" />Over the past month I have been working on a large project that has taken up a lot of my time, but I wanted to quickly share two small functions that helped me. The first will display the content of a page by passing the page ID. Add the following code to your functions.php file:</p>

<div class="wp_syntax"><table border='0' cellpadding='0' cellspacing='0'><tr><td><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> get_page_content<span style="color: #009900;">&#40;</span><span style="color: #000088;">$page_id</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$page_data</span> <span style="color: #339933;">=</span> get_page<span style="color: #009900;">&#40;</span><span style="color: #000088;">$page_id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
	<span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> apply_filters<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'the_content'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$page_data</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">post_content</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$content</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span></pre></div></td></tr></table></div>

<p>If your page ID is &#8220;15&#8243;, you can now use the following code to display its content:</p>

<div class="wp_syntax"><table border='0' cellpadding='0' cellspacing='0'><tr><td><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> get_page_content<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">15</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></td></tr></table></div>

<p>The second function will return the page ID based on the slug. Add the following code to your functions.php file:</p>

<div class="wp_syntax"><table border='0' cellpadding='0' cellspacing='0'><tr><td><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> get_page_id<span style="color: #009900;">&#40;</span><span style="color: #000088;">$page_slug</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$page</span> <span style="color: #339933;">=</span> get_page_by_path<span style="color: #009900;">&#40;</span><span style="color: #000088;">$page_slug</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$page</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$page</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ID</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></td></tr></table></div>

<p>Now the following snippet will store the page ID in a variable called <code>$page_id</code>:</p>

<div class="wp_syntax"><table border='0' cellpadding='0' cellspacing='0'><tr><td><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$page_id</span> <span style="color: #339933;">=</span> get_page_id<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;about-me&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></td></tr></table></div>

<p>You could even use the two functions together:</p>

<div class="wp_syntax"><table border='0' cellpadding='0' cellspacing='0'><tr><td><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> get_page_content<span style="color: #009900;">&#40;</span>get_page_id<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;about-me&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></td></tr></table></div>

<p>Or you could modify the first function to use the page slug instead of the page ID:</p>

<div class="wp_syntax"><table border='0' cellpadding='0' cellspacing='0'><tr><td><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> get_page_content<span style="color: #009900;">&#40;</span><span style="color: #000088;">$page_slug</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$page</span> <span style="color: #339933;">=</span> get_page_by_path<span style="color: #009900;">&#40;</span><span style="color: #000088;">$page_slug</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$page_data</span> <span style="color: #339933;">=</span> get_page<span style="color: #009900;">&#40;</span><span style="color: #000088;">$page</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
	<span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> apply_filters<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'the_content'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$page_data</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">post_content</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$content</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span></pre></div></td></tr></table></div>

<p>Now you could use this to display your content:</p>

<div class="wp_syntax"><table border='0' cellpadding='0' cellspacing='0'><tr><td><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> get_page_content<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;about-me&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></td></tr></table></div>

<p>You could even take it a step further and make the function take either an ID or a page slug:</p>

<div class="wp_syntax"><table border='0' cellpadding='0' cellspacing='0'><tr><td><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> get_page_content<span style="color: #009900;">&#40;</span><span style="color: #000088;">$page_id_or_slug</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_int</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$page_id_or_slug</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$page</span> <span style="color: #339933;">=</span> get_page_by_path<span style="color: #009900;">&#40;</span><span style="color: #000088;">$page_id_or_slug</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$page_id_or_slug</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$page</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ID</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span> 
	<span style="color: #000088;">$page_data</span> <span style="color: #339933;">=</span> get_page<span style="color: #009900;">&#40;</span><span style="color: #000088;">$page_id_or_slug</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
	<span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> apply_filters<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'the_content'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$page_data</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">post_content</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$content</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span></pre></div></td></tr></table></div>

<p>Now the following will both give the same result:</p>

<div class="wp_syntax"><table border='0' cellpadding='0' cellspacing='0'><tr><td><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> get_page_content<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;about-me&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> get_page_content<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">15</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://bavotasan.com/2011/helpful-page-functions-for-wordpress/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Use jQuery to Replace a Word with a Link</title>
		<link>http://bavotasan.com/2010/jquery-replace-word-with-link/</link>
		<comments>http://bavotasan.com/2010/jquery-replace-word-with-link/#comments</comments>
		<pubDate>Mon, 09 Aug 2010 20:54:46 +0000</pubDate>
		<dc:creator>c.bavota</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[body]]></category>
		<category><![CDATA[footer]]></category>
		<category><![CDATA[Function]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[head]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[Lt]]></category>
		<category><![CDATA[page]]></category>
		<category><![CDATA[piece]]></category>
		<category><![CDATA[search]]></category>
		<category><![CDATA[site]]></category>
		<category><![CDATA[text]]></category>
		<category><![CDATA[var]]></category>
		<category><![CDATA[way]]></category>
		<category><![CDATA[Word]]></category>

		<guid isPermaLink="false">http://bavotasan.com/?p=2615</guid>
		<description><![CDATA[If you use a word multiple times on your site and you don&#8217;t feel like going through all your posts to replace every instance of it, you can use jQuery to search your page and do the replacing for you. I wanted to replace a word throughout an entire site with a link and all [...]]]></description>
			<content:encoded><![CDATA[<p>If you use a word multiple times on your site and you don&#8217;t feel like going through all your posts to replace every instance of it, you can use jQuery to search your page and do the replacing for you. I wanted to replace a word throughout an entire site with a link and all it took was a small piece of code. </p>
<p>If you don&#8217;t already have jQuery loaded into your page, include this before your closing <code>&lt;head&gt;</code>.</p>

<div class="wp_syntax"><table border='0' cellpadding='0' cellspacing='0'><tr><td><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">'text/javascript'</span> src<span style="color: #339933;">=</span><span style="color: #3366CC;">'http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></td></tr></table></div>

<p>That will load up jQuery from Google.</p>
<p>Now you just need to include this in your site&#8217;s footer, before the closing <code>&lt;body&gt;</code>. (Why in the footer? Because including your JavaScript after all of your code is the efficient way to do it.)</p>

<div class="wp_syntax"><table border='0' cellpadding='0' cellspacing='0'><tr><td><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> thePage <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;body&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  thePage.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>thePage.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/jQuery/ig</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'&lt;a href=&quot;http://jquery.com&quot;&gt;jQuery&lt;/a&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></td></tr></table></div>

<p>Since jQuery doesn&#8217;t have a core function for replacing text, we will use the JavaScript <code><a href="http://www.w3schools.com/jsref/jsref_replace.asp">replace()</a></code> function. The <code>i</code> after the slash makes the search and replace case-insensitive, and the <code>g</code> makes it global. </p>
<p>I have the script working on this page to replace every instance of the word jQuery with a link to the jQuery Web site.</p>
<p><script type="text/javascript">
(function($) {
  $(".posttop p").each(function() {
    $(this).html($(this).html().replace(/jQuery/ig, '<a href="http://jquery.com">jQuery</a>')); 
  });
})(jQuery)
</script></p>
]]></content:encoded>
			<wfw:commentRss>http://bavotasan.com/2010/jquery-replace-word-with-link/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Using Ajax with WordPress</title>
		<link>http://bavotasan.com/2010/using-ajax-with-wordpress/</link>
		<comments>http://bavotasan.com/2010/using-ajax-with-wordpress/#comments</comments>
		<pubDate>Mon, 02 Aug 2010 18:53:53 +0000</pubDate>
		<dc:creator>c.bavota</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[admin]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[Email]]></category>
		<category><![CDATA[feedback]]></category>
		<category><![CDATA[file]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[Function]]></category>
		<category><![CDATA[List]]></category>
		<category><![CDATA[look]]></category>
		<category><![CDATA[magazine]]></category>
		<category><![CDATA[mailing]]></category>
		<category><![CDATA[person]]></category>
		<category><![CDATA[way]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://bavotasan.com/?p=2586</guid>
		<description><![CDATA[A little while back, I updated Magazine Basic and added the Ajax save function. When I uploaded it to WordPress to await approval, I received some feedback on how I could incorporate Ajax using the admin-ajax.php file instead of the sloppy way I had it coded. All of the core Ajax functions in WordPress rely [...]]]></description>
			<content:encoded><![CDATA[<p>A little while back, I updated Magazine Basic and added the Ajax save function. When I uploaded it to WordPress to await approval, I received some feedback on how I could incorporate Ajax using the <code>admin-ajax.php</code> file instead of the sloppy way I had it coded. All of the core Ajax functions in WordPress rely on <code>admin-ajax.php</code> and using it for custom scripts is extremely easy to implement. </p>
<p>Let&#8217;s take a look at all the pieces you need to get it working. In this example I will build a small mailing list function that will send out an email to the admin when a person clicks submit.</p>
<p>Here&#8217;s our form:</p>

<div class="wp_syntax"><table border='0' cellpadding='0' cellspacing='0'><tr><td><div class="code"><pre class="php" style="font-family:monospace;">&lt;form method=&quot;post&quot; id=&quot;mailinglist&quot; action=&quot;&quot;&gt;
	&lt;div id=&quot;message&quot;&gt;&lt;/div&gt;
	&lt;input type=&quot;text&quot; name=&quot;mailinglistemail&quot; id=&quot;mailinglistemail&quot; /&gt;
	&lt;input type=&quot;submit&quot; name=&quot;submit&quot; id=&quot;mailinglistsubmit&quot; value=&quot;Join&quot; /&gt;&lt;img src=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> admin_url<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>/wp-admin/images/wpspin_light.gif&quot; alt=&quot;&quot; class=&quot;ajaxsave&quot; style=&quot;display: none;&quot; /&gt;
&lt;/form&gt;</pre></div></td></tr></table></div>

<p>Here&#8217;s the jQuery:</p>

<div class="wp_syntax"><table border='0' cellpadding='0' cellspacing='0'><tr><td><div class="code"><pre class="javascript" style="font-family:monospace;">jQuery<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#mailinglist&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">submit</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>	<span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#mailinglistemail&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#mailinglist #message&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Please enter your email address.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #003366; font-weight: bold;">var</span> email <span style="color: #339933;">=</span> jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#mailinglistemail'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>email.<span style="color: #660066;">indexOf</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;@&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span> <span style="color: #339933;">||</span> email.<span style="color: #660066;">indexOf</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#mailinglist #message&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Please enter a valid email address.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #003366; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
					action<span style="color: #339933;">:</span> <span style="color: #3366CC;">'join_mailinglist'</span><span style="color: #339933;">,</span>
					email<span style="color: #339933;">:</span> email
				<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
				jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#mailinglistsubmit&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">hide</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.ajaxsave&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				jQuery.<span style="color: #660066;">post</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&lt;?php echo admin_url('admin-ajax.php'); ?&gt;&quot;</span><span style="color: #339933;">,</span> data<span style="color: #339933;">,</span>
				<span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>response<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
					jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.ajaxsave&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">hide</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#mailinglistsubmit&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#mailinglist #message&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>response<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>		
				<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span> 
&nbsp;
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></td></tr></table></div>

<p>Let me explain each part. </p>
<p>When a user clicks submit, we need to check that the email field is not empty. If it is empty, we let the user know by sending the &#8220;Please enter your email address.&#8221; message and we stop the form from being submitted by including the <code>return false;</code> line.</p>

<div class="wp_syntax"><table border='0' cellpadding='0' cellspacing='0'><tr><td><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#mailinglistemail&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#mailinglist #message&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Please enter your email address.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></td></tr></table></div>

<p>Next, we&#8217;ll do a simple validation to make sure the email address actually contains the @ symbol and a period. If not, send an error message and return false.</p>

<div class="wp_syntax"><table border='0' cellpadding='0' cellspacing='0'><tr><td><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> email <span style="color: #339933;">=</span> jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#mailinglistemail'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>email.<span style="color: #660066;">indexOf</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;@&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span> <span style="color: #339933;">||</span> email.<span style="color: #660066;">indexOf</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#mailinglist #message&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Please enter a valid email address.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></td></tr></table></div>

<p>Once all that is done and everything is good, we can submit our form. This is where <code>admin-ajax.php</code> comes into play. When using <code>admin-ajax.php</code>, you need to include an action callback to make it work. Our callback is <code>join_mailinglist</code>. This is going to interact with our PHP to make everything work on the backend. Let&#8217;s create a data variable to store our action and our email value.</p>

<div class="wp_syntax"><table border='0' cellpadding='0' cellspacing='0'><tr><td><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
	action<span style="color: #339933;">:</span> <span style="color: #3366CC;">'join_mailinglist'</span><span style="color: #339933;">,</span>
	email<span style="color: #339933;">:</span> email
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></td></tr></table></div>

<p>Now we will hide our submit button and have a loader graphic appear to let people know something is going on. Then will will use the jQuery <a href="http://api.jquery.com/jQuery.post/">.post</a> function to send everything out to the <code>admin-ajax.php</code> file. Once the function is complete and a response is given, the loader will disappear, the submit button will return and a success message, or error message will indicate what happened.</p>

<div class="wp_syntax"><table border='0' cellpadding='0' cellspacing='0'><tr><td><div class="code"><pre class="javascript" style="font-family:monospace;">jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#mailinglistsubmit&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">hide</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.ajaxsave&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
jQuery.<span style="color: #660066;">post</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&lt;?php echo admin_url('admin-ajax.php'); ?&gt;&quot;</span><span style="color: #339933;">,</span> data<span style="color: #339933;">,</span>
<span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>response<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.ajaxsave&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">hide</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#mailinglistsubmit&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#mailinglist #message&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>response<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>		
<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span></pre></div></td></tr></table></div>

<p>With the form and the jQuery in place, all we need now is some PHP. </p>
<p>The <code>admin-ajax.php</code> file requires two action filters.</p>

<div class="wp_syntax"><table border='0' cellpadding='0' cellspacing='0'><tr><td><div class="code"><pre class="php" style="font-family:monospace;">add_action<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'wp_ajax_my_action'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'my_action_callback'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
add_action<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'wp_ajax_nopriv_my_action'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'my_action_callback'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></td></tr></table></div>

<p>The first action will only work if users are logged in. The second is if users don&#8217;t have to be logged in. For our requirements, it should look like this:</p>

<div class="wp_syntax"><table border='0' cellpadding='0' cellspacing='0'><tr><td><div class="code"><pre class="php" style="font-family:monospace;">add_action<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'wp_ajax_join_mailinglist'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'join_mailinglist_callback'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
add_action<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'wp_ajax_nopriv_join_mailinglist'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'join_mailinglist_callback'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></td></tr></table></div>

<p>Here is our join_mailinglist_callback function, which needs to be added to <code>functions.php</code>:</p>

<div class="wp_syntax"><table border='0' cellpadding='0' cellspacing='0'><tr><td><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> join_mailinglist_callback<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$email</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'email'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$email</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$yourEmail</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'c@bavotasan.com'</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$subject</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Add me to your mailing list'</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$success</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mail</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$yourEmail</span><span style="color: #339933;">,</span> <span style="color: #000088;">$subject</span><span style="color: #339933;">,</span> <span style="color: #000088;">$email</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$success</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Your email is subscribed to our mailing list.'</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'There was a problem. Please try again.'</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></td></tr></table></div>

<p>With everything in place, you will get a form that works like the one below.</p>
<form method="post" id="mailinglist" action="">
<div id="message"></div>
<input type="text" name="mailinglistemail" id="mailinglistemail" />
<input type="submit" name="submit" id="mailinglistsubmit" value="Join" /><img src="http://bavotasan.com/wp-admin/images/wpspin_light.gif" alt="" class="ajaxsave" style="display: none;" /><br />
</form>
]]></content:encoded>
			<wfw:commentRss>http://bavotasan.com/2010/using-ajax-with-wordpress/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Shorten Your Post Titles in WordPress</title>
		<link>http://bavotasan.com/2010/shorten-your-post-titles-in-wordpress/</link>
		<comments>http://bavotasan.com/2010/shorten-your-post-titles-in-wordpress/#comments</comments>
		<pubDate>Fri, 16 Jul 2010 15:24:22 +0000</pubDate>
		<dc:creator>c.bavota</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Array]]></category>
		<category><![CDATA[count]]></category>
		<category><![CDATA[file]]></category>
		<category><![CDATA[Function]]></category>
		<category><![CDATA[idea]]></category>
		<category><![CDATA[length]]></category>
		<category><![CDATA[line]]></category>
		<category><![CDATA[loop]]></category>
		<category><![CDATA[magazine]]></category>
		<category><![CDATA[Php]]></category>
		<category><![CDATA[php tags]]></category>
		<category><![CDATA[pop]]></category>
		<category><![CDATA[post]]></category>
		<category><![CDATA[reason]]></category>
		<category><![CDATA[Slideshow]]></category>
		<category><![CDATA[title]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://bavotasan.com/?p=2542</guid>
		<description><![CDATA[This is not a function that you would need to use often, but I came across a reason to use it recently and I thought it might be a good idea to share it. All it does is limit the number of words in your title, just in case you don&#8217;t want it to wrap [...]]]></description>
			<content:encoded><![CDATA[<p>This is not a function that you would need to use often, but I came across a reason to use it recently and I thought it might be a good idea to share it. All it does is limit the number of words in your title, just in case you don&#8217;t want it to wrap onto a second line. I used it in the featured slideshow on <a href="http://themes.bavotasan.com/our-themes/premium-themes/magazine-premium/">Magazine Premium</a> since the size of the boxes is fixed.</p>
<p>Include this in your <code>functions.php</code> file between the PHP tags:</p>

<div class="wp_syntax"><table border='0' cellpadding='0' cellspacing='0'><tr><td><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> short_title<span style="color: #009900;">&#40;</span><span style="color: #000088;">$after</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #000088;">$length</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$mytitle</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">,</span> get_the_title<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$length</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$mytitle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&gt;=</span><span style="color: #000088;">$length</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #990000;">array_pop</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$mytitle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$mytitle</span> <span style="color: #339933;">=</span> <span style="color: #990000;">implode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; &quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$mytitle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span> <span style="color: #000088;">$after</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$mytitle</span> <span style="color: #339933;">=</span> <span style="color: #990000;">implode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; &quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$mytitle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$mytitle</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></td></tr></table></div>

<p>Then you can use it within the WordPress loop like this:</p>

<div class="wp_syntax"><table border='0' cellpadding='0' cellspacing='0'><tr><td><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> short_title<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'...'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">8</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></td></tr></table></div>

<p>If you would rather count by characters instead of just words, you can add this function to your <code>functions.php</code> file instead of the one above:</p>

<div class="wp_syntax"><table border='0' cellpadding='0' cellspacing='0'><tr><td><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> short_title<span style="color: #009900;">&#40;</span><span style="color: #000088;">$after</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #000088;">$length</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$mytitle</span> <span style="color: #339933;">=</span> get_the_title<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$size</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$mytitle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$size</span><span style="color: #339933;">&gt;</span><span style="color: #000088;">$length</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$mytitle</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$mytitle</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$length</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$mytitle</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">,</span><span style="color: #000088;">$mytitle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #990000;">array_pop</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$mytitle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$mytitle</span> <span style="color: #339933;">=</span> <span style="color: #990000;">implode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; &quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$mytitle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #000088;">$after</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$mytitle</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://bavotasan.com/2010/shorten-your-post-titles-in-wordpress/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>6 Tips to Speed Up jQuery</title>
		<link>http://bavotasan.com/2010/tips-speed-up-jquery/</link>
		<comments>http://bavotasan.com/2010/tips-speed-up-jquery/#comments</comments>
		<pubDate>Tue, 13 Jul 2010 16:10:12 +0000</pubDate>
		<dc:creator>c.bavota</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[attr]]></category>
		<category><![CDATA[call]]></category>
		<category><![CDATA[Click]]></category>
		<category><![CDATA[div]]></category>
		<category><![CDATA[element]]></category>
		<category><![CDATA[Elements]]></category>
		<category><![CDATA[fff]]></category>
		<category><![CDATA[Function]]></category>
		<category><![CDATA[img]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[Location]]></category>
		<category><![CDATA[Padding]]></category>
		<category><![CDATA[Properties]]></category>
		<category><![CDATA[selector]]></category>
		<category><![CDATA[use]]></category>
		<category><![CDATA[way]]></category>

		<guid isPermaLink="false">http://bavotasan.com/?p=2518</guid>
		<description><![CDATA[As it stands, jQuery is an amazingly efficient JavaScript framework, but there are a few things that you can do when creating your code that will end up slowing it down significantly. I&#8217;ve noticed that a few small changes to my standard way of using jQuery have made my code run a lot faster. 1. [...]]]></description>
			<content:encoded><![CDATA[<p>As it stands, <a href="http://jquery.com/">jQuery</a> is an amazingly efficient JavaScript framework, but there are a few things that you can do when creating your code that will end up slowing it down significantly. I&#8217;ve noticed that a few small changes to my standard way of using <a href="http://jquery.com/">jQuery</a> have made my code run a lot faster.</p>
<h3>1. Be More Specific with Selectors</h3>
<p>When you are setting up <a href="http://jquery.com/">jQuery</a> to search through the DOM for a specific element, the more information you give it, the faster it will find the element. It is also faster to use IDs instead of classes.</p>
<p>Something like this will require going through the whole DOM structure to find the element:</p>

<div class="wp_syntax"><table border='0' cellpadding='0' cellspacing='0'><tr><td><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.item&quot;</span><span style="color: #009900;">&#41;</span></pre></div></td></tr></table></div>

<p>These are way more efficient:</p>

<div class="wp_syntax"><table border='0' cellpadding='0' cellspacing='0'><tr><td><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#item&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #006600; font-style: italic;">// use IDs instead of Classes</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;p.first .item&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #006600; font-style: italic;">// be more specific with the location</span></pre></div></td></tr></table></div>

<h3>2. Always Use $(this)  </h3>
<p>If you are using a selector to traverse the DOM in search of an element to perform a specific function on, that element becomes stored and can be manipulated within the function by using <code>$(this)</code>.</p>
<p>This code will traverse the DOM twice to get the same element:</p>

<div class="wp_syntax"><table border='0' cellpadding='0' cellspacing='0'><tr><td><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#item&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#item&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'clicked'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></td></tr></table></div>

<p>This is the efficient way since the element is already stored within the <code>$(this)</code> call:</p>

<div class="wp_syntax"><table border='0' cellpadding='0' cellspacing='0'><tr><td><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#item&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'clicked'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></td></tr></table></div>

<h3>3. Pass Multiple Selectors</h3>
<p>If you want the same functions to occur on multiple elements, it is possible to group selectors together to increase performance and avoid repetitive traversing of the DOM.</p>

<div class="wp_syntax"><table border='0' cellpadding='0' cellspacing='0'><tr><td><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// bad</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;p#one&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">fadeIn</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;p#two&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">fadeIn</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;p#three&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">fadeIn</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></td></tr></table></div>


<div class="wp_syntax"><table border='0' cellpadding='0' cellspacing='0'><tr><td><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// good</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;p#one, p#two, p#three&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">fadeIn</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></td></tr></table></div>

<h3>4. Use Multiple Properties and Values</h3>
<p>Many <a href="http://jquery.com/">jQuery</a> methods accept multiple properties and values instead of just a single property and value pair. Including multiple properties will help speeds things up as it reduces the amount of <a href="http://jquery.com/">jQuery</a> objects that are created within your code.</p>

<div class="wp_syntax"><table border='0' cellpadding='0' cellspacing='0'><tr><td><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// bad</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;img#one&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'src'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'http://mysite.com/images/image.jpg'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;img#one&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'alt'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'My Image'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></td></tr></table></div>


<div class="wp_syntax"><table border='0' cellpadding='0' cellspacing='0'><tr><td><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// good</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;img#one&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #3366CC;">'src'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'http://mysite.com/images/image.jpg'</span><span style="color: #339933;">,</span>
  <span style="color: #3366CC;">'alt'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'My Image'</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></td></tr></table></div>

<h3>5. Add Styles with Classes or IDs</h3>
<p>If you want to add multiple style changes to an element, it make more sense to use CSS to assign your styles to a Class or ID, and then add that Class or ID to the element. </p>
<p>Create your styles:</p>

<div class="wp_syntax"><table border='0' cellpadding='0' cellspacing='0'><tr><td><div class="code"><pre class="css" style="font-family:monospace;">&lt;style type<span style="color: #00AA00;">=</span><span style="color: #ff0000;">&quot;text/css&quot;</span><span style="color: #00AA00;">&gt;</span>
.<span style="color: #993333;">faster</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#dd0000</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#fff</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">5px</span> <span style="color: #933;">8px</span><span style="color: #00AA00;">;</span>
  <span style="color: #00AA00;">&#125;</span>
&lt;/style<span style="color: #00AA00;">&gt;</span></pre></div></td></tr></table></div>

<p>And instead of doing this:</p>

<div class="wp_syntax"><table border='0' cellpadding='0' cellspacing='0'><tr><td><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;p&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #3366CC;">'background'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'#dd0000'</span><span style="color: #339933;">,</span>
  <span style="color: #3366CC;">'color'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'#fff'</span><span style="color: #339933;">,</span>
  <span style="color: #3366CC;">'padding'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'5px 8px'</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></td></tr></table></div>

<p>Do this:</p>

<div class="wp_syntax"><table border='0' cellpadding='0' cellspacing='0'><tr><td><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;p&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;faster&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></td></tr></table></div>

<h3>6. Use Chaining</h3>
<p>Once you have already selected an element, you can chain your commands using <code>.end()</code> instead of traversing the DOM multiple times to find the same element. Understanding chaining might take a bit of trial and error to really get the hang of it. Certain commands require that you <code>.end()</code> the chain and certain don&#8217;t. I&#8217;ll give more in depth tutorial on chaining in the future to help guide you through the ins and outs of it all.</p>

<div class="wp_syntax"><table border='0' cellpadding='0' cellspacing='0'><tr><td><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">//bad</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;div&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">fadeIn</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;div p.one&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slideDown</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;div p.two&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'highlight'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></td></tr></table></div>


<div class="wp_syntax"><table border='0' cellpadding='0' cellspacing='0'><tr><td><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">//good</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;div&quot;</span><span style="color: #009900;">&#41;</span>
  .<span style="color: #660066;">fadeIn</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  .<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'p.one'</span><span style="color: #009900;">&#41;</span>
  .<span style="color: #660066;">slideDown</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
.<span style="color: #660066;">end</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  .<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'p.two'</span><span style="color: #009900;">&#41;</span>
  .<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'highlight'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></td></tr></table></div>

<p>These are just a few things that have helped me make my <a href="http://jquery.com/">jQuery</a> code run more efficiently. There are tons of books and resources out there that can help you take it to the next level to create the most efficient <a href="http://jquery.com/">jQuery</a> code imaginable. Here are a few I suggest:</p>
<ul>
<li><a href="http://jqueryenlightenment.com/">jQuery Enlightenment by Cody Lindley</a></li>
<li><a href="http://www.learningjquery.com/">Learning jQuery</a></li>
<li><a href="http://jqueryfordesigners.com/">jQuery for Designers</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://bavotasan.com/2010/tips-speed-up-jquery/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Excluding Pages from WordPress Search</title>
		<link>http://bavotasan.com/2010/excluding-pages-from-wordpress-search/</link>
		<comments>http://bavotasan.com/2010/excluding-pages-from-wordpress-search/#comments</comments>
		<pubDate>Thu, 08 Jul 2010 20:05:18 +0000</pubDate>
		<dc:creator>c.bavota</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[file]]></category>
		<category><![CDATA[Function]]></category>
		<category><![CDATA[order]]></category>
		<category><![CDATA[page]]></category>
		<category><![CDATA[Pages]]></category>
		<category><![CDATA[Php]]></category>
		<category><![CDATA[piece]]></category>
		<category><![CDATA[post]]></category>
		<category><![CDATA[query]]></category>
		<category><![CDATA[return]]></category>
		<category><![CDATA[search]]></category>
		<category><![CDATA[Search Query]]></category>
		<category><![CDATA[site]]></category>
		<category><![CDATA[solution]]></category>
		<category><![CDATA[type]]></category>

		<guid isPermaLink="false">http://bavotasan.com/?p=2508</guid>
		<description><![CDATA[When people visit my site and do a search, I assume they&#8217;re looking for results within my posts and not in any of my static pages. Too bad WordPress automatically searches through both. In order to exclude pages from the WordPress search, you need to add a small piece of code to the functions.php file. [...]]]></description>
			<content:encoded><![CDATA[<p>When people visit my site and do a search, I assume they&#8217;re looking for results within my posts and not in any of my static pages. Too bad WordPress automatically searches through both. In order to exclude pages from the WordPress search, you need to add a small piece of code to the <code>functions.php</code> file. I found a solution on <a href="http://www.wprecipes.com/how-to-exclude-posts-or-pages-from-search-results">WP Recipes</a> but I was not a fan of having to add all of my category IDs in order to make it work. So I decided to modify it a bit.</p>

<div class="wp_syntax"><table border='0' cellpadding='0' cellspacing='0'><tr><td><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> SearchFilter<span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">is_search</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$query</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'post_type'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'post'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$query</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
add_filter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'pre_get_posts'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'SearchFilter'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></td></tr></table></div>

<p>I forced it to just search for posts through setting the <code>post_type</code>. You can also make it do the opposite by setting the <code>post_type</code> to page.</p>
]]></content:encoded>
			<wfw:commentRss>http://bavotasan.com/2010/excluding-pages-from-wordpress-search/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Creating a Scrolling Back to Top Button with jQuery</title>
		<link>http://bavotasan.com/2010/scrolling-back-to-top-button-jquery/</link>
		<comments>http://bavotasan.com/2010/scrolling-back-to-top-button-jquery/#comments</comments>
		<pubDate>Mon, 28 Jun 2010 20:55:39 +0000</pubDate>
		<dc:creator>c.bavota</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[anchor]]></category>
		<category><![CDATA[animate]]></category>
		<category><![CDATA[body]]></category>
		<category><![CDATA[button]]></category>
		<category><![CDATA[Click]]></category>
		<category><![CDATA[Creating]]></category>
		<category><![CDATA[Function]]></category>
		<category><![CDATA[hundred times]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[link]]></category>
		<category><![CDATA[Lt]]></category>
		<category><![CDATA[name]]></category>
		<category><![CDATA[page]]></category>
		<category><![CDATA[span]]></category>
		<category><![CDATA[tag]]></category>
		<category><![CDATA[version]]></category>
		<category><![CDATA[way]]></category>

		<guid isPermaLink="false">http://bavotasan.com/?p=2234</guid>
		<description><![CDATA[If you take a look way, way, way down at the bottom of this page, you will see a Back to Top button that scrolls the whole page until it reaches to top. It is a pretty simple effect to add to your site and looks a hundred-times cooler than just using your typical anchor [...]]]></description>
			<content:encoded><![CDATA[<p>If you take a look way, way, way down at the bottom of this page, you will see a Back to Top button that scrolls the whole page until it reaches to top. It is a pretty simple effect to add to your site and looks a hundred-times cooler than just using your typical anchor name and link. Getting it all to work takes nothing more than a few lines of <a href="http://jquery.com/">jQuery</a> version 1.4.2.<br />
<span id="more-2234"></span><br />
First, we need to create our button/link. I am just going to use an anchor tag and some text:</p>

<div class="wp_syntax"><table border='0' cellpadding='0' cellspacing='0'><tr><td><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>a href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;javascript:void(0)&quot;</span> <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;backtotop&quot;</span><span style="color: #339933;">&gt;</span>Back to Top<span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;</span></pre></div></td></tr></table></div>

<p>Next we need to add some <a href="http://jquery.com/">jQuery</a> between the <code>&lt;head&gt;</code> tags:</p>

<div class="wp_syntax"><table border='0' cellpadding='0' cellspacing='0'><tr><td><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">'text/javascript'</span> src<span style="color: #339933;">=</span><span style="color: #3366CC;">'http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">'text/javascript'</span><span style="color: #339933;">&gt;</span>
jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.backtotop'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'html, body'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">animate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>scrollTop<span style="color: #339933;">:</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'slow'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></td></tr></table></div>

<p>Remember, you can change the speed by replacing <code>slow</code> with a numerical value. You can also add some easing effects by including the <a href="http://jqueryui.com/">jQuery UI</a>. Read more about easing effects <a href="http://jqueryui.com/demos/effect/#easing">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://bavotasan.com/2010/scrolling-back-to-top-button-jquery/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Using get_theme_data() when Developing a Custom Theme for WordPress</title>
		<link>http://bavotasan.com/2010/get_theme_data-developing-custom-theme-wordpress/</link>
		<comments>http://bavotasan.com/2010/get_theme_data-developing-custom-theme-wordpress/#comments</comments>
		<pubDate>Tue, 22 Jun 2010 18:39:45 +0000</pubDate>
		<dc:creator>c.bavota</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[author]]></category>
		<category><![CDATA[com]]></category>
		<category><![CDATA[Css File]]></category>
		<category><![CDATA[description]]></category>
		<category><![CDATA[download]]></category>
		<category><![CDATA[file]]></category>
		<category><![CDATA[Function]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[ie 7]]></category>
		<category><![CDATA[layout]]></category>
		<category><![CDATA[magazine]]></category>
		<category><![CDATA[menu system]]></category>
		<category><![CDATA[online]]></category>
		<category><![CDATA[page]]></category>
		<category><![CDATA[Php]]></category>
		<category><![CDATA[search]]></category>
		<category><![CDATA[Style]]></category>
		<category><![CDATA[style css]]></category>

		<guid isPermaLink="false">http://bavotasan.com/?p=2224</guid>
		<description><![CDATA[If you are building a custom theme that you would like to add to the WordPress directory or start to sell online somewhere, a great function that can help you out is get_theme_data(). What exactly does it do? It gathers all the theme information that you were supposed to add to the style.css file so [...]]]></description>
			<content:encoded><![CDATA[<p>If you are building a custom theme that you would like to add to the WordPress directory or start to sell online somewhere, a great function that can help you out is <code>get_theme_data()</code>. What exactly does it do? It gathers all the theme information that you were supposed to add to the <code>style.css</code> file so you can use it throughout your theme.<br />
<span id="more-2224"></span><br />
If you download a copy of Magazine  Basic, you can see it in action in the <code>functions.php</code> file, right at the top. To make sure it is working, though, you have to have your style.css file setup correctly. The top of that file should look something like this:</p>

<div class="wp_syntax"><table border='0' cellpadding='0' cellspacing='0'><tr><td><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/*
Theme Name: Magazine Basic
Theme URI: http://themes.bavotasan.com/our-themes/basic-themes/magazine-basic
Description: Built for WordPress 3.0. A magazine style theme with a fully customizable layout. Theme options include site width, 1 or 2 widgetized sidebars, header logo, multiple front page grid layouts, new WP 3.0 menu system, Google Analytics, pagination, drop-down menus and tons more. Also includes dynamic SEO keywords and site descriptions. Tested in Firefox 3.6, IE 7 &amp; 8 and Safari 4. Fully optimized for search engine ranking. Translation ready. 100% valid xHTML. Designed by &lt;a href=&quot;http://themes.bavotasan.com/&quot;&gt;Themes by bavotasan.com&lt;/a&gt;.
Version: 2.6.7
Author: c.bavota
Author URI: http://themes.bavotasan.com/
Tags: right-sidebar,left-sidebar,fixed-width,three-columns,two-columns,white,custom-header,theme-options
&nbsp;
	The CSS, XHTML and design is released under GPL:
	http://www.opensource.org/licenses/gpl-license.php
&nbsp;
*/</span></pre></div></td></tr></table></div>

<p>This is all information that you can now use with <code>get_theme_data()</code>. I use it in Magazine Basic like so:</p>

<div class="wp_syntax"><table border='0' cellpadding='0' cellspacing='0'><tr><td><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$bavotasan_theme_data</span> <span style="color: #339933;">=</span> get_theme_data<span style="color: #009900;">&#40;</span>TEMPLATEPATH<span style="color: #339933;">.</span><span style="color: #0000ff;">'/style.css'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></td></tr></table></div>

<p>With that in my <code>functions.php</code> file, I can use the array of data that it provides like so:</p>

<div class="wp_syntax"><table border='0' cellpadding='0' cellspacing='0'><tr><td><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$bavotasan_theme_data</span> <span style="color: #339933;">=</span> get_theme_data<span style="color: #009900;">&#40;</span>TEMPLATEPATH<span style="color: #339933;">.</span><span style="color: #0000ff;">'/style.css'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'THEME_NAME'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$bavotasan_theme_data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Name'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'THEME_AUTHOR'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$bavotasan_theme_data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Author'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'THEME_HOMEPAGE'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$bavotasan_theme_data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'URI'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'THEME_VERSION'</span><span style="color: #339933;">,</span> <span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$bavotasan_theme_data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Version'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></td></tr></table></div>

<p>If you are not familiar with the define() function, check out <a href="http://bavotasan.com/tutorials/define-constant-php/">Defining a Constant with PHP</a>.</p>
<p>Now you can use those four constants throughout your theme. Here are some examples that I use:</p>
<p>In <code>footer.php</code>:</p>

<div class="wp_syntax"><table border='0' cellpadding='0' cellspacing='0'><tr><td><div class="code"><pre class="php" style="font-family:monospace;">&lt;span class=&quot;red&quot;&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> THEME_NAME<span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/span&gt; <span style="color: #000000; font-weight: bold;">&lt;?php</span> _e<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;theme designed by&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;magazine-basic&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> &lt;a href=&quot;http://themes.bavotasan.com&quot;&gt;&lt;span class=&quot;red&quot;&gt;Themes by bavotasan.com&lt;/span&gt;&lt;/a&gt;.&lt;br /&gt;</pre></div></td></tr></table></div>

<p>In <code>functions.php</code>:</p>

<div class="wp_syntax"><table border='0' cellpadding='0' cellspacing='0'><tr><td><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;p&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;'</span><span style="color: #339933;">.</span>__<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Version'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;magazine-basic&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">':&lt;/strong&gt; '</span><span style="color: #339933;">.</span>THEME_VERSION<span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;/li&gt;&lt;li&gt;&lt;strong&gt;'</span><span style="color: #339933;">.</span>__<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Author'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;magazine-basic&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">':&lt;/strong&gt; &lt;a href=&quot;http://bavotasan.com/&quot;&gt;'</span><span style="color: #339933;">.</span>THEME_AUTHOR<span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;strong&gt;'</span><span style="color: #339933;">.</span>__<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Built by'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;magazine-basic&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">':&lt;/strong&gt; &lt;a href=&quot;http://themes.bavotasan.com/&quot;&gt;Themes by bavotasan.com&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;strong&gt;'</span><span style="color: #339933;">.</span>__<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Theme home page'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;magazine-basic&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">':&lt;/strong&gt; &lt;a href=&quot;'</span><span style="color: #339933;">.</span>THEME_HOMEPAGE<span style="color: #339933;">.</span><span style="color: #0000ff;">'&quot;&gt;'</span><span style="color: #339933;">.</span>THEME_NAME<span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/p&gt;'</span><span style="color: #339933;">;</span></pre></div></td></tr></table></div>

<p>You can also get the description and the tags. Check out the <a href="http://codex.wordpress.org/Function_Reference/get_theme_data">Codex</a> for a little more info.</p>
]]></content:encoded>
			<wfw:commentRss>http://bavotasan.com/2010/get_theme_data-developing-custom-theme-wordpress/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

