<?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>matty.co.za &#187; Tutorials</title>
	<atom:link href="http://matty.co.za/category/tutorials/feed/" rel="self" type="application/rss+xml" />
	<link>http://matty.co.za</link>
	<description>Web developer, WordPress enthusiast, avid musician and blogger</description>
	<lastBuildDate>Thu, 12 Apr 2012 08:43:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Filtering the Options API in WordPress</title>
		<link>http://matty.co.za/2012/01/filtering-wordpress-options-api/</link>
		<comments>http://matty.co.za/2012/01/filtering-wordpress-options-api/#comments</comments>
		<pubDate>Thu, 19 Jan 2012 09:00:13 +0000</pubDate>
		<dc:creator>Matty</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[Project 365]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Web Developer]]></category>
		<category><![CDATA[WordPress Plugins]]></category>
		<category><![CDATA[WordPress Themes]]></category>

		<guid isPermaLink="false">http://matty.co.za/?p=1228</guid>
		<description><![CDATA[The Options API in WordPress is one of the many WordPress APIs we all use every day when developing with WordPress. A quick call to get_option() is not an uncommon sight. What if you could dynamically filter those options? You can. Adding filters in WordPress is also a common practice. Combining this with the Options [...]]]></description>
			<content:encoded><![CDATA[<p>The Options API in WordPress is one of the many WordPress APIs we all use every day when developing with WordPress. A quick call to get_option() is not an uncommon sight. What if you could dynamically filter those options? <strong>You can</strong>.</p>
<p>Adding filters in WordPress is also a common practice. Combining this with the Options API can allow for, for example, the ability to change an option when in preview mode without committing to the change.</p>
<p>In the &#8220;Magazine&#8221; template in Canvas by WooThemes, for example, WooTumblog &#8220;image&#8221; and &#8220;video&#8221; posts are aware when they are present in the magazine-style grid. This is an example of filtering the Options API.</p>
<p><span id="more-1228"></span><br />
<h3>How do I use this?</h3>
<p>Filtering the Options API is as easy as compiling any other filter. The filter hook is as follows: &#8220;<code>option_optionname</code>&#8221; (replace &#8220;<code>optionname</code>&#8221; with the name of the option you want to filter).</p>
<h3>That filter is applied after the database lookup. What about before that even happens?</h3>
<p>It is possible to short-circuit an option&#8217;s value using a second filter that has been made available. This means it&#8217;s possible to set a custom value, via a filter, for an option, bypassing the database lookup. My thoughts are packed with ideas for where to use this filter!</p>
<p>To short-circuit an option called, for example, &#8220;testoption&#8221;, you&#8217;d add the following code to your <code>functions.php</code> file:</p>
<p><code></p>
<pre>
add_filter( 'pre_option_testoption', 'matty_shortcircuit_options' );

function matty_shortcircuit_options ( $default ) {
	$value = 'this is a short-circuited option';
	return $value;
} // End matty_shortcircuit_options()
</pre>
<p></code></p>
<p>Using the &#8220;testoption&#8221; example again, this is how you&#8217;d override the value after it&#8217;s been retrieved from the database:</p>
<p><code></p>
<pre>
add_filter( 'option_testoption', 'matty_override_options' );

function matty_override_options ( $value ) {
	$value = 'this is an overridden option';

	// To override based on a condition, do this.
	if ( $value == 'test' ) {
		$value = 'this is an overridden option, based on a condition.';
	}

	return $value;
} // End matty_override_options()
</pre>
<p></code></p>
<p>This is just, however, a starting point as to how to do this. The possibilities are virtually limitless as to what is possible here and how to apply this in your projects.</p>
]]></content:encoded>
			<wfw:commentRss>http://matty.co.za/2012/01/filtering-wordpress-options-api/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to improve web developer coding practices (and code)</title>
		<link>http://matty.co.za/2012/01/web-coding-best-practices/</link>
		<comments>http://matty.co.za/2012/01/web-coding-best-practices/#comments</comments>
		<pubDate>Sun, 15 Jan 2012 18:44:15 +0000</pubDate>
		<dc:creator>Matty</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Project 365]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Web Developer]]></category>

		<guid isPermaLink="false">http://matty.co.za/?p=1223</guid>
		<description><![CDATA[Every developer approaches their day to day development tasks from a different angle. In addition to this, each developer &#8220;designs&#8221; their code to suit their own personal preferences and approaches towards specifics in a project. When developers examine code written by other developers, we&#8217;re often critical (sometimes hyper-critical) of the code itself, mostly according to [...]]]></description>
			<content:encoded><![CDATA[<p>Every developer approaches their day to day development tasks from a different angle. In addition to this, each developer &#8220;designs&#8221; their code to suit their own personal preferences and approaches towards specifics in a project. When developers examine code written by other developers, we&#8217;re often critical (sometimes hyper-critical) of the code itself, mostly according to our personal preferences. While there is a place for being critical of code, and it should be encouraged, there are a few aspects of this criticism that should be left at the door&#8230; namely, the personal preferences.</p>
<p>While we all have our own preferences, it&#8217;s important to solidify a few areas when approaching code and to, ultimately, hone the developer&#8217;s mindset into certain guidelines. Below are a few thoughts I have running through my mind constantly while developing:</p>
<p><span id="more-1223"></span><br />
<h3>1. Minimize lookups.</h3>
<p>This one is crucial when developing systems with heavy load. How many times are you hitting the database for information and how can you minimize that number? For example, when <a title="A Quick Guide to the WordPress Transients API" href="http://matty.co.za/2012/01/wordpress-transients-api/">using the Transients API in WordPress</a>, why would you run get_transient() again after you&#8217;ve just set the transient up? You&#8217;ve already got the data, so there&#8217;s no need.</p>
<h3>2. D.R.Y (don&#8217;t repeat yourself).</h3>
<p>This is a common development practice. Don&#8217;t repeat yourself. If you&#8217;ve got, for example, two functions that perform API requests, create a function that runs the API request with a few parameters to customise it in each case, rather than coding the request twice.</p>
<h3>3. One function, one purpose.</h3>
<p>This one helps me every day. It also ensures that your code is kept clean. If, for example, you have to display a collection of posts with specific criteria, why compile one function to do it all, when you could have a small function to get the data from the database and a second small function to output the data neatly? Surely that would make your code easier to read and maintain? Yep, it would. <img src='http://matty.co.za/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h3>4. Do I need this function here?</h3>
<p>This one is about load and separation of code. Why load extra functions when you don&#8217;t need them in a specific context? Keeping this in mind ensures that you keep your code in &#8220;bite size chunks&#8221;, for example, splitting admin-related functions versus frontend functionality.</p>
<p>These items, while simple to adhere to, can alter the way a developer approaches a project, as well as creating a better end result. Most are also common development practices that most developers should be familiar with, making it easier for other developers to read and understand your projects.</p>
<p><em>Do you have any tips and tricks that help you day to day while developing?</em></p>
]]></content:encoded>
			<wfw:commentRss>http://matty.co.za/2012/01/web-coding-best-practices/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>GROW Academy Bootcamp 2012</title>
		<link>http://matty.co.za/2012/01/grow-academy-bootcamp-2012/</link>
		<comments>http://matty.co.za/2012/01/grow-academy-bootcamp-2012/#comments</comments>
		<pubDate>Mon, 09 Jan 2012 13:00:31 +0000</pubDate>
		<dc:creator>Matty</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[Blogging]]></category>
		<category><![CDATA[Project 365]]></category>
		<category><![CDATA[Social Media]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Web Developer]]></category>

		<guid isPermaLink="false">http://matty.co.za/?p=1198</guid>
		<description><![CDATA[This week, Jeff and I will be presenting at our second GROW Academy Bootcamp session. We&#8217;ll be discussing &#8220;Website Design &#38; Development&#8221; with the recruits, running through WordPress and how to setup a website using WordPress.com or WordPress.org. The GROW Academy is an initiative to educate and empower the youth of today through technology. The [...]]]></description>
			<content:encoded><![CDATA[<p>This week, <a title="Jeffikus" href="http://jeffikus.com/">Jeff</a> and I will be presenting at our second GROW Academy Bootcamp session. We&#8217;ll be discussing &#8220;Website Design &amp; Development&#8221; with the recruits, running through WordPress and how to setup a website using WordPress.com or WordPress.org.</p>
<p>The <a title="The GROW Academy" href="http://grow.org.za/">GROW Academy</a> is an initiative to educate and empower the youth of today through technology. The Bootcamp session covers everything from social media and setting up e-mail, all the way through to search engine optimisation and an internet super-user course, for those who wish to continue on with more advanced studies. The GROW website&#8217;s &#8220;<a title="About GROW" href="http://grow.org.za/about/">About</a>&#8221; page (built on Canvas and Canvas BuddyPress by WooThemes) has a detailed explanation of the initiative and it&#8217;s founding partners.</p>
<p><span id="more-1198"></span>This year, Jeff and I will be making some exciting additions to our presentation. While code, to some, can seem dry and boring, I can assure you that there&#8217;s nothing dry or boring about our presentation. We had such a blast at last year&#8217;s Bootcamp and are really looking forward to getting into some code with this year&#8217;s recruits.</p>
<p>If you&#8217;re keen to get involved with GROW, I&#8217;d encourage you to contact the GROW Academy and see how you can help out.</p>
<p>Remember: it&#8217;s all easy, and there&#8217;s no such thing as a stupid question. <img src='http://matty.co.za/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://matty.co.za/2012/01/grow-academy-bootcamp-2012/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>More tips for styling the WordPress tinyMCE editor</title>
		<link>http://matty.co.za/2010/08/more-wordpress-custom-editor-styles/</link>
		<comments>http://matty.co.za/2010/08/more-wordpress-custom-editor-styles/#comments</comments>
		<pubDate>Mon, 09 Aug 2010 13:11:07 +0000</pubDate>
		<dc:creator>Matty</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[Theming]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[WordPress Support]]></category>
		<category><![CDATA[WordPress Themes]]></category>

		<guid isPermaLink="false">http://matty.co.za/?p=1084</guid>
		<description><![CDATA[A few weeks ago, I blogged about styling the tinyMCE editor in WordPress to resemble your WordPress theme&#8217;s content area. On this post, I received a comment from LA, asking if it&#8217;s possible to style the tinyMCE editor for specific posts or post templates. Folks, it&#8217;s WordPress&#8230; anything&#8217;s possible! With my mission at hand, I [...]]]></description>
			<content:encoded><![CDATA[<p><a title="Browse through more posts I've written about WordPress" href="http://matty.co.za/category/wordpress/"><img class="alignright size-full wp-image-1013" title="WordPress logo in blue" src="http://matty.co.za/wp-content/uploads/2010/04/blue-m.png" alt="Blue WordPress logo, courtesy http://wordpress.org/about/logos/" width="100" height="100" /></a>A few weeks ago, I blogged about <a title="Styling the tinyMCE editor in WordPress" href="http://matty.co.za/2010/05/editor-style-in-wordpress/">styling the tinyMCE editor in WordPress</a> to resemble your WordPress theme&#8217;s content area. On this post, I received a comment from LA, asking if it&#8217;s possible to style the tinyMCE editor for specific posts or post templates. Folks, it&#8217;s <a title="Browse more posts about WordPress" href="http://matty.co.za/category/wordpress/">WordPress</a>&#8230; anything&#8217;s possible!</p>
<p>With my mission at hand, I set to work. I&#8217;d been thinking about this for a while after writing the initial blog post and am please to say that I have found a solution. Please be sure you&#8217;ve read through the <a title="Styling the tinyMCE editor in WordPress" href="http://matty.co.za/2010/05/editor-style-in-wordpress/">initial blog post</a>, as the main points are covered over there.</p>
<p>There are a few steps we need to go through here. They&#8217;re pretty straight forward, so bear with me. <img src='http://matty.co.za/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><span id="more-1084"></span></p>
<p><img class="aligncenter size-full wp-image-1088" title="LA's question regarding styling the tinyMCE editor in WordPress" src="http://matty.co.za/wp-content/uploads/2010/08/la_question.png" alt="LA's question regarding styling the tinyMCE editor in WordPress" width="620" height="150" /></p>
<h3>Reference the global $post object</h3>
<p>First things first. How do we know what page, post or custom post type entry we&#8217;re working with? We need to get this information from WordPress. Luckily for us, WordPress also needs to know which entry we&#8217;re editing. We get this information by adding the following to the beginning of our function:</p>
<pre>global $post;</pre>
<p>This lets us work with the variable $post, which was previously created outside of our function. This variable is an object that holds information about the entry we are currently editing (ID, title, content, template, etc). We will be using this information at a later stage to insert our custom CSS file(s).</p>
<p>Due to this being a custom solution specific to a particular page, post or custom post type entry, this solution will only truly take effect on the &#8220;Edit&#8221; screens when targeting a specific entry or an entry with a particular custom template applied.</p>
<h3>Check if the file we&#8217;re looking for exists</h3>
<p>We wouldn&#8217;t want to call a file that we know doesn&#8217;t exist. Therefore, we will use a conditional (in this case, an IF statement) to determine whether or not to look for our custom CSS file, based on whether or not it exists.</p>
<h3>If the CSS file exists, add it to the list of CSS files to include</h3>
<p>After the line where we include our main editor-style.css file, add the following code:</p>
<pre><code>if ( file_exists( trailingslashit( get_stylesheet_directory() ) . 'assets/css/editor-style-' . $post-&gt;post_type . '.css' ) ) {

	if ( !empty($url) )
 	$url .= ',';

	// Change the path here if using sub-directory
 	$url .= trailingslashit( get_stylesheet_directory_uri() ) . 'assets/css/editor-style-' . $post-&gt;post_type . '.css';

} // End IF Statement
</code></pre>
<p>The above code checks if our custom CSS file (in this case, for a post type-specific CSS file) exists and, if it does, adds it to the list of CSS files to include in the tinyMCE editor. We include it after checking for our main editor-style.css file so that any of our custom CSS declarations override the defaults we have in the main editor-style.css file.</p>
<h3>Extending this functionality</h3>
<p>In the above example, I am including a custom CSS file based on the post type of the entry being edited. Altering this is relatively straight forward and can allow for custom CSS files depending on date published, post publish status, custom template being used and virtually any condition imaginable.</p>
<p>As an example of extending this functionality, we will include a file based on post ID. To do this, simply paste the above code into the function again and replace all instances of `post_type` with `ID` (such that $post-&gt;post_type becomes $post-&gt;ID). Thus, if a post has an ID of 7 (for example), the system will look for a file called editor-style-7.css and, if it exists, include it in the tinyMCE editor.</p>
<p>Please note that using post IDs can result in a large quantity of custom CSS files and is not recommended. The above is simply an example for extending the functionality we just created.</p>
<p>Folks, there you have it. Not too difficult, I hope. This allows for further customisation of the WordPress tinyMCE editor, creating a richer user experience for the bloggers using your theme, as well as creating a more accurate representation of what the post will look like on the front end of their WordPress website.</p>
]]></content:encoded>
			<wfw:commentRss>http://matty.co.za/2010/08/more-wordpress-custom-editor-styles/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Styling the tinyMCE editor in WordPress</title>
		<link>http://matty.co.za/2010/05/editor-style-in-wordpress/</link>
		<comments>http://matty.co.za/2010/05/editor-style-in-wordpress/#comments</comments>
		<pubDate>Sun, 09 May 2010 11:54:18 +0000</pubDate>
		<dc:creator>Matty</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[WordPress Support]]></category>
		<category><![CDATA[WordPress Themes]]></category>

		<guid isPermaLink="false">http://matty.co.za/?p=1048</guid>
		<description><![CDATA[With WordPress&#8217; easy to use nature and user interface, content management of websites is accessible to a vast range of users, from the Bill Gates&#8217; of the world right through to users who discovered this &#8220;internet thing&#8221; just yesterday. Once the concepts of &#8220;what is a content management system?&#8221; and &#8220;Okay, so this is the [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignright size-full wp-image-1051" title="WordPress logo in Grey" src="http://matty.co.za/wp-content/uploads/2010/05/grey-m.png" alt="WordPress logo" width="100" height="100" />With WordPress&#8217; easy to use nature and user interface, content management of websites is accessible to a vast range of users, from the Bill Gates&#8217; of the world right through to users who discovered this &#8220;internet thing&#8221; just yesterday. Once the concepts of &#8220;what is a content management system?&#8221; and &#8220;Okay, so this is the &#8216;backend&#8217; and the website is the &#8216;frontend&#8217;&#8221; have been grasped, the usual question arises: &#8220;So, why does the backend content look different to the frontend content?&#8221;. To this question, we are about to say one thing: &#8220;Question&#8230; be gone!&#8221;<span id="more-1048"></span></p>
<h3>What are we going to achieve today?</h3>
<p>Today, we&#8217;re going to style the editor in the WordPress backend to resemble the content as it is in the theme that is being used on the frontend. Simple objective, so lets get started.</p>
<h3>What do we need?</h3>
<p>We&#8217;ll need three things to get this done: an editor-specific CSS stylesheet (I usually call this editor-style.css), a PHP function to do the work and a WordPress filter to piece things all together.</p>
<h3>Creating the editor-specific CSS stylesheet</h3>
<p>To do this, we need to keep in mind one thing: we&#8217;re cloning the styling of our theme&#8217;s content area into a new stylesheet. Simply loading the existing stylesheet in place of this file will not work out properly. One other thing to note is, the content area styles are now to be placed within the .mceContentBody{} CSS style. In addition to copying and pasting the content styling from my theme  (and changing all instances of #content to read .mceContentBody), I have also imported the CSS reset script I use via an @import call. This is to ensure added consistency within the editor styling. If your theme uses a CSS reset file, I would recommend importing it as well.</p>
<p>A snippet from my editor-style.css file looks like this:</p>
<pre><code>.mceContentBody { color: #666666; font-family: Arial, Helvetica, sans-serif !important; font-size: 12px; line-height: 18px; width: 640px; }
		.mceContentBody a, .mceContentBody a:visited { background: none; color: rgb(60, 130, 170); text-decoration: underline; }
		.mceContentBody a:hover { background: none; color: rgb(60, 140, 180); text-decoration: underline; }</code></pre>
<h3>Writing the PHP function to do the work</h3>
<p>Right, folks. This is where we do the main work involved with this. We&#8217;ll be writing a function to add our new editor-style.css file into the mix with the existing tinyMCE stylesheet in the backend. The function looks like this:</p>
<pre class="code"><code>
/**
 * matty_editor_style()
 */

function matty_editor_style( $url ) {

  if ( !empty($url) )
    $url .= ',';

  // Change the path here if using sub-directory
  $url .= trailingslashit( get_stylesheet_directory_uri() ) . 'assets/css/editor-style.css';

  return $url;

} // End matty_editor_style()
</code></pre>
<p>And in English, this function reads like this:</p>
<p>The function accepts $url as a parameter. This is the existing tinyMCE stylesheet, passed through the WordPress filter. More on that in a bit. If the URL is provided (and not empty), add a comma (,) at the end of it. After that, create the URL to our editor-style.css file, as it sits in our theme folder, and add it to the end of $url. Return out the newly filtered $url back to the system.</p>
<h3>The WordPress filter that makes it all happen</h3>
<p>The last line. Nice and simple.</p>
<pre><code>add_filter( 'mce_css', 'matty_editor_style' );</code></pre>
<p>This is using the native WordPress function, add_filter(), to take in the &#8216;mce_css&#8217; stylesheet (the apply_filters() function is applied to the main tinyMCE CSS stylesheet before it is loaded into the system) and add the editor-style.css file into the mix with the existing file. No mess, no fuss.</p>
<p>Place the above PHP function and filter in your theme&#8217;s functions.php file, and you&#8217;re set.</p>
<p>And there we have it, folks. The editor in the WordPress backend no resembles the frontend of our WordPress theme. I hope this tutorial helps you in creating beautiful and easy to use content managed websites with WordPress.</p>
]]></content:encoded>
			<wfw:commentRss>http://matty.co.za/2010/05/editor-style-in-wordpress/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Enhancing the comments list in WordPress</title>
		<link>http://matty.co.za/2010/05/enhancing-wordpress-comments/</link>
		<comments>http://matty.co.za/2010/05/enhancing-wordpress-comments/#comments</comments>
		<pubDate>Sun, 02 May 2010 08:21:06 +0000</pubDate>
		<dc:creator>Matty</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[WordPress Support]]></category>
		<category><![CDATA[WordPress Themes]]></category>

		<guid isPermaLink="false">http://matty.co.za/?p=1024</guid>
		<description><![CDATA[With the introduction of the wp_list_comments() function, WordPress enabled users to easily list comments on the websites without having to manually run a series of loops and queries to get the comments into neat XHTML. This function outputs default code with a selection of options for how this code is structured. Today we&#8217;ll be customising [...]]]></description>
			<content:encoded><![CDATA[<p>With the introduction of the wp_list_comments() function, WordPress enabled users to easily list comments on the websites without having to manually run a series of loops and queries to get the comments into neat XHTML. This function outputs default code with a selection of options for how this code is structured. Today we&#8217;ll be customising how comments are displayed in our WordPress theme, and adding a few extra enhancements to our comments while we&#8217;re at it (one of them being the Twitter username we added before). Lets start with the callback, shall we?<span id="more-1024"></span></p>
<h3>&#8220;Callback&#8221;, you say? Erm&#8230; what&#8217;s that?</h3>
<p>&#8220;callback&#8221; is one of the arguments we can pass to the wp_list_comments() function. The &#8220;callback&#8221; argument expects a string which, in this case, is the name of our callback function. Essentially, by using the &#8220;callback&#8221; argument, we&#8217;re saying to WordPress; &#8220;okay, that layout looks cool, but use this awesome layout instead&#8221;.</p>
<h3>So&#8230; this function&#8230;</h3>
<p>Right. The meat of this method is the callback function itself. This is the code that will be used to display each comment in the list. No need to create the list itself, just the items (as is most likely done in your comments.php theme file at this time). The callback function we&#8217;ll be creating looks something like this:</p>
<pre>
<div id="_mcePaste">&lt;?php</div>
<div id="_mcePaste">/**</div>
<div id="_mcePaste">* matty_comment_layout()</div>
<div id="_mcePaste">* A callback function to be used with the wp_list_comments() function.</div>
<div id="_mcePaste">*/</div>
<div id="_mcePaste">function matty_comment_layout ( $comment, $args, $depth ) {</div>
<div id="_mcePaste">$GLOBALS['comment'] = $comment;</div>
<div id="_mcePaste">$path_to_default_gravatar = get_bloginfo('stylesheet_directory') . '/assets/images/gravatar.png';</div>
<div id="_mcePaste">$twitter = '';</div>
<div id="_mcePaste">$twitter = get_comment_meta( get_comment_ID(), 'twitter', true );</div>
<div id="_mcePaste">if ( $twitter != '' ) {</div>
<div id="_mcePaste">$twitter = strtolower($twitter);</div>
<div id="_mcePaste">$twitter = str_replace(' ', '', $twitter);</div>
<div id="_mcePaste">$twitter = str_replace('.', '', $twitter);</div>
<div id="_mcePaste">$twitter = ' &lt;span&gt;(&lt;a href="http://twitter.com/' . $twitter . '" rel="nofollow"&gt;' . $twitter . '&lt;/a&gt;)&lt;/span&gt;';</div>
<div id="_mcePaste">} // End IF Statement</div>
<div id="_mcePaste">// Sanitise gravatar and make sure the image has an ALT attribute that isn't empty.</div>
<div id="_mcePaste">$gravatar = get_avatar($comment,$size='48',$default=$path_to_default_gravatar );</div>
<div id="_mcePaste">$gravatar = str_replace ("alt=''", 'alt="' . $comment-&gt;comment_author . '\'s Gravatar"', $gravatar);</div>
<div id="_mcePaste">?&gt;</div>
<div id="_mcePaste">&lt;li &lt;?php comment_class(); ?&gt; id="comment-&lt;?php echo $comment-&gt;comment_ID; ?&gt;"&gt;</div>
<div id="_mcePaste">&lt;div&gt;&lt;?php echo $gravatar; ?&gt;&lt;?php printf( __('&lt;cite&gt;%s&lt;/cite&gt;'), get_comment_author_link(), $twitter ); ?&gt;&lt;/div&gt;</div>
<div id="_mcePaste">&lt;div&gt;&lt;?php printf(__('Posted %1$s at %2$s (%3$s ago)', 'matty'),</div>
<div id="_mcePaste">get_comment_date(),</div>
<div id="_mcePaste">get_comment_time(),</div>
<div id="_mcePaste">human_time_diff(get_comment_date('U'), current_time('timestamp')) ); ?&gt;&lt;/div&gt;</div>
<div id="_mcePaste">&lt;?php if ($comment-&gt;comment_approved == '0') _e("\t\t\t\t\t&lt;span class='unapproved'&gt;Your comment is awaiting moderation.&lt;/span&gt;\n", 'matty') ?&gt;</div>
<div id="_mcePaste">&lt;div&gt;&lt;?php comment_text(); ?&gt;&lt;/div&gt;&lt;!--/.comment-content--&gt;</div>
<div id="_mcePaste">&lt;div&gt;&lt;?php printf(__('&lt;a href="%1$s" rel="bookmark" title="Permalink to ' . $comment-&gt;comment_author . '\'s comment"&gt;Permalink&lt;/a&gt;', 'matty'),</div>
<div id="_mcePaste">'#comment-' . $comment-&gt;comment_ID );</div>
<div id="_mcePaste">edit_comment_link(__('Edit', 'matty'), '&lt;span&gt;', '&lt;/span&gt;'); echo get_comment_reply_link(array_merge( $args, array('depth' =&gt; $depth, 'max_depth' =&gt; $args['max_depth']))) ?&gt;&lt;/div&gt;&lt;!--/.comment-links--&gt;</div>
<div id="_mcePaste">&lt;?php</div>
<div id="_mcePaste">} // End matty_comment_layout()</div>
<div id="_mcePaste">?&gt;</div>
</pre>
<p>Okay, that&#8217;s a long snippet. Lets go through it.</p>
<pre><span style="font-family: Consolas, Monaco, 'Courier New', Courier, monospace; line-height: 18px; font-size: 12px; white-space: pre;">function matty_comment_layout ( $comment, $args, $depth ) {</span></pre>
<p>This is our opening function tag. What&#8217;s important to note here is the passing of three arguments: the $comment we&#8217;ll be displaying, the $args that go along with that comment and the $depth of the comment in the structure.</p>
<pre>$GLOBALS['comment'] = $comment;
$path_to_default_gravatar = get_bloginfo('stylesheet_directory') . '/assets/images/gravatar.png';
$twitter = '';
$twitter = get_comment_meta( get_comment_ID(), 'twitter', true );

if ( $twitter != '' ) {

$twitter = strtolower($twitter);
$twitter = str_replace(' ', '', $twitter);
$twitter = str_replace('.', '', $twitter);
$twitter = ' &lt;span&gt;(&lt;a href="http://twitter.com/' . $twitter . '" rel="nofollow"&gt;' . $twitter . '&lt;/a&gt;)&lt;/span&gt;';

} // End IF Statement

// Sanitise gravatar and make sure the image has an ALT attribute that isn't empty.
$gravatar = get_avatar($comment,$size='48',$default=$path_to_default_gravatar );
$gravatar = str_replace ("alt=''", 'alt="' . $comment-&gt;comment_author . '\'s Gravatar"', $gravatar);</pre>
<p>I&#8217;ve bundled the above together as it&#8217;s all extra information that isn&#8217;t directly part of the comment itself.</p>
<p>The first line is to ensure we have the correct data for our comment. Below that, $path_to_default_gravatar is where we set the URL of a custom gravatar that we would like to use to replace the default gravatar used. This can be really useful when doing advanced premium WordPress themes. Now, onto the Twitter username.</p>
<p>We create a variable called $twitter, to which we will assign whatever is returned from the native WordPress function, get_comment_meta(). This function takes in the ID of the comment we&#8217;re currently processing, the field name (in this case &#8220;twitter&#8221;) and whether to expect a single value, instead of an array of values (in most cases, this should just be set to true, as we&#8217;re only expecting a single value here). If $twitter isn&#8217;t empty, we remove and spaces or full-stops from it and create an anchor XHTML tag pointing to the user&#8217;s Twitter profile page.</p>
<p>The last two lines get the user&#8217;s gravatar and make sure it has alternative text, to keep things neat and tidy. We&#8217;ve also told the get_avatar() function that, should it not find a gravatar for the user, it must use $path_to_default_gravatar as the default gravatar to display.</p>
<p>The rest of the comment function looks like this:</p>
<pre style="font: normal normal normal 12px/18px Consolas, Monaco, 'Courier New', Courier, monospace;">
<div id="_mcePaste">&lt;li &lt;?php comment_class(); ?&gt; id="comment-&lt;?php echo $comment-&gt;comment_ID; ?&gt;"&gt;</div>
<div id="_mcePaste">&lt;div&gt;&lt;?php echo $gravatar; ?&gt;&lt;?php printf( __('&lt;cite&gt;%s&lt;/cite&gt;'), get_comment_author_link(), $twitter ); ?&gt;&lt;/div&gt;</div>
<div id="_mcePaste">&lt;div&gt;&lt;?php printf(__('Posted %1$s at %2$s (%3$s ago)', 'matty'),</div>
<div id="_mcePaste">get_comment_date(),</div>
<div id="_mcePaste">get_comment_time(),</div>
<div id="_mcePaste">human_time_diff(get_comment_date('U'), current_time('timestamp')) ); ?&gt;&lt;/div&gt;</div>
<div id="_mcePaste">&lt;?php if ($comment-&gt;comment_approved == '0') _e("\t\t\t\t\t&lt;span class='unapproved'&gt;Your comment is awaiting moderation.&lt;/span&gt;\n", 'matty') ?&gt;</div>
<div id="_mcePaste">&lt;div&gt;&lt;?php comment_text(); ?&gt;&lt;/div&gt;&lt;!--/.comment-content--&gt;</div>
<div id="_mcePaste">&lt;div&gt;&lt;?php printf(__('&lt;a href="%1$s" rel="bookmark" title="Permalink to ' . $comment-&gt;comment_author . '\'s comment"&gt;Permalink&lt;/a&gt;', 'matty'),</div>
<div id="_mcePaste">'#comment-' . $comment-&gt;comment_ID );</div>
<div id="_mcePaste">edit_comment_link(__('Edit', 'matty'), '&lt;span&gt;', '&lt;/span&gt;'); echo get_comment_reply_link(array_merge( $args, array('depth' =&gt; $depth, 'max_depth' =&gt; $args['max_depth']))) ?&gt;&lt;/div&gt;&lt;!--/.comment-links--&gt;</div>
</pre>
<p>The one very important thing to note here is the lack of a closing &lt;/li&gt; tag. WordPress inserts this automatically for us. While this still puzzles me, I&#8217;m sure there&#8217;s a decently good reason for it.</p>
<p>This code looks a bit random, doesn&#8217;t it? Well, it&#8217;s not really all that bad. Lets take a look.</p>
<p>We open the &lt;li&gt; tag and call the comment_class() WordPress function. This adds necessary microformats and comment-specific classes to the &lt;li&gt; for this comment. We also give it a unique ID.</p>
<p>Next up, we create a containing &lt;div&gt; tag and echo out our $gravatar image, as well as a prepared statement containing the comment author&#8217;s name and a link to their website (via the get_comment_author_link() function) and our $twitter URL, created in the snippet above.</p>
<p>Below the comment author information, we echo out a prepared statement with three values: the comment date, comment time and a human-readable version of how long ago the comment was posted (for example, 3 days ago).<br />
If the comment has not yet been approved, we let the user know this with <code>&lt;?php if ($comment-&gt;comment_approved == '0') _e("\t\t\t\t\t&lt;span class='unapproved'&gt;Your comment is awaiting moderation.&lt;/span&gt;\n", 'matty') ?&gt;</code>.</p>
<p>Now for the main event. The comment itself. A simple function called <code>comment_text()</code> gets this part of the show done.</p>
<p>Next up we create links that apply to this comment. In the above snippet, we create a permalink to the comment, an edit link for users who are logged in and have access to edit comments and a reply link (if we&#8217;re not too deep into the comments thread) that users can use to reply to the comment.</p>
<h3>That&#8217;s all, folks!</h3>
<p>Yep, that&#8217;s it. In the above snippet (which is almost a store-and-use-without-thinking snippet, in my opinion), we&#8217;ve created a customised layout for the way each comment is displayed, integrated a custom default gravatar, displayed a human-readable version of how long ago the comment was posted and integrated a Twitter username, submitted by the user when the comment was first made.</p>
<p>I&#8217;d really appreciate your thoughts on and ideas for this in the comments below.</p>
<p>Pretty cool, hey? <img src='http://matty.co.za/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://matty.co.za/2010/05/enhancing-wordpress-comments/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Storing a Twitter username with comments in WordPress</title>
		<link>http://matty.co.za/2010/05/storing-twitter-username-with-wordpress-comments/</link>
		<comments>http://matty.co.za/2010/05/storing-twitter-username-with-wordpress-comments/#comments</comments>
		<pubDate>Sat, 01 May 2010 09:32:29 +0000</pubDate>
		<dc:creator>Matty</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[WordPress Support]]></category>
		<category><![CDATA[WordPress Themes]]></category>

		<guid isPermaLink="false">http://matty.co.za/?p=1019</guid>
		<description><![CDATA[We&#8217;ve all seen this before when commenting on a blog post we&#8217;ve just read. The standard comment form on a WordPress-driven website asks for a user&#8217;s name, email address (not published), website address and their comment. What if we could get some other information from the user*, and later integrate that into their comment? Why [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://twitter.com/mattyza"><img class="alignright size-full wp-image-1029" title="Twitter" src="http://matty.co.za/wp-content/uploads/2010/05/twitter.png" alt="Follow me on Twitter" width="174" height="100" /></a>We&#8217;ve all seen this before when commenting on a blog post we&#8217;ve just read. The standard comment form on a WordPress-driven website asks for a user&#8217;s name, email address (not published), website address and their comment. What if we could get some other information from the user*, and later integrate that into their comment? Why not get their Twitter username and link back to their Twitter profile as well as to their website? This tutorial will explain how to do just that.</p>
<p><em>* While this tutorial uses a Twitter username as an example, virtually any additional information supplied by the user can be stored along with their comment (a rating, a selection of their social media profiles, etc).<span style="font-style: normal;"><span id="more-1019"></span></span></em></p>
<h3>Asking for it by name (aka. creating the Twitter username textfield)</h3>
<p>Here we&#8217;ll add a field to the comment form, in which the user can input their Twitter username. The snippet of code I&#8217;ve used for this is as follows:</p>
<pre>&lt;p&gt;&lt;label for="twitter"&gt;Twitter&lt;/label&gt;&lt;input type="text" name="twitter" id="twitter" value="" /&gt;&lt;/p&gt;</pre>
<p>The above code adds a field to the comment form of the theme in question. I&#8217;d recommend making a copy of the way the website field is generated and replacing all instances of the word &#8220;website&#8221; with the word &#8220;twitter&#8221;. This is an easy way to ensure consistency with the theme to which you are adding this functionality.</p>
<h3>Okay, lets store this thing!</h3>
<p>Right. The user has entered their Twitter username. Lets store it along with the comment, using the comment meta functionality provided by WordPress.</p>
<p>What we&#8217;ll be doing here is the following:</p>
<ol>
<li>Check if the user inputted a Twitter username.</li>
<li>Sanitize the username.</li>
<li>If there are any characters left after we&#8217;ve sanitised the username (it removes all unwanted characters), store the Twitter username along with the comment.</li>
</ol>
<p>And here&#8217;s how that looks in code:</p>
<pre>
<div id="_mcePaste">&lt;?php</div>
<div id="_mcePaste">/**</div>
<div id="_mcePaste">* Storing the commenter's Twitter username.</div>
<div id="_mcePaste">*/</div>
<div id="_mcePaste">function matty_store_twitter_username ( $post_id ) {</div>
<div id="_mcePaste">$twitter_username = $_POST['twitter'];</div>
<div id="_mcePaste">if ( $twitter_username ) {</div>
<div id="_mcePaste">$twitter_username = sanitize_user( $twitter_username, true );</div>
<div id="_mcePaste">$twitter_username = str_replace( ' ', '', $twitter_username );</div>
<div id="_mcePaste">$twitter_username = str_replace( '.', '', $twitter_username );</div>
<div id="_mcePaste">} // End IF Statement</div>
<div id="_mcePaste">if ( $twitter_username ) {</div>
<div id="_mcePaste">add_comment_meta( $post_id, 'twitter', $twitter_username, true );</div>
<div id="_mcePaste">} // End IF Statement</div>
<div id="_mcePaste">} // End matty_store_twitter_username()</div>
<div id="_mcePaste">add_action( 'comment_post', 'matty_store_twitter_username', 1 );</div>
<div id="_mcePaste">?&gt;</div>
</pre>
<p>Pretty simple, right?</p>
<p>If you make use of the above or know of other ways of achieving this, please share your thoughts in the comments below.</p>
<p>In a follow-up tutorial, I&#8217;ll discuss how to integrate this stored Twitter username into comments on your WordPress website, using a custom comment callback function.</p>
<p><strong>2010-05-03:</strong> Code updated thank to feedback from Ben in the comments below. Thanks man. <img src='http://matty.co.za/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://matty.co.za/2010/05/storing-twitter-username-with-wordpress-comments/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Theming for Magento — Part 04 &#8211; How it all works</title>
		<link>http://matty.co.za/2010/04/magento-themes-part-04/</link>
		<comments>http://matty.co.za/2010/04/magento-themes-part-04/#comments</comments>
		<pubDate>Sun, 25 Apr 2010 08:42:15 +0000</pubDate>
		<dc:creator>Matty</dc:creator>
				<category><![CDATA[Magento]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Magento Themes]]></category>
		<category><![CDATA[Theming]]></category>

		<guid isPermaLink="false">http://matty.co.za/?p=917</guid>
		<description><![CDATA[Wow, part 4 already? This is where the real fun comes in, folks. Today we&#8217;ll be opening up some *.phtml and *.xml files, looking at what&#8217;s going on under the hood and finding out how the theme files all tie together. Lets get started, shall we? Before we get to those files&#8230; Folks, before we [...]]]></description>
			<content:encoded><![CDATA[<p>Wow, part 4 already? This is where the real fun comes in, folks. Today we&#8217;ll be opening up some *.phtml and *.xml files, looking at what&#8217;s going on under the hood and finding out how the theme files all tie together. Lets get started, shall we?</p>
<h3>Before we get to those files&#8230;</h3>
<p>Folks, before we dive into the files, lets get some concepts down that are crucial to understanding how the Magento theme files play together.</p>
<p>Magento themes consist of, aside from styling assets (CSS, images, Javascript, etc) a collection of *.phtml (essentially, XHTML files that can execute PHP) and *.xml (Extensible Markup Language) files that make the theme work.</p>
<p>The *.xml files define blocks and regions, which template files get used for which blocks &amp; regions as well as various attributes and parameters for each if these definitions, where applicable.</p>
<p>While the *.xml files tell everything where to be and what to look like, the *.phtml files contain the actual XHTML code required for each block and region.</p>
<ul class="messages">
<li>
<div class="notify-msg">At the time of writing this tutorial, I have upgraded my Magento installation to Community Edition 1.4. All further tutorials, as well as this one, will be  referencing Magento Community Edition 1.4 unless otherwise stated. Please be sure you have the latest version.</div>
</li>
</ul>
<p><span id="more-917"></span></p>
<h4>Blocks and regions? Huh?</h4>
<p>Yes, blocks and regions.</p>
<p>Magento has a system called &#8220;Static Blocks&#8221;, which allows theme authors to define areas which are updatable via the administration console using a simple textarea. This is used, by default, in the footer as well as for one or two other areas, and can be very  useful for allowing the theme users to update small chunks of content on their store. These are referred to in this tutorial as &#8220;blocks&#8221;.</p>
<p>Regions. This is where we need to get conceptual.</p>
<p>The easiest way I&#8217;ve found to explain this concept is by comparing regions to widgetised areas in WordPress. When users create widgetised areas in WordPress, they are creating areas that are open for the user to insert blocks of content (widgets) that will take on appropriate styling. The region concept is similar.</p>
<p>In Magento, by default, certain blocks are placed in certain regions (for example, Layered Navigation in the left column). This is the reason why it is so important to make use of all possible template types when theming for Magento (3 columns, 2 columns, 1 column, etc), as there is a tried and tested reason why each block is placed in a particular region. This can, however, be modified via the *.xml file (which does all the &#8220;do this, do that&#8221;, remember?), so its not a major worry if you need to move a block.</p>
<h3>So, what&#8217;s a *.phtml file anyway?</h3>
<p>A *.phtml file is a template file. It can either define a page template to be used when displaying a particular type of page (product page, category page, CMS page, etc) or a template to be used in a particular area, for example, the way each single product is displayed when a category is in list mode or grid mode, or the way your footer links static block (activated by default when Magento is installed) is displayed within your theme.</p>
<p>Lets take a look at a fairly standard *.phtml file, 2columns-right.phtml, found in your theme&#8217;s directory within the &#8220;app &gt; design&#8221; folder.</p>
<pre name="code" class="php">&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="&lt;?php echo $this-&gt;getLang() ?&gt;" lang="&lt;?php echo $this-&gt;getLang() ?&gt;"&gt;
&lt;head&gt;
&lt;?php echo $this-&gt;getChildHtml('head') ?&gt;
&lt;/head&gt;
&lt;body&lt;?php echo $this-&gt;getBodyClass()?' class="'.$this-&gt;getBodyClass().'"':'' ?&gt;&gt;
&lt;?php echo $this-&gt;getChildHtml('after_body_start') ?&gt;
&lt;div class="wrapper"&gt;
    &lt;?php echo $this-&gt;getChildHtml('global_notices') ?&gt;
    &lt;div class="page"&gt;
        &lt;?php echo $this-&gt;getChildHtml('header') ?&gt;
        &lt;div class="main-container col2-right-layout"&gt;
            &lt;div class="main"&gt;
                &lt;?php echo $this-&gt;getChildHtml('breadcrumbs') ?&gt;
                &lt;div class="col-main"&gt;
                    &lt;?php echo $this-&gt;getChildHtml('global_messages') ?&gt;
                    &lt;?php echo $this-&gt;getChildHtml('content') ?&gt;
                &lt;/div&gt;
                &lt;div class="col-right sidebar"&gt;&lt;?php echo $this-&gt;getChildHtml('right') ?&gt;&lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;
        &lt;?php echo $this-&gt;getChildHtml('footer') ?&gt;
        &lt;?php echo $this-&gt;getChildHtml('before_body_end') ?&gt;
    &lt;/div&gt;
&lt;/div&gt;
&lt;?php echo $this-&gt;getAbsoluteFooter() ?&gt;
&lt;/body&gt;
&lt;/html&gt;</pre>
<p>The above code is a direct copy/paste of the 2columns-right.phtml file, packaged with the base &gt; default theme in Magento 1.4. The use of $this-&gt;getChildHtml() is directed at retrieving specific template files, declared within the page.xml file. The other functions, getAbsoluteFooter() and getBodyClass() are, for the scope of this tutorial, not relevant.</p>
<h3>&#8230; and the XML?</h3>
<p>This file defines our regions, sets up blocks within those regions and tells each region which template to use. It is also used for loading theme-specific CSS and JavaScript, as well as activating and deactivating blocks within specific regions in particular sections of your Magento theme. Essentially, the XML file controls everything related to the module in question.</p>
<p>In most cases, the majority of your time when theming will be spent within page.xml. As this file is well and truly gigantic for pasting into a blog post (approximately 182 lines), I&#8217;ll paste just the relevant snippets for getting the aim of this tutorial across.</p>
<pre>&lt;block type="core/text_list" name="left" as="left" translate="label"&gt;
&lt;label&gt;Left Column&lt;/label&gt;
&lt;/block&gt;</pre>
<div>What you see above is the creation of the region called &#8220;left&#8221;. This is telling Magento that we have a region called &#8220;left&#8221;, to which blocks in a multitude of modules can be assigned. This is a blank area of the theme, to which content, in the form of blocks, is pointed (emphasising the point, there).</div>
<pre name="code" class="php">
<div id="_mcePaste">&lt;reference name="left"&gt;</div>
<div id="_mcePaste">&lt;block type="core/template" name="left.permanent.callout" template="callouts/left_col.phtml"&gt;</div>
<div id="_mcePaste">&lt;action method="setImgSrc"&gt;&lt;src&gt;images/media/col_left_callout.jpg&lt;/src&gt;&lt;/action&gt;</div>
<div id="_mcePaste">&lt;action method="setImgAlt" translate="alt" module="catalog"&gt;&lt;alt&gt;Our customer service is available 24/7. Call us at (555) 555-0123.&lt;/alt&gt;&lt;/action&gt;</div>
<div id="_mcePaste">&lt;action method="setLinkUrl"&gt;&lt;url&gt;checkout/cart&lt;/url&gt;&lt;/action&gt;</div>
<div id="_mcePaste">&lt;/block&gt;</div>
<div id="_mcePaste">&lt;/reference&gt;</div>
</pre>
<p>The block of code above is from catalog.xml, the controller XML file for all things related to the Magento catalog. The above block of code creates one of the default callout blocks (a block with an image and a URL) that is bundled with the default Magento theme. Lets spend some time on this block of code.</p>
<h3>So&#8230; how do I use these files?</h3>
<p>The first and last lines, in this case, are the most important. They tell Magento that anything within them (ie: the block inside the &lt;reference&gt; tags) will be assigned to the &#8220;left&#8221; region. Inside that, the &lt;block&gt; tags setup the callout block with it&#8217;s various attributes. As the &lt;action&gt; tags are rather specific to this callout (they use specific image-related methods to setup the source of the image, it&#8217;s alternate text and the URL to which it points), lets focus on the opening &lt;block&gt; tag.</p>
<p>The opening &lt;block&gt; tag has the following attributes: type, name and template. These do exactly what they say on the tin.</p>
<p>type- Tells Magento what type of block this is (it could be a product list, catalog, a display of only new products, etc). This is dependent on the type of content within the block. In this case, it&#8217;s a raw image using a specific template, so it&#8217;s a core/template.</p>
<p>name- What this block is referred to as within the XML files.</p>
<p>template- What template file is used to render this block within the theme. In this case, it&#8217;s a template called left_col.phtml within the &#8220;callouts&#8221; folder. This file looks like this:</p>
<pre name="code" class="php">
<div class="block block-banner">
<div class="block-content">
   &lt;div class="block block-banner"&gt;</div>

&lt;div class="block-content"&gt;
        &lt;?php if(strtolower(substr($this-&gt;getLinkUrl(),0,4))==='http'): ?&gt;
            &lt;a href="&lt;?php echo $this-&gt;getLinkUrl() ?&gt;" title="&lt;?php echo $this-&gt;__($this-&gt;getImgAlt()) ?&gt;"&gt;
        &lt;?php elseif($this-&gt;getLinkUrl()): ?&gt;
            &lt;a href="&lt;?php echo $this-&gt;getUrl($this-&gt;getLinkUrl()) ?&gt;" title="&lt;?php echo $this-&gt;__($this-&gt;getImgAlt()) ?&gt;"&gt;
        &lt;?php endif; ?&gt;
            &lt;img src="&lt;?php echo $this-&gt;getSkinUrl($this-&gt;getImgSrc()) ?&gt;"&lt;?php if(!$this-&gt;getLinkUrl()): ?&gt; title="&lt;?php echo $this-&gt;__($this-&gt;getImgAlt()) ?&gt;"&lt;?php endif; ?&gt; alt="&lt;?php echo $this-&gt;__($this-&gt;getImgAlt()) ?&gt;" /&gt;
        &lt;?php if($this-&gt;getLinkUrl()): ?&gt;
        &lt;/a&gt;
        &lt;?php endif ?&gt;
    &lt;/div&gt;
&lt;/div&gt;</div>
</pre>
<p>Above you can see some familiar functions such as getImgSrc() (related to setImgSrc() within the XML file). These functions retrieve the data that is set within the &lt;reference&gt; tags in the XML file.</p>
<h3>&#8230; and to sum it all up &#8230;</h3>
<p>Okay. Lets sum this up in a single paragraph.</p>
<p>Wherever dynamic content is set within your theme, it is more often than not done using a region. These regions are usually set within the page.xml file, as the &#8220;page&#8221; module defines the main skeleton of the Magento theme. Blocks specific to the module they work with are set in that modules XML file, with a &lt;reference&gt; to the region in which they are displayed. A template file, usually stored within the module in question, is used to render the content that is retrieved by the block it is used on.</p>
<p>Wow, this was a busy tutorial. There&#8217;s a lot to take in above, folks. If any of it seems unclear, please leave a comment and I&#8217;ll do my best to help clarify things.</p>
]]></content:encoded>
			<wfw:commentRss>http://matty.co.za/2010/04/magento-themes-part-04/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Javascript and WordPress &#8211; The Definitive Guide</title>
		<link>http://matty.co.za/2010/03/enqueue-javascript-in-wordpress/</link>
		<comments>http://matty.co.za/2010/03/enqueue-javascript-in-wordpress/#comments</comments>
		<pubDate>Sun, 14 Mar 2010 12:34:25 +0000</pubDate>
		<dc:creator>Matty</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[WordPress Plugins]]></category>
		<category><![CDATA[WordPress Support]]></category>
		<category><![CDATA[WordPress Themes]]></category>

		<guid isPermaLink="false">http://matty.co.za/?p=905</guid>
		<description><![CDATA[Using custom JavaScript code in a WordPress theme or plugin is, in many cases, a given. Fortunately, WordPress comes bundled with a selection of popular Javascript libraries (jQuery, Prototype and others) for use with your plugins and themes. Many users, however, simply write the `&#60;script&#62;` tags in the header.php file of their theme or as [...]]]></description>
			<content:encoded><![CDATA[<p>Using custom JavaScript code in a WordPress theme or plugin is, in many cases, a given. Fortunately, WordPress comes bundled with a selection of popular Javascript libraries (jQuery, Prototype and others) for use with your plugins and themes. Many users, however, simply write the `&lt;script&gt;` tags in the header.php file of their theme or as part of a function in their plugin that is run in the header of the theme being used. This is a potential problem area that can have you, the developer, sitting for ages looking at your code and wondering why plugin `X` isn&#8217;t working correctly when theme `Y` is active. This guide aims to provide an understanding of how to correctly enqueue Javascript in WordPress and how to avoid potential Javascript conflicts.</p>
<h3>Okay, what are we doing here?</h3>
<p>We&#8217;re going to enqueue the Javascript files, used by our WordPress plugin or theme, using the correct method and the <code>wp_enqueue_script()</code>function. We will be adding an action to the wp_print_scripts() action and, inside a function within our theme or plugin, running the <code>wp_enqueue_script()</code> function. We will also be including the Javascript in the administration area only, creating dependencies between our various custom Javascript files and enqueuing the scripts on only a specific page in the administration area.<span id="more-905"></span></p>
<h3>How do we enqueue the Javascript?</h3>
<p>Okay, you will need:</p>
<ol>
<li>1 x custom function in either your theme&#8217;s functions.php file or in your plugin.</li>
<li>1 x selection of Javascript files required by your theme or plugin.</li>
<li>1 x <code>add_action()</code> line within your code.</li>
</ol>
<p>Lets follow this like a recipe. The above ingredients each form part of the recipe we need to follow in order to correctly enqueue Javascript in WordPress.</p>
<h4>1. Write a function in your theme or plugin that will run the various enqueue commands.</h4>
<p>This function is the heart of enqueuing the JavaScripts in WordPress. It tells the system which scripts to include, what they require (if anything) and in what order to enqueue the scripts. An example of this function may look like this:<br />
<code>function matty_enqueue_theme_js () {<br />
wp_enqueue_script('matty-functions', get_template_directory_uri() . 'js/functions.js', array('jquery'), '1.0', false);<br />
} // End matty_enqueue_theme_js()</code></p>
<h4>Okay. Lets run through this function and work out what it does.</h4>
<p>For each Javascript file you wish to include, add another <code>wp_enqueue_script()</code> line to the function. This line reads as follows:</p>
<p>The name by which I would like to refer to this JavaScript file is `matty-functions`. It is located at get_template_directory_uri() . &#8216;js/functions.js&#8217; *, requires jQuery to run and has a version number of 1.0. The `false` at the end denotes whether or not the script is to be loaded in the footer of the theme (on <code>wp_footer()</code>). If false, the script will load in <code>wp_head()</code> of the current theme.</p>
<p>Each parameter can be explained as follows:</p>
<p><strong>$handle</strong> &#8211; A lowercase text string, no spaces, that is the &#8220;identifier&#8221; of the JavaScript file being loaded on that line. Can be used if one of your files requires another of your files in order to run.</p>
<p><strong>$src</strong> &#8211; The full URL path to the Javascript file being loaded.</p>
<p><strong>$deps</strong> &#8211; Does your script require anything to run? It could require a specific library (for example, jQuery) or another of your Javascript files being loaded in (referred to by its $handle).</p>
<p><strong>$ver</strong> &#8211; The version number of the script being loaded. This is an optional parameter.</p>
<p><strong>$in_footer</strong> &#8211; Should the script be loaded in the footer or in the header? If set to false, the script will load in the header. This is an optional parameter that defaults to `false`.</p>
<p>And hey presto! We&#8217;ve got a function that we can tell to include all our JavaScripts!</p>
<h4>Run the function when the WordPress wp_print_scripts() action occurs.</h4>
<p>No sweat. We&#8217;re going to run the above function on the WordPress <code>wp_head()</code>action (when the system initialises). This is how we do it: <code>add_action('wp_print_scripts', 'matty_enqueue_theme_js', 1);</code></p>
<p>Explaination? Okay.</p>
<p>When the WordPress <code>wp_print_scripts()</code> function runs, add the `matty_enqueue_theme_js` function to the list of functions to run. Give it a priority setting of 1 (this can be used to control which functions execute before others).</p>
<p>Easy, right?</p>
<h3>What JavaScript libraries are already bundled with WordPress?</h3>
<p>In a word; loads. Here&#8217;s a comprehensive list of <a title="List of JavaScript libraries bundled with WordPress" href="http://codex.wordpress.org/Function_Reference/wp_enqueue_script#Default_scripts_included_with_WordPress">Javascript libraries bundled with WordPress</a>. Try to stick to one library, as it will greatly minimize potential conflicts between your scripts. Where possible, be sure to use the bundled version of the script your code requires, as it will minimize load times and generally be a lot easier to work with your scripts (unless the version you require is not yet bundled with WordPress&#8230; *cough* jQuery 1.4 *cough*.).</p>
<p>I only want the Javascript in the WordPress aministration area. How do I do that?</p>
<p>No sweat. Simply run the following script instead of the above <code>add_action()</code>:</p>
<p><code>add_action('admin_print_scripts', 'matty_enqueue_theme_js');</code></p>
<p>To include the Javascript on only one particular administration page, run something like the following:</p>
<p><code>add_action('admin_print_scripts-widgets.php', 'matty_enqueue_theme_js');</code></p>
<p>The above line will enqueue the JavaScript only on the widgets screen of the administration area.</p>
<p>One of my scripts relies on another. What do I do?</p>
<p>Remember that `$handle` parameter we discussed earlier? Lets use it.</p>
<p>When one script relies on another script (note: not a bundled library but another custom Javascript file), the &#8216;handle&#8217; parameter is used in the `$deps` to let the script know that another script is required. Lets take a look at our function again:</p>
<p><code>function matty_enqueue_theme_js () {<br />
wp_enqueue_script('matty-some-cool-code', get_template_directory_uri() . 'js/some-cool-code.js', array('jquery'), '1.0', false);<br />
wp_enqueue_script('matty-functions', get_template_directory_uri() . 'js/functions.js', array('jquery', 'matty-some-cool-code'), '1.0', false);<br />
} // End matty_enqueue_theme_js()</code></p>
<p>See what we did there? We added the handle of the first line to the dependencies array of the second line. Therefore, &#8216;matty-some-cool-code&#8217; requires jQuery ** to run, and &#8216;matty-functions&#8217; requires both jQuery and &#8216;matty-some-cool-code&#8217; in order to run. Please note that, because I have specified that my scripts require jQuery, there is no need to load the jQuery library itself on it&#8217;s own line.</p>
<h3>Right. Lets sum it up.</h3>
<p>What did we discuss here today? We learned how to correctly enqueue Javascript files in WordPress, how to create dependencies between files and how to load Javascript libraries that come bundled with WordPress.</p>
<p>And all the code we discussed above:<br />
<code>function matty_enqueue_theme_js () {<br />
wp_enqueue_script('matty-some-cool-code', get_template_directory_uri() . 'js/some-cool-code.js', array('jquery'), '1.0', false);<br />
wp_enqueue_script('matty-functions', get_template_directory_uri() . 'js/functions.js', array('jquery', 'matty-some-cool-code'), '1.0', false);<br />
} // End matty_enqueue_theme_js()</code></p>
<p><code>add_action( 'wp_print_scripts', 'matty_enqueue_theme_js', 1 );</code><br />
I hope this post is found useful. If you have any queries, please post them in the comments.</p>
<p><em>* <code>get_template_directory_uri()</code> should be changed to <code>get_stylesheet_directory_uri()</code> if the JavaScript file is within a child theme.</em></p>
<p><em>** Notice how jQuery is required by both JavaScripts but is only loaded once in your code? This is why we use the <code>wp_enqueue_script()</code> action&#8230; to avoid a) multiple versions or instances of a script being included and b) to avoid script conflicts because of these load issues.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://matty.co.za/2010/03/enqueue-javascript-in-wordpress/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>A few guidelines for WordPress plugin development</title>
		<link>http://matty.co.za/2009/06/guidelines-for-wordpress-plugin-development/</link>
		<comments>http://matty.co.za/2009/06/guidelines-for-wordpress-plugin-development/#comments</comments>
		<pubDate>Sat, 20 Jun 2009 08:12:02 +0000</pubDate>
		<dc:creator>Matty</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Web Developer]]></category>
		<category><![CDATA[WordPress Plugins]]></category>

		<guid isPermaLink="false">http://matty.co.za/?p=510</guid>
		<description><![CDATA[The WordPress plugin API is vast and powerful. It allows developers to essentially hook code into almost any area of the WordPress system without modifying the core files at all. It also allows for the creation of standalone plugins that work within the WordPress system but do not hook into the core modules. Over the [...]]]></description>
			<content:encoded><![CDATA[<p>The WordPress plugin API is vast and powerful. It allows developers to essentially hook code into almost any area of the WordPress system without modifying the core files at all. It also allows for the creation of standalone plugins that work within the WordPress system but do not hook into the core modules.</p>
<p>Over the last few weeks, WordPress plugin development has become one of my favourite things to do. I find it exciting to be able to create functionality, incorporate it seemlessly into the WordPress system and see it work smoothly with the other modules. While plugin development for WordPress is incredibly powerful, it also carries with it a few areas where people commonly stumble over and potentially lose interest in their code&#8230; which could be the next big thing. Here are a few guidelines I&#8217;ve picked up in order to step over the stumbling blocks.<br />
<span id="more-510"></span></p>
<h3>1. Understand actions, filters and hooks, where to use which action and their limitations.</h3>
<p><a title="Wordpress Actions Reference" href="http://codex.wordpress.org/Plugin_API/Action_Reference" target="_blank">Actions, filters and hooks</a> are what allow developers to insert their code into virtually any part of the WordPress system. While there are only almost 400 hooks documented, there are in fact around 800 hooks in the WordPress system. The WordPress codex has a great reference for actions, filters, hooks and how to use them.</p>
<h3>2. <code>register_activation_hook()</code> does not function the same way as <code>add_action()</code>.</h3>
<p>While looking through the WordPress forums, the <code>register_activation_hook()</code> function (runs the function you pass to it when the plugin is activated) has come up as a regular topic of conversation. Users are attempting to display text on activation, using it as if it were the add_action() function. register_activation_hook() does not function the same way. The function, the way I understand it, works in an almost overall scope, not having access to variables within your plugin for display purposes. In short, don&#8217;t try to echo or print variables with this function. It won&#8217;t work. The function is best used for doing common setup such as installing database tables, setting default options, etc.</p>
<h3>3. Use <code>dbDelta</code> instead of <code>mysql_query</code> or <code>$wpdb-&gt;query()</code> when installing database tables.</h3>
<p>The dbDelta function (which you pass your database query to) is used when installing tables into your WordPress database. It is also used as an upgrade script in the same area. Don&#8217;t forget to include the WordPress upgrade script before running <code>dbDelta()</code>. The include looks like this:</p>
<p><code>require_once(ABSPATH . 'wp-admin/includes/upgrade.php');</code></p>
<h3>4. Understand the scope of your plugin and work within it.</h3>
<p>As mentioned above, WordPress plugins can be used to hook into essentially any area of the WordPress system. This includes standalone menus, as well as running actions when a post or page is saved, for example. Understanding the objective/s and scope of one&#8217;s WordPress plugin is essential to choosing the correct approach for the plugin. If it&#8217;s a short plugin with specific objective, there&#8217;s no need, in my opinion, for more than one or two files. Keep it simple. On the other hand, if the plugin is a large, standalone module, I don&#8217;t believe there&#8217;s any reason to have cluttered files of hundreds of lines of code, where an Object Oriented approach would ultimately serve as a much cleaner and more extendable solution.</p>
<h3>5. Plan, plan, plan.</h3>
<p>Planning on paper, drafting a code map on screen, talking it through with other developers. Planning a plugin can never be done too much. It helps with all of the above steps as well as helping to see where you need to be at the end of the development and planning a solid roadmap for the journey to get there. Also, other users may have ideas that could streamline your plugin even further.</p>
<p>These are just a few things I&#8217;ve picked up on my WordPress plugin development journey. I&#8217;ll post more if and when they come up. I hope these tips are useful and help in overcoming the potential stumbling blocks when getting started with WordPress plugin development.</p>
]]></content:encoded>
			<wfw:commentRss>http://matty.co.za/2009/06/guidelines-for-wordpress-plugin-development/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced
Object Caching 631/684 objects using disk: basic

Served from: matty.co.za @ 2012-05-11 02:02:18 -->
