<?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>Digging into WordPress &#187; shortcode</title>
	<atom:link href="http://digwp.com/tag/shortcode/feed/" rel="self" type="application/rss+xml" />
	<link>http://digwp.com</link>
	<description>Take your WordPress skills to the next level.</description>
	<lastBuildDate>Thu, 09 Feb 2012 19:03:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Shortcode for Includes</title>
		<link>http://digwp.com/2010/06/shortcode-for-includes/</link>
		<comments>http://digwp.com/2010/06/shortcode-for-includes/#comments</comments>
		<pubDate>Mon, 28 Jun 2010 09:08:19 +0000</pubDate>
		<dc:creator>Chris Coyier</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[content]]></category>
		<category><![CDATA[include]]></category>
		<category><![CDATA[shortcode]]></category>

		<guid isPermaLink="false">http://digwp.com/?p=2365</guid>
		<description><![CDATA[One thing that WordPress doesn&#8217;t have the ability to do &#8220;out-of-the-box&#8221; is do includes, in the sense of including the content of one post into the content of another post directly in the post editor. For the umpteenth time around here, shortcodes to the rescue! This issue came up while my co-worker Tim at Wufoo [...]]]></description>
			<content:encoded><![CDATA[<p>One thing that WordPress doesn&#8217;t have the ability to do &#8220;out-of-the-box&#8221; is do <em>includes</em>, in the sense of including the content of one post into the content of another post directly in the post editor. For the <a href="http://digwp.com/tag/shortcode/">umpteenth time</a> around here, shortcodes to the rescue! </p>
<p><span id="more-2365"></span></p>
<p>This issue came up while my co-worker Tim at <a href="http://wufoo.com">Wufoo</a> was documenting parts of the latest <a href="http://wufoo.com/docs/api/v3/">Wufoo API</a>. Some of the API pages have areas on them that are exactly the same as other pages, for example, a chunk of navigation that links to other pages of documentation.</p>
<p>One possible way to deal with this is to make a special template just for these pages and include that chunk inside that template. This solution edges on the issue of template-bloat which I&#8217;ve been thinking a lot about lately. Creating a new template every time you need one little change is solving the problem with a sledgehammer rather than a scalpel. </p>
<p>The ideal solution is just to make a shortcode. You pass the ID of the post (in our case, page) that you want to include and the shortcode is replaced with that content. This is the usage, where XXXX would be the ID of the post:</p>
<pre><code>[digwp_include postidparam=XXXX]</code></pre>
<p>To make it work, we&#8217;ll add a fairly simple function to the functions.php file in our theme. The function will take the parameter, run a query for it, and return back the content if it finds any:</p>
<pre><code>function digwp_includeContentShortcode($atts) {
  
  $thepostid = intval($atts[postidparam]);
  $output = '';

  query_posts("p=$thepostid&amp;post_type=page");
  if (have_posts()) : while (have_posts()) : the_post();
    $output .= get_the_content($post-&gt;ID);
  endwhile; else:
    // failed, output nothing
  endif;
  wp_reset_query();

  return $output;

}

// USAGE
// In the post content, you can use [digwp_include postidparam="1234"]
// "1234" would be the WordPress ID of the Page you are trying to include

add_shortcode("digwp_include", "digwp_includeContentShortcode");</code></pre>
<p>Now you can publish small modules of content, and include them on any Post/Page that needs them! I&#8217;d probably create a Page on your site called like &#8220;Includes&#8221; or &#8220;Modules&#8221; and post them as Pages with that as the Parent Page. That way you don&#8217;t clutter up the root and they all stay organized together. </p>
<p>Check out this graphic (click for full size), which hopefully will drive home the idea:</p>
<p><a href="http://digwp.com/wp-content/blog-images/20100621-qgt1km86je73ci7ikwjpp3bexw.png"><img src="http://digwp.com/wp-content/blog-images/_20100621-qgt1km86je73ci7ikwjpp3bexw.png" width="570" height="442" alt="" title=""  /></a></p>
<p>Random notes: </p>
<ul>
<li>This is similar to the <a href="http://digwp.com/2010/01/custom-query-shortcode/">custom loop shortcode</a> I previously published.</li>
<li>This may be good territory for a plugin rather than functions.php code as I have done.</li>
<li>Notice the function is <a href="http://www.andrewnacin.com/2010/05/11/in-wordpress-prefix-everything/">properly prefixed</a>.</li>
</ul>
<hr />
<p><small>© 2010 <a href="http://digwp.com">Digging into WordPress</a> | <a href="http://digwp.com/2010/06/shortcode-for-includes/">Permalink</a> | <a href="http://digwp.com/2010/06/shortcode-for-includes/#comments">28 comments</a> | Add to <a href="http://del.icio.us/post?url=http://digwp.com/2010/06/shortcode-for-includes/&title=Shortcode for Includes">del.icio.us</a> | Post tags: <a href="http://digwp.com/tag/content/" rel="tag">content</a>, <a href="http://digwp.com/tag/include/" rel="tag">include</a>, <a href="http://digwp.com/tag/shortcode/" rel="tag">shortcode</a><br/></small></p>]]></content:encoded>
			<wfw:commentRss>http://digwp.com/2010/06/shortcode-for-includes/feed/</wfw:commentRss>
		<slash:comments>28</slash:comments>
		</item>
		<item>
		<title>Add Private Content to Posts via Shortcode</title>
		<link>http://digwp.com/2010/05/private-content-posts-shortcode/</link>
		<comments>http://digwp.com/2010/05/private-content-posts-shortcode/#comments</comments>
		<pubDate>Wed, 05 May 2010 07:25:10 +0000</pubDate>
		<dc:creator>Jeff Starr</dc:creator>
				<category><![CDATA[Admin]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[shortcode]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[tricks]]></category>

		<guid isPermaLink="false">http://digwp.com/?p=2023</guid>
		<description><![CDATA[Recently, WPRecipes posted an incredibly useful technique that uses a shortcode to add private content to blog posts. This functionality makes it easy to manage leftover data, miscellaneous notes and other communication by keeping everything together with its corresponding post. Consolidating information like this helps to streamline flow and organization into the future. Combine the [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, <a href="http://www.wprecipes.com/">WPRecipes</a> posted an <a href="http://www.wprecipes.com/add-private-notes-to-your-wordpress-blog-posts" title="Add private notes to your WordPress blog posts">incredibly useful technique</a> that uses a <strong>shortcode</strong> to add <strong>private content</strong> to blog posts. This functionality makes it easy to manage leftover data, miscellaneous notes and other communication by keeping everything together with its corresponding post. Consolidating information like this helps to <strong>streamline flow and organization</strong> into the future.</p>
<blockquote><p>Combine the power and elegance of shortcodes with the convenience of private post content</p></blockquote>
<p>For those unfamiliar with shortcodes, they are basically short strings of bracketed text used in post content to run functions and output content. Since WordPress 2.5, WordPress&rsquo; <a href="http://codex.wordpress.org/Shortcode_API">Shortcode API</a> provides a set of functions for creating <strong>macro codes</strong>. Here are some hypothetical examples:</p>
<ul>
<li><code>[homepage]</code> &ndash; displays your blog&rsquo;s home <acronym title="Uniform Resource Locator">URL</acronym></li>
<li><code>[navigation]</code> &ndash; displays a list of links to your categories</li>
<li><code>[copyright]</code> &ndash; displays your copyright information</li>
</ul>
<p><span id="more-2023"></span></p>
<p>For shortcodes to work, you just add a <a href="http://digwp.com/2010/03/wordpress-functions-php-template-custom-functions/" title="WordPress functions.php Template with 15 Essential Custom Functions">custom function</a> to your theme&rsquo;s <a href="http://digwp.com/2010/04/wordpress-custom-functions-php-template-part-2/" title="WordPress Custom functions.php Template, Part 2">functions.php file</a>. Once that&rsquo;s in place, you simply type the shortcode text-snippet anywhere in your post content. When such posts are viewed on your blog, the shortcode will run its function and output content, filter posts, or whatever else you&rsquo;ve rigged it to do. Shortcodes can really <strong>simplify and speed up</strong> content creation.</p>
<p>You can create shortcodes for just about anything, including <a href="http://digwp.com/2009/09/easy-shortcode-permalinks/" title="Easy Shortcode Permalinks">custom permalinks</a>, <a href="http://digwp.com/2010/04/call-widget-with-shortcode/" title="Call a Widget with a Shortcode">calling widgets</a>, <a href="http://digwp.com/2010/01/google-maps-shortcode/" title="Google Maps Shortcode">Google Maps</a>, and even <a href="http://digwp.com/2010/01/custom-query-shortcode/" title="Custom Query Shortcode: Run a Loop inside Any Post/Page">custom queries</a>. In this post, we&rsquo;re combining the power and elegance of shortcodes with the convenience of <strong>private post content</strong>. And the best part is that it&rsquo;s <em>so</em> easy..</p>
<p><!--more--></p>
<h3>Step 1: Add some code to your functions.php file</h3>
<p>Slap the following code into your active theme&rsquo;s <code>functions.php</code> file:</p>
<pre><code>// add private content via shortcode
function private_content($atts, $content = null) {
	if (current_user_can('create_users'))
		return '&lt;div class="private-content"&gt;' . $content . '&lt;/div&gt;';
	return '';
}
add_shortcode('private', 'private_content');</code></pre>
<p>What we&rsquo;re doing here is creating a function that will recognize a shortcode named &ldquo;<code>[private]</code>&rdquo; and only display its contents if the user is <strong>logged in as an Administrator</strong>. For everyone else, the private content remains just that &ndash; <em>completely private</em>.</p>
<h3>Step 2: Be a private-content shortcode ninja</h3>
<p>Once you have the custom function in place, you are ready to add some private content to any of your posts. Here is an example of how to use the shortcode:</p>
<pre><code>[private]
Note to self: this post contains some private content!
[/private]</code></pre>
<p>Then, when the user is logged in as Admin, this private content will be displayed wrapped with the following markup:</p>
<pre><code>&lt;div class="private-content"&gt;
Note to self: this post contains some private content!
&lt;/div&gt;</code></pre>
<p>By using the &ldquo;<code>private-content</code>&rdquo; class, you can style your private content with a little <acronym title="Cascading Style Sheets">CSS</acronym> to make it all sweet&nbsp;;)</p>
<h3>Now in plugin flavor!</h3>
<p>Since posting the article, we have seen two different plugins for this &ldquo;private-content shortcode&rdquo; functionality. After reading this post, <a href="http://www.aldolat.it/" title="Ubuntu block notes">Aldo Latino</a> whipped up <a href="http://digwp.com/plugins/ubn-private-content.zip" title="WordPress Plugin: UBN Private Content">UBN Private Content</a>, which takes it to the next level by using shortcode attributes to enable private content for <em>any</em> user-permission level (i.e., not just Admin). <a href="http://digwp.com/plugins/ubn-private-content.zip" title="Download UBN Private Content">Download here!</a> </p>
<p>There is also the <a href="http://dicaswp.com/2010/04/23/esconder-texto-para-leitores-nao-registados/" title="Esconder texto para leitores nÃ£o registados">The Esconder Shortcode Plugin</a> is in Portuguese, and enables private content that is viewable by <em>any</em> logged-in users (i.e., not just Admin). See the linked post for download.</p>
<p>As <a href="#comment-4540" title="Read comment">Barry points out</a> in the comments, using a plugin for private post content is a better idea if you ever plan on switching themes. Plugins work <em>across themes</em>, so your private content will always stay private. Conversely, when implemented via your local theme&rsquo;s <code>functions.php</code> file, the shortcode function will keep your content private <em>only for the current theme</em>.</p>
<hr />
<p><small>© 2010 <a href="http://digwp.com">Digging into WordPress</a> | <a href="http://digwp.com/2010/05/private-content-posts-shortcode/">Permalink</a> | <a href="http://digwp.com/2010/05/private-content-posts-shortcode/#comments">28 comments</a> | Add to <a href="http://del.icio.us/post?url=http://digwp.com/2010/05/private-content-posts-shortcode/&title=Add Private Content to Posts via Shortcode">del.icio.us</a> | Post tags: <a href="http://digwp.com/tag/security/" rel="tag">Security</a>, <a href="http://digwp.com/tag/shortcode/" rel="tag">shortcode</a>, <a href="http://digwp.com/tag/tips/" rel="tag">tips</a>, <a href="http://digwp.com/tag/tricks/" rel="tag">tricks</a><br/></small></p>]]></content:encoded>
			<wfw:commentRss>http://digwp.com/2010/05/private-content-posts-shortcode/feed/</wfw:commentRss>
		<slash:comments>28</slash:comments>
		</item>
		<item>
		<title>Shortcodes in Widgets</title>
		<link>http://digwp.com/2010/03/shortcodes-in-widgets/</link>
		<comments>http://digwp.com/2010/03/shortcodes-in-widgets/#comments</comments>
		<pubDate>Wed, 10 Mar 2010 13:07:32 +0000</pubDate>
		<dc:creator>Chris Coyier</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[shortcode]]></category>
		<category><![CDATA[widgets]]></category>

		<guid isPermaLink="false">http://digwp.com/?p=1560</guid>
		<description><![CDATA[I had occasion the other day to run a shortcode inside of a text widget. You know shortcodes&#8230; we talk about them all the time. They are keywords in [square brackets] that do something special. Sometimes something really simple like returning a string (so you can have a global location to change that string) or [...]]]></description>
			<content:encoded><![CDATA[<p>I had occasion the other day to run a shortcode inside of a text widget. You know shortcodes&#8230; we talk about them <a href="http://digwp.com/tag/shortcode/">all the time</a>. They are keywords in [square brackets] that do something special. Sometimes something really simple like returning a string (so you can have a global location to change that string) or something complicated like call a plugin that does something fancy like build a photo gallery.</p>
<p>But alas&#8230;</p>
<div class="image-wrap">
<img src="http://digwp.com/wp-content/blog-images/shortcodeinwidget.png" width="371" height="222" alt="" title="" /><br />
This ain&#8217;t gonna work &#8220;out-of-the-box&#8221;
</div>
<p><span id="more-1560"></span></p>
<p>After tinkering with some far-too-complicated solutions, someone on Twitter helped me with a deliciously simple solution: Just add this to your functions.php file:</p>
<pre><code>add_filter('widget_text', 'do_shortcode');</code></pre>
<p>This will ensure that the text content of widgets is parsed for shortcodes and those shortcodes are ran. Awesome.</p>
<p>Stephanie Leary wrote a great article on <a href="http://sillybean.net/wordpress/content/using-shortcodes-everywhere/">Using Shortcodes Everywhere</a>, which covers this as well as using shortcodes in a bunch of other places: Comments, templates, excerpts, etc.</p>
<hr />
<p><small>© 2010 <a href="http://digwp.com">Digging into WordPress</a> | <a href="http://digwp.com/2010/03/shortcodes-in-widgets/">Permalink</a> | <a href="http://digwp.com/2010/03/shortcodes-in-widgets/#comments">4 comments</a> | Add to <a href="http://del.icio.us/post?url=http://digwp.com/2010/03/shortcodes-in-widgets/&title=Shortcodes in Widgets">del.icio.us</a> | Post tags: <a href="http://digwp.com/tag/shortcode/" rel="tag">shortcode</a>, <a href="http://digwp.com/tag/widgets/" rel="tag">widgets</a><br/></small></p>]]></content:encoded>
			<wfw:commentRss>http://digwp.com/2010/03/shortcodes-in-widgets/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Custom Query Shortcode: Run a Loop inside Any Post/Page</title>
		<link>http://digwp.com/2010/01/custom-query-shortcode/</link>
		<comments>http://digwp.com/2010/01/custom-query-shortcode/#comments</comments>
		<pubDate>Thu, 21 Jan 2010 14:43:59 +0000</pubDate>
		<dc:creator>Chris Coyier</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[functions]]></category>
		<category><![CDATA[loop]]></category>
		<category><![CDATA[shortcode]]></category>

		<guid isPermaLink="false">http://digwp.com/?p=1283</guid>
		<description><![CDATA[I had the occasion yesterday to have a page with a section on it where it would output a very specific set of other pages, which would need to change dynamically. What I could have done is built a special page template for this page, and inside that template run a query_posts() to get these [...]]]></description>
			<content:encoded><![CDATA[<p>I had the occasion yesterday to have a page with a section on it where it would output a very specific set of other pages, which would need to change dynamically. What I could have done is built a special page template for this page, and inside that template run a <code>query_posts()</code> to get these posts. But I wanted this page to remain editable from the admin. Besides, creating a special page template every time you need to do something like this is too cumbersome. WordPress is extensible enough to do better. </p>
<p>Hence, the Custom Query Shortcode!</p>
<p><span id="more-1283"></span></p>
<p>This is basically how I wanted it to work. You have a shortcode with one single parameter called &#8220;query&#8221;. (Remember, shortcodes are those things in brackets you can use in the body of Pages/Posts that do special stuff). In the &#8220;query&#8221; parameter, you literally enter exactly what you would enter for a query when using the <code>query_posts()</code> function. In other words:</p>
<pre><code>[loop query=""]</code></pre>
<p>You can pass whatever query you want to it. For example, let&#8217;s say you wanted to query for 20 Pages that were the child of a specific page and list them in Ascending order:</p>
<pre><code>[loop the_query="showposts=20&amp;post_type=page&amp;post_parent=453&amp;ord=ASC"]</code></pre>
<h3>What it returns</h3>
<p>This is totally customizable in the function itself, but I made the judgement call for myself that it would return list items with linked post titles inside. So you&#8217;d go like this:</p>
<pre><code>&lt;ul&gt;
[loop query="posts_per_page=3"]
&lt;/ul&gt;</code></pre>
<p>and you&#8217;d get:</p>
<pre><code>&lt;ul&gt;
&lt;li&gt;&lt;a href="http://digwp.com/post-1/"&gt;Post 1&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://digwp.com/post-2/"&gt;Post 2&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://digwp.com/post-3/"&gt;Post 3&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</code></pre>
<p>And of course those would be real URL&#8217;s and real Post titles&#8230;</p>
<h3>The Code</h3>
<p>This would go in your themes functions.php file. Now that I&#8217;m thinking more clearly about it, this probably makes more sense to be a plugin since it probably shouldn&#8217;t be theme-dependant, but oh well, we can do that another day.</p>
<pre><code>function custom_query_shortcode($atts) {

   // EXAMPLE USAGE:
   // [loop the_query="showposts=100&amp;post_type=page&amp;post_parent=453"]
   
   // Defaults
   extract(shortcode_atts(array(
      "the_query" =&gt; ''
   ), $atts));

   // de-funkify query
   $the_query = preg_replace('~&amp;#x0*([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $the_query);
   $the_query = preg_replace('~&amp;#0*([0-9]+);~e', 'chr(\\1)', $the_query);

   // query is made               
   query_posts($the_query);
   
   // Reset and setup variables
   $output = '';
   $temp_title = '';
   $temp_link = '';
   
   // the loop
   if (have_posts()) : while (have_posts()) : the_post();
   
      $temp_title = get_the_title($post-&gt;ID);
      $temp_link = get_permalink($post-&gt;ID);
      
      // output all findings - CUSTOMIZE TO YOUR LIKING
      $output .= "&lt;li&gt;&lt;a href='$temp_link'&gt;$temp_title&lt;/a&gt;&lt;/li&gt;";
          
   endwhile; else:
   
      $output .= "nothing found.";
      
   endif;
   
   wp_reset_query();
   return $output;
   
}
add_shortcode("loop", "custom_query_shortcode");</code></pre>
<h3>Credit</h3>
<p>When I first wrote this up to use, it totally didn&#8217;t work. I couldn&#8217;t figure it out&#8230; the query was getting passed over just fine, and if I hard coded the query, the whole thing would work just fine. It turns out that because the shortcode lived inside the content of a Post/Page, the ampersand characters in the query were getting encoded, and thus screwing up the query. </p>
<p><a href="http://sketchpad.co.uk/">Dan Smith</a> had the genius idea to use html_entity_decode() on the query before using it in query_posts(), but for some reason I couldn&#8217;t get it to take. </p>
<p><a href="http://www.rogerstringer.com/">Roger Stringer</a> coded up the solution with regular expressions you see above. Thanks guys!</p>
<hr />
<p><small>© 2010 <a href="http://digwp.com">Digging into WordPress</a> | <a href="http://digwp.com/2010/01/custom-query-shortcode/">Permalink</a> | <a href="http://digwp.com/2010/01/custom-query-shortcode/#comments">10 comments</a> | Add to <a href="http://del.icio.us/post?url=http://digwp.com/2010/01/custom-query-shortcode/&title=Custom Query Shortcode: Run a Loop inside Any Post/Page">del.icio.us</a> | Post tags: <a href="http://digwp.com/tag/functions/" rel="tag">functions</a>, <a href="http://digwp.com/tag/loop/" rel="tag">loop</a>, <a href="http://digwp.com/tag/shortcode/" rel="tag">shortcode</a><br/></small></p>]]></content:encoded>
			<wfw:commentRss>http://digwp.com/2010/01/custom-query-shortcode/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Google Maps Shortcode</title>
		<link>http://digwp.com/2010/01/google-maps-shortcode/</link>
		<comments>http://digwp.com/2010/01/google-maps-shortcode/#comments</comments>
		<pubDate>Wed, 20 Jan 2010 01:34:59 +0000</pubDate>
		<dc:creator>Chris Coyier</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[functions]]></category>
		<category><![CDATA[shortcode]]></category>

		<guid isPermaLink="false">http://digwp.com/?p=1224</guid>
		<description><![CDATA[David Hollander of SparkWeb Interactive sent us in a little code clip for inserting Google Maps into Posts/Pages by the use of shortcodes. Google actually has copy-and-pastable iframe code already in Google Maps that is really easy to snag, but David was having problems with the Visual text editor screwing up the code when saving [...]]]></description>
			<content:encoded><![CDATA[<p>David Hollander of <a href="http://sparkweb.net/">SparkWeb Interactive</a> sent us in a little code clip for inserting Google Maps into Posts/Pages by the use of shortcodes. Google actually has copy-and-pastable iframe code already in Google Maps that is really easy to snag, but David was having problems with the Visual text editor screwing up the code when saving the Post. With a short code, no problem.  </p>
<p><span id="more-1224"></span></p>
<h3>Put in functions.php file</h3>
<pre><code>//Google Maps Shortcode
function fn_googleMaps($atts, $content = null) {
   extract(shortcode_atts(array(
      "width" =&gt; '640',
      "height" =&gt; '480',
      "src" =&gt; ''
   ), $atts));
   return '&lt;iframe width="'.$width.'" height="'.$height.'" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="'.$src.'&amp;amp;output=embed"&gt;&lt;/iframe&gt;';
}
add_shortcode("googlemap", "fn_googleMaps");</code></pre>
<p>Usage inside Post/Page:</p>
<pre><code>[googlemap width="200" height="200" src="[url]"]</code></pre>
<p>Notice the three parameters above. Width and height are self explanitory, don&#8217;t use them at all for the default values set in the function itself. The src URL you can snag from Google Maps like this:</p>
<p><img src="http://digwp.com/wp-content/blog-images/mapsURL.png" width="590" height="294" alt="" title="" /></p>
<h3>Easy!</h3>
<p>This post is a good reminder of how easy it is to build your own shortcodes for specific bits of functionality that are specifically useful to you, whatever that might be. In the past, we&#8217;ve covered making <a href="http://digwp.com/2009/09/easy-shortcode-permalinks/">shortcode permalinks</a>. </p>
<hr />
<p><small>© 2010 <a href="http://digwp.com">Digging into WordPress</a> | <a href="http://digwp.com/2010/01/google-maps-shortcode/">Permalink</a> | <a href="http://digwp.com/2010/01/google-maps-shortcode/#comments">12 comments</a> | Add to <a href="http://del.icio.us/post?url=http://digwp.com/2010/01/google-maps-shortcode/&title=Google Maps Shortcode">del.icio.us</a> | Post tags: <a href="http://digwp.com/tag/functions/" rel="tag">functions</a>, <a href="http://digwp.com/tag/php/" rel="tag">PHP</a>, <a href="http://digwp.com/tag/shortcode/" rel="tag">shortcode</a><br/></small></p>]]></content:encoded>
			<wfw:commentRss>http://digwp.com/2010/01/google-maps-shortcode/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Easy Shortcode Permalinks</title>
		<link>http://digwp.com/2009/09/easy-shortcode-permalinks/</link>
		<comments>http://digwp.com/2009/09/easy-shortcode-permalinks/#comments</comments>
		<pubDate>Mon, 28 Sep 2009 15:33:30 +0000</pubDate>
		<dc:creator>Chris Coyier</dc:creator>
				<category><![CDATA[Theme]]></category>
		<category><![CDATA[functions]]></category>
		<category><![CDATA[Links]]></category>
		<category><![CDATA[shortcode]]></category>
		<category><![CDATA[tricks]]></category>

		<guid isPermaLink="false">http://digwp.com/?p=736</guid>
		<description><![CDATA[When you are building a theme, and the circumstance comes up where you need to create a link to a specific page hard-baked right into the theme, there is a function you should be using. Not great: &#60;a href="/contact/"&#62;Contact&#60;/a&#62; Much better: &#60;a href="&#60;?php echo get_permalink(12); ?&#62;"&#62;Contact&#60;/a&#62; That &#8220;12&#8243; would be the ID of the Post [...]]]></description>
			<content:encoded><![CDATA[<p>When you are building a theme, and the circumstance comes up where you need to create a link to a specific page hard-baked right into the theme, there is a function you should be using. </p>
<h4>Not great:</h4>
<pre><code>&lt;a href="/contact/"&gt;Contact&lt;/a&gt;</code></pre>
<h4>Much better:</h4>
<pre><code>&lt;a href="&lt;?php echo get_permalink(12); ?&gt;"&gt;Contact&lt;/a&gt;</code></pre>
<p>That &#8220;12&#8243; would be the ID of the Post or Page. Why is this better?</p>
<ul>
<li>If the slug ever changes, you are still cool.</li>
<li>If the site moves from a subdomain (like if you were developing and then moving) to a top level domain or vice versa, you are still cool.</li>
</ul>
<p>Doing it this way is a permanent reference to that Post or Page that will never change. This works great when we are working within our theme files, but what about when we are working within WordPress and actually writing Posts and Pages?</p>
<p><span id="more-736"></span></p>
<p>By default, we can&#8217;t run PHP within the content of our Posts and Pages, so we can&#8217;t use the <code>get_permalink</code> function.  What we <em>can</em> do, is create a shortcode with just about the same functionality. And here it is:</p>
<pre><code>function permalink_thingy($atts) {
	extract(shortcode_atts(array(
		'id' =&gt; 1,
		'text' =&gt; ""  // default value if none supplied
    ), $atts));
    
    if ($text) {
        $url = get_permalink($id);
        return "&lt;a href='$url'&gt;$text&lt;/a&gt;";
    } else {
	   return get_permalink($id);
	}
}
add_shortcode('permalink', 'permalink_thingy');</code></pre>
<p>This shortcode can be used in two ways:</p>
<h4>Basic:</h4>
<pre><code>&lt;a href="[permalink id=49]"&gt;Using without providing text&lt;/a&gt;</code></pre>
<p>This way you only provide the id parameter and it only returns a URL. This was you can use that URL however you want. For example, if you needed to add a special class name to the link or something (but only occasionally).</p>
<h4>Providing text:</h4>
<pre><code>[permalink id=49 text='providing text']</code></pre>
<p>This way returns a fully formatted anchor link back, using the text you pass.</p>
<hr />
<p><small>© 2009 <a href="http://digwp.com">Digging into WordPress</a> | <a href="http://digwp.com/2009/09/easy-shortcode-permalinks/">Permalink</a> | <a href="http://digwp.com/2009/09/easy-shortcode-permalinks/#comments">13 comments</a> | Add to <a href="http://del.icio.us/post?url=http://digwp.com/2009/09/easy-shortcode-permalinks/&title=Easy Shortcode Permalinks">del.icio.us</a> | Post tags: <a href="http://digwp.com/tag/functions/" rel="tag">functions</a>, <a href="http://digwp.com/tag/links/" rel="tag">Links</a>, <a href="http://digwp.com/tag/shortcode/" rel="tag">shortcode</a>, <a href="http://digwp.com/tag/tricks/" rel="tag">tricks</a><br/></small></p>]]></content:encoded>
			<wfw:commentRss>http://digwp.com/2009/09/easy-shortcode-permalinks/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Global Custom Fields</title>
		<link>http://digwp.com/2009/09/global-custom-fields/</link>
		<comments>http://digwp.com/2009/09/global-custom-fields/#comments</comments>
		<pubDate>Wed, 16 Sep 2009 11:49:35 +0000</pubDate>
		<dc:creator>Chris Coyier</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[custom-fields]]></category>
		<category><![CDATA[echo]]></category>
		<category><![CDATA[functions]]></category>
		<category><![CDATA[shortcode]]></category>
		<category><![CDATA[tricks]]></category>

		<guid isPermaLink="false">http://digwp.com/?p=659</guid>
		<description><![CDATA[UPDATE: Make sure to take out &#8220;Take Two&#8221; on this concept, with a cleaner method for doing this. Custom fields allow us to attach data to Posts or Pages that we can yank out and use at will in our templates. They are awesomely flexible and single-handedly allow WordPress to be used for about any [...]]]></description>
			<content:encoded><![CDATA[<p><strong>UPDATE</strong>: Make sure to take out &#8220;<a href="http://digwp.com/2009/09/global-custom-fields-take-two/">Take Two</a>&#8221; on this concept, with a cleaner method for doing this.</p>
<p>Custom fields allow us to attach data to Posts or Pages that we can yank out and use at will in our templates. They are awesomely flexible and single-handedly allow WordPress to be used for about any CMS need. The fact that they can only be used on single Posts can be limiting in some circumstances. Sometimes you wish you could grab a custom value that you can control and is consistent globally, regardless of the current post. In this post we&#8217;ll look at a technique to do so.</p>
<p><span id="more-659"></span></p>
<h3>Fair Warning</h3>
<p>This might not be the perfect technique. If you read this and have a smarter way, I&#8217;m all ears, this worked for me in my quick testing. What would really be slick is to make a plugin out of this, where you can create global key/value pairs through a special settings panel that could be use anywhere.</p>
<p>WordPress already has a somewhat-similar system in place through how the <a href="http://codex.wordpress.org/Template_Tags/bloginfo">bloginfo</a> function works. </p>
<pre><code>&lt;?php bloginfo('name'); ?&gt;</code></pre>
<p>Perhaps that could be tapped into and expanded upon, I dunno.</p>
<h3>Example Use</h3>
<p>One idea is for something like an Amazon affiliate ID code. You might use a value like this all over the place on a site, embedding within amazon links and widgets. If you hard-code this value, and then need to change it someday, that would suck. It would be better to save this value in one place, and use a variable (global custom field) to output it.</p>
<h3>1. Create a Page</h3>
<p>Call it &#8220;Global Custom Fields&#8221;. It doesn&#8217;t need any content, but you can use this page to add the custom fields you wish to use globally.</p>
<p><img src="http://digwp.com/wp-content/blog-images/globalcustomfieldspage.png" width="589" height="300" alt="" title="" /></p>
<p>Notice down below, we just add custom fields in key/value pairs like any other custom fields on any other Page or Post.</p>
<p>Make sure to note the ID of this Page. We&#8217;ll need that ID for the custom functions we are about to write, but you can also use it to exclude this page from any dynamic navigation and such, as it&#8217;s not really meant for public viewage.</p>
<p><img src="http://digwp.com/wp-content/blog-images/idvalueofpage.png" width="473" height="39" alt="" title="" /></p>
<h3>2. Create Generic Function</h3>
<p>The built-in WordPress <a href="http://codex.wordpress.org/Function_Reference/get_post_meta">get_post_meta</a> function accepts any Post ID and any key value. So we&#8217;ll add a new function to our theme&#8217;s functions.php file that generically returns values for keys we pass it. The Page ID will be hard-coded, equal to the value we just noted down.</p>
<pre><code>function global_custom_field($key) {
   $output = get_post_meta(677, $key, true);
   echo $output;
}</code></pre>
<h3>3. Create a Shortcode Function</h3>
<p>With our generic function, we can already use it just as-is in our templates. Without a special plugin though, you can&#8217;t execute PHP functions in the body of articles though. We can be even smarter though, and create a shortcode function for use in our content. </p>
<p>More stuff for your functions.php file:</p>
<pre><code>function gcf_func($atts) {
    extract(shortcode_atts(array(
		'key' =&gt; 'amazon_id'  // default value if none supplied
    ), $atts));
    return global_custom_field($key);
}
add_shortcode('global_custom_field', 'gcf_func');</code></pre>
<h3>4. Use it!</h3>
<p>Now anywhere in our template we wish to call in the value of a global custom field, we just pass the function the key value and we get back our value:</p>
<pre><code>echo global_custom_field("amazon_id");</code></pre>
<p>Or right inside our posts content:</p>
<pre><code>[global_custom_field key="amazon_id"]</code></pre>
<hr />
<p><small>© 2009 <a href="http://digwp.com">Digging into WordPress</a> | <a href="http://digwp.com/2009/09/global-custom-fields/">Permalink</a> | <a href="http://digwp.com/2009/09/global-custom-fields/#comments">12 comments</a> | Add to <a href="http://del.icio.us/post?url=http://digwp.com/2009/09/global-custom-fields/&title=Global Custom Fields">del.icio.us</a> | Post tags: <a href="http://digwp.com/tag/custom-fields/" rel="tag">custom-fields</a>, <a href="http://digwp.com/tag/echo/" rel="tag">echo</a>, <a href="http://digwp.com/tag/functions/" rel="tag">functions</a>, <a href="http://digwp.com/tag/shortcode/" rel="tag">shortcode</a>, <a href="http://digwp.com/tag/tricks/" rel="tag">tricks</a><br/></small></p>]]></content:encoded>
			<wfw:commentRss>http://digwp.com/2009/09/global-custom-fields/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>

