<?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>Orange Cabin &#187; wordpress</title>
	<atom:link href="http://blog.orangecabin.com/category/wordpress/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.orangecabin.com</link>
	<description>Blog of a freelance programmer</description>
	<lastBuildDate>Thu, 27 May 2010 13:25:14 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>WordPress 2.7 Plugin tutorial : First Plugin in 20 lines</title>
		<link>http://blog.orangecabin.com/2009/05/wordpress-2-7-plugin-tutorial-first-plugin-in-20-lines/</link>
		<comments>http://blog.orangecabin.com/2009/05/wordpress-2-7-plugin-tutorial-first-plugin-in-20-lines/#comments</comments>
		<pubDate>Sat, 30 May 2009 05:06:24 +0000</pubDate>
		<dc:creator>Pan</dc:creator>
				<category><![CDATA[plugin]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://blog.orangecabin.com/?p=3</guid>
		<description><![CDATA[WordPress doesn&#8217;t provide an &#8220;Hello world&#8221;  plugin tutorial. There are some links to 3rd party tutorials,  but those samples are years old and doing stuff in old way.
It doesn&#8217;t have to be that complex. I will demonstrate how to create a simple plugin in 20 lines, the plugin can add some HTML to page footer [...]]]></description>
			<content:encoded><![CDATA[<p>WordPress doesn&#8217;t provide an &#8220;Hello world&#8221;  plugin tutorial. There are some links to 3rd party tutorials,  but those samples are years old and doing stuff in old way.</p>
<p>It doesn&#8217;t have to be that complex. I will demonstrate how to create a simple plugin in 20 lines, the plugin can add some HTML to page footer and/or post. <span id="more-3"></span> This is an &#8220;Hello World&#8221;, so I won&#8217;t explain the plugin framework in detail but I will cover essential concepts.<br />
<em>This tutorial is for (newbie) plugin developers,  it won&#8217;t explain how to use WP and how to code PHP. </em></p>
<h3>Creating a plugin</h3>
<p>Create a directory &#8220;simple-add-to-footer&#8221; under wp-content/plugins, then create a file &#8220;simple-add-to-footer.php&#8221;. Add this code to the newly created page.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">/*
Plugin Name: Simple Add To Footer
Plugin URI: http://localhost/wordpress/
Description: Add HTML snippets to posts and/or page footer.
It is a plugin tutorial using latest plugin API of WP2.7+
Version: 0.1
Author: me
Author URI: http://blog.orangecabin.com/
Disclaimer: Use at your own risk. No warranty expressed or implied is provided.
*/</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>This is called &#8220;Standard Plugin Information&#8221;, you can read more about it on  <a href="http://codex.wordpress.org/Writing_a_Plugin">http://codex.wordpress.org/Writing_a_Plugin</a>.  That is all it needs to be a valid plugin. Now this &#8220;plugin&#8221; can be activated like other plugins.</p>
<h3>Adding something interesting</h3>
<p>OK it does nothing till now. Please add this after the plugin info.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">add_action<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'wp_footer'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'simple_add_to_footer_wp_footer'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">function</span> simple_add_to_footer_wp_footer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;div&gt;Hello world! '</span><span style="color: #339933;">.</span> get_option<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'blogname'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;/div&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>If you activated the plugin now, you can see it print something in the page footer.  </p>
<h3>What is &#8220;action&#8221;</h3>
<p>Here goes some theory : &#8220;action&#8221; is one type of the extension points of WordPress, that means you extend an action to do something to WordPress.<br />
add_action is &#8220;the interface&#8221; between wordpress and your plugin. The 1st parameter &#8220;wp-footer&#8221; is a simple action which is called to produce page footer. The 2nd parameter is name of you function. And you do what you need in that function.<br />
simple_add_to_footer_wp_footer calls get_option(&#8216;blogname&#8217;). It is a WordPress defined API to get blog options, like blog name. </p>
<h3>Filter &#8211; another type of extension point</h3>
<p>Now add this to the file</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">add_filter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'the_content'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'simple_add_to_footer_the_content'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">function</span> simple_add_to_footer_the_content<span style="color: #009900;">&#40;</span><span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$content</span> <span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;div&gt;I am '</span><span style="color: #339933;">.</span> get_the_title<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;/div&gt;'</span> <span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Reload your blog page, now you can see it adds some info to the post.<br />
Filter is another type of extension point. Instead of echo something directly, you get a parameter ($content) from WordPress and you can manipulate it, like upper case or something.</p>
<h3>Conclusion</h3>
<p>That is all. Here is what we have now:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">/*
Plugin Name: Simple Add To Footer
Plugin URI: http://localhost/wordpress/
Description: Add HTML snippets to posts and/or page footer.
It is a plugin tutorial using latest plugin API of WP2.7+
Version: 0.1
Author: me
Author URI: http://blog.orangecabin.com/
Disclaimer: Use at your own risk. No warranty expressed or implied is provided.
*/</span>
add_action<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'wp_footer'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'simple_add_to_footer_wp_footer'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">function</span> simple_add_to_footer_wp_footer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;div&gt;Hello world! '</span><span style="color: #339933;">.</span> get_option<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'blogname'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span><span style="color: #0000ff;">' &lt;/div&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
add_filter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'the_content'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'simple_add_to_footer_the_content'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">function</span> simple_add_to_footer_the_content<span style="color: #009900;">&#40;</span><span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$content</span> <span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;div&gt; I am '</span><span style="color: #339933;">.</span> get_the_title<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;/div&gt;'</span> <span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>You can download source code here: <a href='http://blog.orangecabin.com/wp-content/uploads/2009/05/simple-add-to-footer_0.1.zip'>simple-add-to-footer_0.1</a><br />
OK I know it is not really useful. But this plugin can be easily extend to do interesting stuff, like inserting arbitrary HTML code to page footer. To do that, you need a page for the plugin user to type the HTML they want to insert. The next post will explain how to do that easily using latest WordPress plugin API.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.orangecabin.com/2009/05/wordpress-2-7-plugin-tutorial-first-plugin-in-20-lines/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
