<?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>Blue Anvil Journal &#187; Wordpress development techniques</title>
	<atom:link href="http://blue-anvil.com/tag/wordpress-development-techniques/feed/" rel="self" type="application/rss+xml" />
	<link>http://blue-anvil.com</link>
	<description>The online blogfolio of Mike Jolley</description>
	<lastBuildDate>Fri, 03 Dec 2010 18:16:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>8 fun &amp; useful shortcode functions for wordpress</title>
		<link>http://blue-anvil.com/archives/8-fun-useful-shortcode-functions-for-wordpress/</link>
		<comments>http://blue-anvil.com/archives/8-fun-useful-shortcode-functions-for-wordpress/#comments</comments>
		<pubDate>Sat, 14 Mar 2009 19:39:08 +0000</pubDate>
		<dc:creator>Mike Jolley</dc:creator>
				<category><![CDATA[Wordpress & plugins]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[shortcode]]></category>
		<category><![CDATA[shortcodes]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[Wordpress development techniques]]></category>

		<guid isPermaLink="false">http://blue-anvil.com/?p=344</guid>
		<description><![CDATA[&#8216;Shortcodes&#8217; are a feature of wordpress used for executing custom functions by inserting a small snippit of code into a post. For example, my download monitor plugin now uses shortcodes; by entering [download id="1"] into a post, the plugin will output a download link. The shortcode syntax varies, here are some examples: [shortcode] [shortcode atribute="value"] [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone size-full wp-image-424" title="shortcodes" src="http://blue-anvil.com/wp-content/uploads/2009/03/shortcodes.gif" alt="shortcodes" width="488" height="100" /></p>
<p>&#8216;Shortcodes&#8217; are a feature of wordpress used for executing custom functions by inserting a small snippit of code into a post. For example, my download monitor plugin now uses shortcodes; by entering [<code>download id="1"</code>] into a post, the plugin will output a download link.</p>
<p>The shortcode syntax varies, here are some examples:</p>
<ul>
<li><code>[shortcode]</code></li>
<li><code>[shortcode atribute="value"]</code></li>
<li><code>[shortcode]Text[/shortcode]</code></li>
</ul>
<p>Shortcodes can be created to perform a handful of useful functions. To use any of the shortcodes in this post I recommend adding the functions to your functions.php theme file (create it if it does not exist!). Ill show shortcodes ranging from showing adsense to speaking pirate! (written for wordpress 2.7)</p>
<p><span id="more-344"></span></p>
<h2>1. Get some bloginfo</h2>
<p>Need to get your wordpress url or template url? Try this on for size:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> bloginfo_shortcode<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$atts</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">extract</span><span style="color: #009900;">&#40;</span>shortcode_atts<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'key'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$atts</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> get_bloginfo<span style="color: #009900;">&#40;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
add_shortcode<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'bloginfo'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'bloginfo_shortcode'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Now you can output some bloginfo using <code>[bloginfo key=""]</code>, for example  <code>[bloginfo key="template_url"]</code> will output the path to your template directory. <a href="http://codex.wordpress.org/Template_Tags/get_bloginfo">This page </a>highlights the data you can grab using this custom shortcode.</p>
<p>Example output: http://blue-anvil.com/wp-content/themes/blueanvil</p>
<p><p style="text-align:center"><script type="text/javascript"><!--
google_ad_client = "pub-6928386133078955";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text_image";
google_ad_channel = "";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "444444";
google_color_text = "444444";
google_color_url = "444444";
//-->
</script>
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p></p>
<h2>2. Integrate some Adsense Ads</h2>
<p>Want to output some adsense ads in posts? Try this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> adsense_shortcode<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$atts</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #990000;">extract</span><span style="color: #009900;">&#40;</span>shortcode_atts<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
	    <span style="color: #0000ff;">'format'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'1'</span><span style="color: #339933;">,</span>
	<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$atts</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">switch</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$format</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">1</span> <span style="color: #339933;">:</span>
			<span style="color: #000088;">$ad</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'&lt;script type=&quot;text/javascript&quot;&gt;&lt;!--
			google_ad_client = &quot;pub-6928386133078955&quot;;
			/* 234x60, created 16/09/08 */
			google_ad_slot = &quot;0834408702&quot;;
			google_ad_width = 234;
			google_ad_height = 60;
			//--&gt;
			&lt;/script&gt;
			&lt;script type=&quot;text/javascript&quot;
			src=&quot;http://pagead2.googlesyndication.com/pagead/show_ads.js&quot;&gt;
			&lt;/script&gt;'</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$ad</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
add_shortcode<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'adsense'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'adsense_shortcode'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Remember to replace the adsense code with your own from google. Now you can output an ad simply using <code>[adsense]</code>.</p>
<div class="adsense"><script type="text/javascript"><!--
			google_ad_client = "pub-6928386133078955";
			/* 234x60, created 16/09/08 */
			google_ad_slot = "0834408702";
			google_ad_width = 234;
			google_ad_height = 60;
			//-->
			</script>
			<script type="text/javascript"
			src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
			</script></div>
<p>Add additional ad formats to the switch statement and you can output other ad formats using, for example,  <code>[adsense format="2"]</code>. Nice eh?</p>
<h2>3. Obfuscate an email address</h2>
<p>This simple shortcode will munge/create a link to an email address to <em>help</em> prevent spam. Not 100% foolproof but better than nothing (and a good basis to expand upon if you want to enhance it).</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> munge_mail_shortcode<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$atts</span> <span style="color: #339933;">,</span> <span style="color: #000088;">$content</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">null</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$content</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$encodedmail</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;&amp;#&quot;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">ord</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$content</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">';'</span><span style="color: #339933;">;</span> 
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">'&lt;a href=&quot;mailto:'</span><span style="color: #339933;">.</span><span style="color: #000088;">$encodedmail</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&quot;&gt;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$encodedmail</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;/a&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
add_shortcode<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'mailto'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'munge_mail_shortcode'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Use: <code>[mailto]email@yourdomain.com[/mailto]</code>.</p>
<a href="mailto:&#101;&#109;&#97;&#105;&#108;&#64;&#121;&#111;&#117;&#114;&#100;&#111;&#109;&#97;&#105;&#110;&#46;&#99;&#111;&#109;">&#101;&#109;&#97;&#105;&#108;&#64;&#121;&#111;&#117;&#114;&#100;&#111;&#109;&#97;&#105;&#110;&#46;&#99;&#111;&#109;</a>
<p>In the source the mail is shown as:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&amp;amp;#101;&amp;amp;#109;&amp;amp;#97;&amp;amp;#105;&amp;amp;#108;&amp;amp;#64;&amp;amp;#101;&amp;amp;#109;&amp;amp;#97;&amp;amp;#105;&amp;amp;#108;&amp;amp;#46;&amp;amp;#99;&amp;amp;#111;&amp;amp;#109;</pre></div></div>

<h2>4. Show related posts (uses tags)</h2>
<p>How about showing some related posts in your current post based on it&#8217;s tags? Lets do it!</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> related_posts_shortcode<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$atts</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #990000;">extract</span><span style="color: #009900;">&#40;</span>shortcode_atts<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
	    <span style="color: #0000ff;">'limit'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'5'</span><span style="color: #339933;">,</span>
	<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$atts</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$wpdb</span><span style="color: #339933;">,</span> <span style="color: #000088;">$post</span><span style="color: #339933;">,</span> <span style="color: #000088;">$table_prefix</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ID</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #000088;">$retval</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'
&lt;ul&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// Get tags</span>
		<span style="color: #000088;">$tags</span> <span style="color: #339933;">=</span> wp_get_post_tags<span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</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;">$tagsarray</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$tags</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$tag</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$tagsarray</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$tag</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">term_id</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000088;">$tagslist</span> <span style="color: #339933;">=</span> <span style="color: #990000;">implode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">','</span><span style="color: #339933;">,</span> <span style="color: #000088;">$tagsarray</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// Do the query</span>
		<span style="color: #000088;">$q</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;
			SELECT p.*, count(tr.object_id) as count
			FROM <span style="color: #006699; font-weight: bold;">$wpdb-&gt;term_taxonomy</span> AS tt, <span style="color: #006699; font-weight: bold;">$wpdb-&gt;term_relationships</span> AS tr, <span style="color: #006699; font-weight: bold;">$wpdb-&gt;posts</span> AS p
			WHERE tt.taxonomy ='post_tag'
				AND tt.term_taxonomy_id = tr.term_taxonomy_id
				AND tr.object_id  = p.ID
				AND tt.term_id IN (<span style="color: #006699; font-weight: bold;">$tagslist</span>)
				AND p.ID != <span style="color: #006699; font-weight: bold;">$post-&gt;ID</span>
				AND p.post_status = 'publish'
				AND p.post_date_gmt &lt; NOW()
			GROUP BY tr.object_id
			ORDER BY count DESC, p.post_date_gmt DESC
			LIMIT <span style="color: #006699; font-weight: bold;">$limit</span>;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000088;">$related</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$wpdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_results</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$q</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$related</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$related</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$r</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$retval</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'
	&lt;li&gt;&lt;a title=&quot;'</span><span style="color: #339933;">.</span>wptexturize<span style="color: #009900;">&#40;</span><span style="color: #000088;">$r</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">post_title</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&quot; href=&quot;'</span><span style="color: #339933;">.</span>get_permalink<span style="color: #009900;">&#40;</span><span style="color: #000088;">$r</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: #0000ff;">'&quot;&gt;'</span><span style="color: #339933;">.</span>wptexturize<span style="color: #009900;">&#40;</span><span style="color: #000088;">$r</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">post_title</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;/a&gt;&lt;/li&gt;
'</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$retval</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'
	&lt;li&gt;No related posts found&lt;/li&gt;
'</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000088;">$retval</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&lt;/ul&gt;
'</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$retval</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">return</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
add_shortcode<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'related_posts'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'related_posts_shortcode'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Magic. Now use <code>[related_posts]</code> to show 5 related posts, or <code>[related_posts limit="x"]</code> to show a number of your choosing. Example:</p>
<ul><li><a href="http://blue-anvil.com/archives/wordpress-development-techniques-2-fetching-rss-feeds-with-wordpress/" title="Wordpress development techniques #2 &#8211; Fetching RSS feeds with wordpress">Wordpress development techniques #2 &#8211; Fetching RSS feeds with wordpress</a></li><li><a href="http://blue-anvil.com/archives/wordpress-development-techniques-1-running-custom-queries-using-the-wpdb-class/" title="Wordpress development techniques #1 &#8211; Running custom queries using the &#8216;wpdb&#8217; class">Wordpress development techniques #1 &#8211; Running custom queries using the &#8216;wpdb&#8217; class</a></li><li><a href="http://blue-anvil.com/archives/wordpress-28-memory-usage/" title="Wordpress 2.8 Memory Usage">Wordpress 2.8 Memory Usage</a></li><li><a href="http://blue-anvil.com/archives/including-css-javascript-in-wordpress-posts-using-custom-fields/" title="Including CSS &#038; JavaScript in wordpress posts using Custom Fields">Including CSS &#038; JavaScript in wordpress posts using Custom Fields</a></li><li><a href="http://blue-anvil.com/archives/create-a-wordpress-recent-posts-widget/" title="Create a Wordpress Recent-Posts Widget">Create a Wordpress Recent-Posts Widget</a></li></ul>
<h2>5. Add a simple paypal donation link</h2>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> donate_shortcode<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$atts</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">extract</span><span style="color: #009900;">&#40;</span>shortcode_atts<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'text'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Make a donation'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'account'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'REPLACE ME'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'for'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$atts</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$post</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$for</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$for</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; &quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;+&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">post_title</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">'&lt;a class=&quot;donateLink&quot; href=&quot;https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&amp;business='</span><span style="color: #339933;">.</span><span style="color: #000088;">$account</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&amp;item_name=Donation+for+'</span><span style="color: #339933;">.</span><span style="color: #000088;">$for</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&quot;&gt;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$text</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;/a&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
add_shortcode<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'donate'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'donate_shortcode'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Replace the default &#8216;account&#8217; with your paypal email address, and output using <code>[donate]</code>. Remember you can override the default &#8216;text&#8217; too in the shortcode. You can style this link with css (as ive done) easily.</p>
<p class="buymebeer"><a href="https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&amp;business=mike.jolley@me.com&amp;item_name=Donation+for+8+fun+&+useful+shortcode+functions+for+wordpress" class="donateLink">Make a donation</a></p>
<h2>6. Output a google powered chart</h2>
<p>Pass this shortcode some data and it will convert it into a graph generated by <a href="http://code.google.com/apis/chart/">google charts api</a>. First I&#8217;ll show it in action. I want to show some info about my traffic to Blue Anvil from analytics; I will show it in a pie chart:</p>
<div class="aligncenter"><img src="http://chart.apis.google.com/chart?cht=p3&chtt=Traffic Sources&chl=Reffering+sites|Search+Engines|Direct+traffic|Other&chco=058DC7,50B432,ED561B,EDEF00&chs=488x200&chd=t:41.52,37.79,20.67,0.03&chf=F7F9FA" title="Traffic Sources" alt="Traffic Sources" /></div>
<p>Code: <code>[chart data="41.52,37.79,20.67,0.03" bg="F7F9FA" labels="Reffering+sites|Search+Engines|Direct+traffic|Other" colors="058DC7,50B432,ED561B,EDEF00" size="488x200" title="Traffic Sources" type="pie"]</code></p>
<h3>The shortcode</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> chart_shortcode<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$atts</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #990000;">extract</span><span style="color: #009900;">&#40;</span>shortcode_atts<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
	    <span style="color: #0000ff;">'data'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span>
	    <span style="color: #0000ff;">'colors'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span>
	    <span style="color: #0000ff;">'size'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'400x200'</span><span style="color: #339933;">,</span>
	    <span style="color: #0000ff;">'bg'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'ffffff'</span><span style="color: #339933;">,</span>
	    <span style="color: #0000ff;">'title'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span>
	    <span style="color: #0000ff;">'labels'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span>
	    <span style="color: #0000ff;">'advanced'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span>
	    <span style="color: #0000ff;">'type'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'pie'</span>
	<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$atts</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">switch</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$type</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'line'</span> <span style="color: #339933;">:</span>
			<span style="color: #000088;">$charttype</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'lc'</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'xyline'</span> <span style="color: #339933;">:</span>
			<span style="color: #000088;">$charttype</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'lxy'</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'sparkline'</span> <span style="color: #339933;">:</span>
			<span style="color: #000088;">$charttype</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'ls'</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'meter'</span> <span style="color: #339933;">:</span>
			<span style="color: #000088;">$charttype</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'gom'</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'scatter'</span> <span style="color: #339933;">:</span>
			<span style="color: #000088;">$charttype</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'s'</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'venn'</span> <span style="color: #339933;">:</span>
			<span style="color: #000088;">$charttype</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'v'</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'pie'</span> <span style="color: #339933;">:</span>
			<span style="color: #000088;">$charttype</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'p3'</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'pie2d'</span> <span style="color: #339933;">:</span>
			<span style="color: #000088;">$charttype</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'p'</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">default</span> <span style="color: #339933;">:</span>
			<span style="color: #000088;">$charttype</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$type</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$title</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$string</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&amp;chtt='</span><span style="color: #339933;">.</span><span style="color: #000088;">$title</span><span style="color: #339933;">.</span><span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$labels</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$string</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&amp;chl='</span><span style="color: #339933;">.</span><span style="color: #000088;">$labels</span><span style="color: #339933;">.</span><span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$colors</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$string</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&amp;chco='</span><span style="color: #339933;">.</span><span style="color: #000088;">$colors</span><span style="color: #339933;">.</span><span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$string</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&amp;chs='</span><span style="color: #339933;">.</span><span style="color: #000088;">$size</span><span style="color: #339933;">.</span><span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$string</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&amp;chd=t:'</span><span style="color: #339933;">.</span><span style="color: #000088;">$data</span><span style="color: #339933;">.</span><span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$string</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&amp;chf='</span><span style="color: #339933;">.</span><span style="color: #000088;">$bg</span><span style="color: #339933;">.</span><span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">'&lt;img title=&quot;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$title</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&quot; src=&quot;http://chart.apis.google.com/chart?cht='</span><span style="color: #339933;">.</span><span style="color: #000088;">$charttype</span><span style="color: #339933;">.</span><span style="color: #0000ff;">''</span><span style="color: #339933;">.</span><span style="color: #000088;">$string</span><span style="color: #339933;">.</span><span style="color: #000088;">$advanced</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&quot; alt=&quot;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$title</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&quot; /&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
add_shortcode<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'chart'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'chart_shortcode'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h3>Other examples:</h3>
<div class="aligncenter"><img src="http://chart.apis.google.com/chart?cht=ls&chs=200x100&chd=t:0,12,24,26,32,64,54,24,22,20,8,2,0,0,3&chf=F7F9FA" title="" alt="" /></div>
<p>Code: <code>[chart data="0,12,24,26,32,64,54,24,22,20,8,2,0,0,3" bg="F7F9FA" size="200x100" type="sparkline"]</code></p>
<div class="aligncenter"><img src="http://chart.apis.google.com/chart?cht=gom&chtt=Macbook Fund&chl=Mula&chco=058DC7,50B432,ED561B,EDEF00&chs=488x200&chd=t:40&chf=F7F9FA" title="Macbook Fund" alt="Macbook Fund" /></div>
<p>Code: <code>[chart data="40" bg="F7F9FA" labels="Mula" colors="058DC7,50B432,ED561B,EDEF00" size="488x200" title="Macbook Fund" type="gom"]</code></p>
<h2>7. Show a google map with a marker on it</h2>
<p>Want to show a map and/or a marker on it? You&#8217;ll need a <a href="http://code.google.com/apis/maps/signup.html">Google Maps API key</a> for this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> googlemap_shortcode<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$atts</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">extract</span><span style="color: #009900;">&#40;</span>shortcode_atts<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'width'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'500px'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'height'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'300px'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'apikey'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'REPLACEME'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'marker'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'center'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'zoom'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'13'</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$atts</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$center</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$setCenter</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'map.setCenter(new GLatLng('</span><span style="color: #339933;">.</span><span style="color: #000088;">$center</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'), '</span><span style="color: #339933;">.</span><span style="color: #000088;">$zoom</span><span style="color: #339933;">.</span><span style="color: #0000ff;">');'</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$marker</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$setMarker</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'map.addOverlay(new GMarker(new GLatLng('</span><span style="color: #339933;">.</span><span style="color: #000088;">$marker</span><span style="color: #339933;">.</span><span style="color: #0000ff;">')));'</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$rand</span> <span style="color: #339933;">=</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">100</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">'
    	&lt;script src=&quot;http://maps.google.com/maps?file=api&amp;v=2.x&amp;sensor=false&amp;key='</span><span style="color: #339933;">.</span><span style="color: #000088;">$apikey</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
&nbsp;
	    &lt;script type=&quot;text/javascript&quot;&gt;
		    function initialize() {
		      if (GBrowserIsCompatible()) {
		        var map = new GMap2(document.getElementById(&quot;map_canvas_'</span><span style="color: #339933;">.</span><span style="color: #000088;">$rand</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&quot;));
		        '</span><span style="color: #339933;">.</span><span style="color: #000088;">$setCenter</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'
		        '</span><span style="color: #339933;">.</span><span style="color: #000088;">$setMarker</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'
		        map.setUIToDefault();
		      }
		    }
		    initialize();
	    &lt;/script&gt;
    '</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
add_shortcode<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'googlemap'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'googlemap_shortcode'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>To display a map just enter <code>[googlemap]</code> replacing center/marker/zoom co-ordinates with whatever you like.</p>

    	<script src="http://maps.google.com/maps?file=api&amp;v=2.x&amp;sensor=false&amp;key=ABQIAAAAijTSokzlqzK5H8ivKKD9ARSx-0R_ijOjIeHomyqtOonL2UUwpRSFeYGL_tJGXtk0R7sMZq2eiDi5xQ" type="text/javascript"></script>
    	<div id="map_canvas_5800" style="width: 488px; height: 300px"></div>
	    <script type="text/javascript">
		    function initialize() {
		      if (GBrowserIsCompatible()) {
		        var map = new GMap2(document.getElementById("map_canvas_5800"));
		        map.setCenter(new GLatLng(52.66389056542801, 0.1641082763671875), 13);
		        map.addOverlay(new GMarker(new GLatLng(52.66389056542801, 0.1641082763671875)));
		        map.setUIToDefault();
		      }
		    }
		    initialize();
	    </script>
    
<p>Code: <code>[googlemap zoom="13" center="52.66389056542801, 0.1641082763671875" marker="52.66389056542801, 0.1641082763671875" width="488px"]</code></p>
<h2>8. Talk like a pirate</h2>
<p>Just for fun we can convert a section of text into pirate speak using the following shortcode (inspired by/taken from <a href="http://drupal.org/project/pirate">Pirate</a> for drupal and the <a href="http://wordpress.org/extend/plugins/text-filter-suite/other_notes/">text filter suite</a> pirate filter).</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> pirate_talk_shortcode<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$atts</span> <span style="color: #339933;">,</span> <span style="color: #000088;">$content</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">null</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
      <span style="color: #000088;">$patterns</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'%\bmy\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'me'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bboss\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'admiral'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bmanager\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'admiral'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\b[Cc]aptain\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;Cap'n&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bmyself\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'meself'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\byour\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'yer'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\byou\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'ye'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bfriend\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'matey'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bfriends\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'maties'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bco[-]?worker\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'shipmate'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bco[-]?workers\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'shipmates'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bpeople\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'scallywags'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bearlier\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'afore'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bold\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'auld'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bthe\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;th'&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bof\b%'</span> <span style="color: #339933;">=&gt;</span>  <span style="color: #0000ff;">&quot;o'&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;%\bdon't\b%&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;dern't&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bdo not\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;dern't&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bnever\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;no nay ne'er&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bever\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;e'er&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bover\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;o'er&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bYes\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Aye'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bNo\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Nay'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bYeah\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Aye'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\byeah\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'aye'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bare\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'be'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bDrupalists\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Bucaneers'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bthere\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'thar'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%b\bnot\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'nay'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bdesign\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'bounty'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bonline\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'on the plank'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'/and\b/'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;an'&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'/ious\b/'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;i'us&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;%\bdon't know\b%&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;dinna&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;%\bdidn't know\b%&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;did nay know&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;%\bhadn't\b%&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;ha'nae&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;%\bdidn't\b%&quot;</span><span style="color: #339933;">=&gt;</span>  <span style="color: #0000ff;">&quot;di'nae&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;%\bwasn't\b%&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;weren't&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;%\bhaven't\b%&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;ha'nae&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bfor\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'fer'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bbetween\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'betwixt'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\baround\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;aroun'&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bto\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;t'&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;%\bit's\b%&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;'tis&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bwoman\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'wench'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bwomen\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'wenches'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\blady\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'wench'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bwife\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'lady'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bgirl\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'lass'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bgirls\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'lassies'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bguy\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'lubber'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bman\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'lubber'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bfellow\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'lubber'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bdude\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'lubber'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bboy\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'lad'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bboys\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'laddies'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bchildren\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'little sandcrabs'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bkids\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'minnows'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bhim\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'that scurvey dog'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bher\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'that comely wench'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bhim\.\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'that drunken sailor'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bHe\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'The ornery cuss'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bShe\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'The winsome lass'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;%\bhe's\b%&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'he be'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;%\bshe's\b%&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'she be'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bwas\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;were bein'&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bHey\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Avast'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bher\.\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'that lovely lass'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bfood\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'chow'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bmoney\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'dubloons'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bdollars\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'pieces of eight'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bcents\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'shillings'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\broad\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'sea'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\broads\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'seas'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bstreet\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'river'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bstreets\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'rivers'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bhighway\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'ocean'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bhighways\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'oceans'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\binterstate\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'high sea'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bprobably\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'likely'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bidea\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'notion'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bcar\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'boat'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bcars\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'boats'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\btruck\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'schooner'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\btrucks\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'schooners'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bSUV\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'ship'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bairplane\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'flying machine'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bjet\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'flying machine'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bmachine\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'contraption'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bdriving\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'sailing'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bunderstand\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'reckon'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bdrive\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'sail'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'%\bdied\b%'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'snuffed it'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'/ing\b/'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;in'&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'/ings\b/'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;in's&quot;</span><span style="color: #339933;">,</span>
      <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$patterns</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$pattern_search</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$pattern_replace</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pattern_search</span><span style="color: #339933;">,</span> <span style="color: #000088;">$pattern_replace</span><span style="color: #339933;">,</span> <span style="color: #000088;">$content</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;">$content</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
add_shortcode<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'pirate'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'pirate_talk_shortcode'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Wrap any content you want to translate in <code>[pirate][/pirate]</code>.</p>
<h2>The sky&#8217;s the limit!</h2>
</p>
<p>Usin' shortcodes ye can write yer own functions an' output pretty much anythin' ye want. Go play!</p>
<p>
<p>The wordpress documentation for the <a href="http://codex.wordpress.org/Shortcode_API">shortcode API can be found here</a>. <a href="http://www.smashingmagazine.com/2009/02/02/mastering-wordpress-shortcodes/">Smashing magazine</a> also had a useful post on shortcodes you can read.</p>
]]></content:encoded>
			<wfw:commentRss>http://blue-anvil.com/archives/8-fun-useful-shortcode-functions-for-wordpress/feed/</wfw:commentRss>
		<slash:comments>25</slash:comments>
		</item>
		<item>
		<title>Including CSS &amp; JavaScript in wordpress posts using Custom Fields</title>
		<link>http://blue-anvil.com/archives/including-css-javascript-in-wordpress-posts-using-custom-fields/</link>
		<comments>http://blue-anvil.com/archives/including-css-javascript-in-wordpress-posts-using-custom-fields/#comments</comments>
		<pubDate>Wed, 28 Jan 2009 19:20:55 +0000</pubDate>
		<dc:creator>Mike Jolley</dc:creator>
				<category><![CDATA[Wordpress & plugins]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[custom fields]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[posts]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[Wordpress development techniques]]></category>

		<guid isPermaLink="false">http://blue-anvil.com/?p=310</guid>
		<description><![CDATA[If you have ever tried to include code in a wordpress post you may have gone through hell trying to get it to output correctly; WordPress&#8217; built in functions for formatting text (autop and texturize) mangle your code making it non-functional. I&#8217;ve tried many solutions in the past, such as disabling wordpress&#8217; formatting functions, however, [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone size-full wp-image-324" title="custom_code" src="http://blue-anvil.com/wp-content/uploads/2009/01/custom_code.gif" alt="custom_code" width="488" height="100" /></p>
<p>If you have ever tried to include code in a wordpress post you may have gone through hell trying to get it to output correctly; WordPress&#8217; built in functions for formatting text (autop and texturize) mangle your code making it non-functional.</p>
<p>I&#8217;ve tried many solutions in the past, such as disabling wordpress&#8217; formatting functions, however, this is not ideal  -especially if you rely on them to clean up your text and properly encode characters. On top of that its not even valid to include certain things such as CSS in the body of a xHTML document.</p>
<p>The solution? Custom fields. This post will show you how.</p>
<p><span id="more-310"></span>First we need to prepare your template. Open your templates header.php file and insert the following php code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>is_single<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</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;">$css</span> <span style="color: #339933;">=</span> get_post_meta<span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ID</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'custom_css'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</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;">'&lt;style type=&quot;text/css&quot;&gt;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$css</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;/style&gt;'</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$js</span> <span style="color: #339933;">=</span> get_post_meta<span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ID</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'custom_js'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</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;">'&lt;script type=&quot;text/javascript&quot;&gt;
    jQuery.noConflict();
    (function($) {
      '</span><span style="color: #339933;">.</span><span style="color: #000088;">$js</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'
    })(jQuery);
    &lt;/script&gt;'</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>When a post is being viewed (single page) this code will check if it contains the custom fields and display them. Notice in this case I&#8217;m using jQuery noConflict; this part is optional (you may not be using jQuery at all) so if you don&#8217;t want jQuery just use:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>is_single<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</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;">$css</span> <span style="color: #339933;">=</span> get_post_meta<span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ID</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'custom_css'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</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;">'&lt;style type=&quot;text/css&quot;&gt;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$css</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;/style&gt;'</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$js</span> <span style="color: #339933;">=</span> get_post_meta<span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ID</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'custom_js'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</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;">'&lt;script type=&quot;text/javascript&quot;&gt;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$js</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;/script&gt;'</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Whist writing a post, add two custom fields: custom_js and custom_css. Fill these with any example code you need to include in the post.</p>
<div id="attachment_313" class="wp-caption aligncenter" style="width: 310px"><a rel="lightbox" href="http://blue-anvil.com/wp-content/uploads/2009/01/screen-capture.png"><img class="size-medium wp-image-313" title="Adding custom css" src="http://blue-anvil.com/wp-content/uploads/2009/01/screen-capture-300x48.png" alt="Adding custom css" width="300" height="48" /></a><p class="wp-caption-text">Adding custom css</p></div>
<p>That&#8217;s it! The code will now show in the page head, perfect for CSS and JS example code.</p>
<div class="examplebox">
<p>And as you can see we can include additional styles in our posts.</p></div>
<p>I hope this technique comes in handy.</p>
]]></content:encoded>
			<wfw:commentRss>http://blue-anvil.com/archives/including-css-javascript-in-wordpress-posts-using-custom-fields/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>WordPress development techniques #2 &#8211; Fetching RSS feeds with wordpress</title>
		<link>http://blue-anvil.com/archives/wordpress-development-techniques-2-fetching-rss-feeds-with-wordpress/</link>
		<comments>http://blue-anvil.com/archives/wordpress-development-techniques-2-fetching-rss-feeds-with-wordpress/#comments</comments>
		<pubDate>Wed, 27 Jun 2007 23:27:58 +0000</pubDate>
		<dc:creator>Mike Jolley</dc:creator>
				<category><![CDATA[Wordpress & plugins]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[Wordpress development techniques]]></category>

		<guid isPermaLink="false">http://82.110.105.91/blue-anvil.co.uk/?p=84</guid>
		<description><![CDATA[As promised last week, this week we are looking at fetching RSS feeds with Magpie RSS. Magpie is an PHP based RSS feed parser; I.E. It fetches RSS feeds to display in other places, which gives it many uses. Unknown to many, wordpress has the magpie RSS parser built in, this article will teach you [...]]]></description>
			<content:encoded><![CDATA[<p><img class="floatleft" style="float: left; margin: 4px 4px 4px 0;" src="http://blue-anvil.com/img/fetch.gif" alt="Fetching RSS feeds" />As promised last week, this week we are looking at fetching RSS feeds with <a href="http://magpierss.sourceforge.net/">Magpie RSS</a>.</p>
<p>Magpie is an PHP based RSS feed parser; I.E. It fetches RSS feeds to display in other places, which gives it many uses.</p>
<p>Unknown to many, wordpress has the magpie RSS parser built in, this article will teach you how to use it, and also how to fetch and show someones <a href="http://del.icio.us/">del.icio.us</a> bookmarks!</p>
<p><span id="more-84"></span></p>
<h2>Fetching feeds in wordpress</h2>
<p>WordPress contains two functions for fetching feeds:</p>
<ol>
<li><code>fetch_rss</code> -This function fetches and parses an RSS feed.</li>
<li><code>wp_rss</code> &#8211; This function fetches and parses an RSS feed, then displays the results in an unordered list.</li>
</ol>
<p>These functions use magpie for parsing the RSS, and <a href="http://sourceforge.net/projects/snoopy">Snoopy</a> for retrieving the RSS.</p>
<blockquote><p>Uses the MagpieRSS and RSSCache (http://magpierss.sourceforge.net/) functions for parsing and automatic caching and the Snoopy HTTP client (http://sourceforge.net/projects/snoopy/) for the actual retrieval.</p></blockquote>
<p><!--adsense--></p>
<h3>Including Magpie in a template or plug in</h3>
<p>Magpie RSS is built into wordpress&#8217; rss-functions.php file (or rss.php in newer versions). It contains the functions needed to fetch feeds, so it needs to first be included:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Include the RSS functions. Older versions of wordpress could use include_once (ABSPATH . WPINC . '/rss-functions.php') or include_once (ABSPATH . WPINC . '/rss.php');;</span>
<span style="color: #b1b100;">include_once</span><span style="color: #009900;">&#40;</span>ABSPATH <span style="color: #339933;">.</span> WPINC <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/rss-functions.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h3>Fetching the feed with fetch_rss</h3>
<p>Next lets tell wordpress what feed we want to parse, and put it to work! If you want to get the results and output them yourself, use <code>fetch_rss</code>.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Fetch a feed</span>
<span style="color: #000088;">$feed</span> <span style="color: #339933;">=</span> fetch_rss<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://yourfeedaddress&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>To limit you results you can slice the array of results:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$items</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_slice</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$feed</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>items<span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$maxitems</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>After that, use a loop to go through the array contents and output the results (see the del.icio.us bookmarks example further down for more detail on this).</p>
<h3>Fetching the feed with wp_rss</h3>
<p>If you want to output the results in an unordered list right away, use <code>wp_rss</code>.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Fetch a feed, replace $limit with the max results you want</span>
wp_rss<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://yourfeedaddress&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$limit</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h2>Fetching del.icio.us bookmarks &#8211; example</h2>
<p>The following code gets my last 5 del.icio.us bookmarks, and outputs them to the page.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$feed</span> <span style="color: #339933;">=</span> fetch_rss<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://del.icio.us/rss/mikejolley&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$maxitems</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$items</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_slice</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$feed</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>items<span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$maxitems</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// Output the results!</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;">$items</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;">'&lt;ul&gt;'</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$items</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;li&gt;'</span><span style="color: #339933;">;</span>
                  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;a href=&quot;&lt;a href=&quot;'</span><span style="color: #339933;">;</span>
                  <span style="color: #000088;">$item</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'link'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'&amp;'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'&amp;amp;'</span><span style="color: #339933;">,</span><span style="color: #000088;">$item</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'link'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&quot;&gt;&lt;/a&gt;'</span><span style="color: #339933;">;</span>
                  <span style="color: #666666; font-style: italic;">// If i've written a description, output it</span>
                  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$item</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'description'</span><span style="color: #009900;">&#93;</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: #000088;">$item</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'description'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                  <span style="color: #009900;">&#125;</span>
                  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;/li&gt;'</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;/ul&gt;'</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Neat huh?</p>
<h2>Help! My feeds contain weird symbols!</h2>
<p>If the character encoding of fetched feeds is ISO-8859-1 and you want something different (like me), such as UTF-8, you can set the output encoding by including the following:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;MAGPIE_OUTPUT_ENCODING&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;UTF-8&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>That line of code simply sets the output of the fetched feed to be in UTF-8 encoding. Handy.</p>
<h2>More about wordpress rss functions</h2>
<ul>
<li><a href="http://codex.wordpress.org/Function_Reference/wp_rss">http://codex.wordpress.org/Function_Reference/wp_rss</a></li>
<li><a href="http://codex.wordpress.org/Function_Reference/fetch_rss">http://codex.wordpress.org/Function_Reference/fetch_rss</a></li>
<li><a href="http://magpierss.sourceforge.net/">http://magpierss.sourceforge.net/</a></li>
</ul>
<p><!--adsense--></p>
]]></content:encoded>
			<wfw:commentRss>http://blue-anvil.com/archives/wordpress-development-techniques-2-fetching-rss-feeds-with-wordpress/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>WordPress development techniques #1 &#8211; Running custom queries using the &#8216;wpdb&#8217; class</title>
		<link>http://blue-anvil.com/archives/wordpress-development-techniques-1-running-custom-queries-using-the-wpdb-class/</link>
		<comments>http://blue-anvil.com/archives/wordpress-development-techniques-1-running-custom-queries-using-the-wpdb-class/#comments</comments>
		<pubDate>Thu, 21 Jun 2007 15:48:56 +0000</pubDate>
		<dc:creator>Mike Jolley</dc:creator>
				<category><![CDATA[Wordpress & plugins]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[Wordpress development techniques]]></category>

		<guid isPermaLink="false">http://82.110.105.91/blue-anvil.co.uk/?p=83</guid>
		<description><![CDATA[In this first edition of &#8220;WordPress development techniques&#8221; I will be talking about one of the most useful classes available for use in wordpress: wpdb. The wpdb class (WordPress DataBase class) is based on the ezSQL class, and handles database connections and queries. Why use it? You can make all sorts of custom queries, and [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://blue-anvil.com/img/wpdb.gif" class="floatleft" style="float:left;margin:4px 4px 4px 0;" alt="wpdb class" />In this first edition of &#8220;WordPress development techniques&#8221; I will be talking about one of the most useful <a href="http://uk2.php.net/manual/en/language.oop.php" title="PHP classes">classes</a> available for use in wordpress: wpdb.</p>
<p>The wpdb class (WordPress DataBase class) is based on the <a href="http://www.jvmultimedia.com/portal/node/6">ezSQL</a> class, and handles database connections and queries.</p>
<p>Why use it? You can make all sorts of custom queries, and pull information from the wordpress database, including posts! Read on for more information on this useful class&#8230;</p>
<p><span id="more-83"></span></p>
<h2>What can I use wpdb for?</h2>
<p>The wpdb class can be used for running queries on the wordpress database, giving you access to ALL wordpress tables, including posts and categories.</p>
<p>An example use, and the example we will focus on in this article, is pulling <strong>recent posts</strong> from the database, for example, on a <strong>homepage</strong>.</p>
<p><!--adsense--></p>
<h2>Using wpdb</h2>
<h3>Where can wpdb be used?</h3>
<p>Wpdb can be used in templates and plugins within wordpress, it can also be used in web pages that are not part of wordpress if you include the following files:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">include_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'wp-config.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">include_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'wp-includes/wp-db.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>If your using the code from a plugin or template file, these files are already included.</p>
<h3>Getting started with wpdb</h3>
<p>When using wpdb, it is a good idea to first set its scope to global:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$wpdb</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This will allow you to use the wpdb class in your code. Wpdb automatically handles the database connection, so you can immediately start running queries.</p>
<h3>Running query&#8217;s</h3>
<p>To run a query you can call one of the following functions within wpdb (replacing &#8216;query&#8217; with your sql code):</p>
<ol>
<li><code>$wpdb-&gt;query('query');</code> &#8211; Runs any query, used for inserts, updates, deletes etc.</li>
<li><code>$wpdb-&gt;get_var('query',column_offset,row_offset); </code>- Get a single variable</li>
<li><code>$wpdb-&gt;get_row('query', output_type, row_offset);</code> &#8211; Get a single row</li>
<li><code>$wpdb-&gt;get_col('query',column_offset);</code> &#8211; Get a single column as a dimensional array</li>
<li><code>$wpdb-&gt;get_results('query', output_type);</code> &#8211; Get generic results</li>
</ol>
<p>The returned results&#8217; type can be changed by setting &#8216;output_type&#8217;, this can be:</p>
<ol>
<li><code>OBJECT</code> &#8211; This is the default output, and returns an object.</li>
<li><code>ARRAY_A</code> &#8211; This returns the results as an associative array.</li>
<li><code>ARRAY_N</code> &#8211; This returns the results as a numbered array.</li>
</ol>
<p>I tend to only use the &#8216;object&#8217; type, this is down to personal preference.</p>
<h3>Get table names easily</h3>
<p>wpdb provides an easy method to get wordpress table names, by using the following variables within queries:</p>
<ol>
<li><code>$wpdb-&gt;posts</code> &#8211; The post table that stores posts and pages</li>
<li><code>$wpdb-&gt;postmeta </code> &#8211; The table which stores custom field data</li>
<li><code>$wpdb-&gt;categories </code> &#8211; The category table</li>
<li><code>$wpdb-&gt;post2cat </code> &#8211; The post2cat table which stores the id&#8217;s of posts and categories that are linked</li>
<li><code>$wpdb-&gt;comments</code> &#8211; The comments table</li>
<li><code>$wpdb-&gt;users </code> &#8211; The table of users and user details</li>
<li><code>$wpdb-&gt;links</code> &#8211; Stores links</li>
<li><code>$wpdb-&gt;link2cat</code> &#8211; The link2cat table which stores the id&#8217;s of links and categories that are linked</li>
<li><code>$wpdb-&gt;options</code> &#8211; The table of wordpress options</li>
</ol>
<h3>Keeping data safe for insertion</h3>
<p>When inserting data into the database, it shoukd <em>always</em> be escaped to stop people hacking your site, wpdb offers a handy little function for doing this &#8211; <code>$wpdb-&gt;escape</code></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$safe_string</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$wpdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">escape</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$unsafe_string</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The above code runs the escape function on an unsafe string, and puts the result into the $safe_string varible. This can then be inserted into the database using wpdb safely.</p>
<h3>Other useful functions</h3>
<p>Some other useful functions wpdb has are listed below.</p>
<ol>
<li><code>$wpdb-&gt;show_errors();</code> &#8211; Echoes mysql errors on the page</li>
<li><code>$wpdb-&gt;hide_errors(); </code> &#8211; Hides mysql errors on the page</li>
<li><code>$wpdb-&gt;print_error();</code> &#8211; Prints mysql errors on the page</li>
<li><code>$wpdb-&gt;flush();</code> &#8211; Flushes the result cache</li>
</ol>
<h3>Useful variables</h3>
<p>After running a query, some useful variables become available:</p>
<ol>
<li>
<code>$wpdb-&gt;num_rows</code> &#8211; Count of the number of rows returned by the last query (useful in SELECT queries)</li>
<li><code>$wpdb-&gt;insert_id</code> &#8212; ID of the AUTO_INCREMENT value of the last query (useful in INSERT queries)</li>
<li><code>$wpdb-&gt;rows_affected</code> &#8211; Count of the number of rows affected by the last query (useful in INSERT, UPDATE and DELETE queries)</li>
<li><code>$wpdb-&gt;last_query </code> &#8211; The last query ran</li>
<li><code>$wpdb-&gt;last_results </code> &#8211; The results of the last query ran</li>
</ol>
<p><!--adsense--></p>
<h2>Recent posts example</h2>
<p>So I&#8217;ve covered the basics, now lets put wpdb into action. Lets say we have a homepage on a wordpress blog, the template for this page is home.php.</p>
<p>On this page we want to show the latest post, plus a list of recent posts (like the setup here at Blue Anvil).</p>
<h3>The normal loop</h3>
<p>To show the latest entry, just use the normal wordpress loop, as well as <code>query_posts</code> to limit it to one post.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
        query_posts<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'showposts=1'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Return only 1 post</span>
	<span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$more</span><span style="color: #339933;">;</span> <span style="color: #000088;">$more</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>      <span style="color: #666666; font-style: italic;">// set $more to 0 in order to only get the first part of the post</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>have_posts<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span>have_posts<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> the_post<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>
&nbsp;
			&lt;div class=&quot;post&quot; id=&quot;post-<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_ID<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>&quot;&gt;
				&lt;h2&gt;&lt;a href=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_permalink<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>&quot; rel=&quot;bookmark&quot; title=&quot;Permanent Link to <span style="color: #000000; font-weight: bold;">&lt;?php</span> the_title<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>&quot;&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_title<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>&lt;/a&gt;&lt;/h2&gt;
				&lt;small&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_time<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'F jS, Y'</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> &lt;!-- by <span style="color: #000000; font-weight: bold;">&lt;?php</span> the_author<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> --&gt;&lt;/small&gt;
&nbsp;
				&lt;div class=&quot;entry&quot;&gt;
					<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_content<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Read the rest of this entry &amp;raquo;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
				&lt;/div&gt;
&nbsp;
				&lt;p class=&quot;postmetadata&quot;&gt;Posted in <span style="color: #000000; font-weight: bold;">&lt;?php</span> the_category<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">', '</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> | <span style="color: #000000; font-weight: bold;">&lt;?php</span> edit_post_link<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Edit'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">' | '</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>  <span style="color: #000000; font-weight: bold;">&lt;?php</span> comments_popup_link<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'No Comments &amp;#187;'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'1 Comment &amp;#187;'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'% Comments &amp;#187;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/p&gt;
			&lt;/div&gt;
&nbsp;
		<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endwhile</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
		&lt;div class=&quot;navigation&quot;&gt;
			&lt;div class=&quot;alignleft&quot;&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> next_posts_link<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'&amp;laquo; Previous Entries'</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/div&gt;
			&lt;div class=&quot;alignright&quot;&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> previous_posts_link<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Next Entries &amp;raquo;'</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/div&gt;
		&lt;/div&gt;
&nbsp;
	<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">else</span> <span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
		&lt;h2 class=&quot;center&quot;&gt;Not Found&lt;/h2&gt;
		&lt;p class=&quot;center&quot;&gt;Sorry, but you are looking for something that isn't here.&lt;/p&gt;
		<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">include</span> <span style="color: #009900;">&#40;</span>TEMPLATEPATH <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;/searchform.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endif</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<h3>The recent posts</h3>
<p>Now lets use wpdb to get recent posts!</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">&lt;h2&gt;Recently posted&lt;/h2&gt;
    &lt;ul id=&quot;recent&quot;&gt;
		<span style="color: #000000; font-weight: bold;">&lt;?php</span>
			<span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$wpdb</span><span style="color: #339933;">;</span>   <span style="color: #666666; font-style: italic;">// Give wpdb with global scope</span>
&nbsp;
                        <span style="color: #666666; font-style: italic;">// This query selects all posts that are published, orders them by date, offsets the first post by 1 (so we dont show the main post in               the list) and limits it to 3 (so we only show 3 recent posts)</span>
			<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'SELECT DISTINCT * FROM '</span><span style="color: #339933;">.</span><span style="color: #000088;">$wpdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">posts</span><span style="color: #339933;">.</span><span style="color: #0000ff;">' AS POSTS
                                 WHERE POSTS.post_type=&quot;post&quot;
                                 AND POSTS.post_status=&quot;publish&quot;
                                 ORDER BY POSTS.post_date DESC
                                 LIMIT 1,3;'</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #000088;">$posts</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$wpdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_results</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// Run our query, getting results as an object</span>
&nbsp;
				<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;">$posts</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// If the query returned something</span>
					 <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$posts</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$post</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>  <span style="color: #666666; font-style: italic;">// Loop though our results!</span>
&nbsp;
						<span style="color: #000088;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">post_date</span> <span style="color: #339933;">=</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;F j, Y&quot;</span><span style="color: #339933;">,</span><span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">post_date</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// Format the date</span>
&nbsp;
                                                 <span style="color: #666666; font-style: italic;">// Get excerpt, or make our own from content</span>
						 <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">post_excerpt</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
							<span style="color: #000088;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">post_excerpt</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; &quot;</span><span style="color: #339933;">,</span><span style="color: #990000;">strrev</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strip_tags</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</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: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">175</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
							<span style="color: #000088;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">post_excerpt</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strrev</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">post_excerpt</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
							<span style="color: #000088;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">post_excerpt</span><span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot; [..]&quot;</span><span style="color: #339933;">;</span>
						 <span style="color: #009900;">&#125;</span>
&nbsp;
                                                <span style="color: #666666; font-style: italic;">// Output the row</span>
						<span style="color: #b1b100;">print</span><span style="color: #009900;">&#40;</span>
							<span style="color: #0000ff;">'&lt;li&gt;&lt;a href=&quot;'</span><span style="color: #339933;">.</span>get_permalink<span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</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: #0000ff;">'&quot;&gt;
							&lt;strong&gt;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">post_title</span><span style="color: #339933;">.</span><span style="color: #0000ff;">' - '</span><span style="color: #339933;">.</span><span style="color: #000088;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">post_date</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;/strong&gt;&lt;br/&gt;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">post_excerpt</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;/a&gt;&lt;/li&gt;'</span>
						<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					 <span style="color: #009900;">&#125;</span>
				<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">?&gt;</span>
    &lt;/ul&gt;</pre></td></tr></table></div>

<p>You will notice in the above example, I got the post data by using <code>$post-&gt;</code>, there is another way of doing this which enables you to use the normal wordpress template functions (e.g. <code>the_content();</code>). To allow this, add the following code within the foreach loop:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"> setup_postdata<span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>You can now use functions like <code>the_content();</code>, <code>the_excerpt();</code> and so on instead of <code>$post-&gt;post_content</code> etc.</p>
<h2>More information on wpdb</h2>
<ul>
<li><a href="http://codex.wordpress.org/Function_Reference/wpdb_Class">http://codex.wordpress.org/Function_Reference/wpdb_Class</a></li>
<li><a href="http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query">http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query</a></li>
</ul>
<h2>Next week&#8230;</h2>
<p>Next week we will be looking at wordpress&#8217; built in RSS feed grabbing features, and create our own delicious RSS feed aggregator. See you then!</p>
]]></content:encoded>
			<wfw:commentRss>http://blue-anvil.com/archives/wordpress-development-techniques-1-running-custom-queries-using-the-wpdb-class/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

