<?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>Palehorse Information Services &#187; code</title>
	<atom:link href="http://palehorseinformation.com/tag/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://palehorseinformation.com</link>
	<description>Bridging the gap between web development and web design</description>
	<lastBuildDate>Thu, 24 Dec 2009 15:45:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Fixing The Excerpt In WordPress</title>
		<link>http://palehorseinformation.com/2009/12/23/fixing-the-wordpress-excerpt/</link>
		<comments>http://palehorseinformation.com/2009/12/23/fixing-the-wordpress-excerpt/#comments</comments>
		<pubDate>Thu, 24 Dec 2009 02:07:48 +0000</pubDate>
		<dc:creator>Duncan MacLeod</dc:creator>
				<category><![CDATA[WordPress]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[how-to]]></category>

		<guid isPermaLink="false">http://palehorseinformation.com/?p=74</guid>
		<description><![CDATA[WordPress theme designers, as well as "hobbyist" designers will most likely be familiar with WordPress' <code>the_excerpt()</code> Template Tag; a post excerpt is displayed as a summary of the post wherever <code>the_excerpt()</code> is used. However it does have some limitations. Here we attempt to improve on Aaron Russell's improvement of this function.]]></description>
			<content:encoded><![CDATA[<p><span class="drop_cap">W</span><span class="first_word">ordPress</span> theme designers, as well as &#8220;hobbyist&#8221; designers will most likely be familiar with WordPress&#8217; <a href="http://codex.wordpress.org/Template_Tags/the_excerpt"><code>the_excerpt()</code> Template Tag</a>; a post excerpt is displayed as a summary of the post wherever <code>the_excerpt()</code> is used. However it does have some limitations, such as stripping HTML tags, and <i>not</i> stripping JavaScript.</p>

<p>The best solution we have found to these limitations was provided by <a href="http://www.aaronrussell.co.uk/blog/improving-wordpress-the_excerpt/">Aaron Russell</a>, but this also had just a couple of stumbling blocks, as evidenced by reading through the comments section of Aaron&#8217;s post.</p>

<p>We here at Palehorse have come up with solutions to two of the weak points brought to light by Aaron&#8217;s readers, and have updated the function for WordPress 2.9 and above.</p>

<p>The two upgrades to the function deal with un-closed HTML tags, and adding a &#8220;read more&#8221; link (or some sort of anchor tag) to the &#8216;[...]&#8216; string at the end of the excerpt.</p>

<p>Here is Aaron&#8217;s original solution:</p>

<pre>
function improved_trim_excerpt($text) {
	global $post;
	if ( '' == $text ) {
		$text = get_the_content('');
		$text = apply_filters('the_content', $text);
		$text = str_replace(']]&gt;', ']]&gt;', $text);
		$text = preg_replace('@&lt;script[^&gt;]*?&gt;.*?&lt;/script&gt;@si', '', $text);
		$text = strip_tags($text, '&lt;p&gt;');
		$excerpt_length = 80;
		$words = explode(' ', $text, $excerpt_length + 1);
		if (count($words)&gt; $excerpt_length) {
			array_pop($words);
			array_push($words, '[...]');
			$text = implode(' ', $words);
		}
	}
	return $text;
}
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'improved_trim_excerpt');
</pre>

<p>Our upgrades include the new (in WordPress 2.9) <code>excerpt_more</code> filter hook, to which we have added a <i>permalink</i> in order to get a &#8220;read more&#8221; link. Of course we had come up with an elegant solution for this, only to discover that the designers were once again one step ahead of us. So the function has been updated with the new hook. I <del>hate</del> love it when that happens.</p>

<p>Next, in order to prevent any &#8220;snipped&#8221; HTML tags, we added this line:</p>

<pre>
$text = force_balance_tags( $text );
</pre>

<p>That will balance any open tags automatically.</p>

<p>Putting it all together is a bit involved, but is achieved by just &#8220;plugging in&#8221; a few lines of code. Instead of explaining the whole thing line-by-line, I will just post the final code <i>in toto</i>, with comments where we have added the fixes. If anyone has any questions, just leave a comment and I&#8217;ll explain further, if required.</p>

<p>The completed, augmented <code>improved_trim_excerpt()</code> function:</p>

<pre>
/**
 * Generates an improved excerpt from the content, if needed
 *
 * The excerpt word amount will be 80 words and if the amount is greater than
 * that, then the string '[...]' will be appended to the excerpt. If the string
 * is less than 80 words, then the content will be returned as is.
 *
 * The 80 word limit can be modified by plugins/themes using the excerpt_length filter
 * The '[...]' string can be modified by plugins/themes using the excerpt_more filter
 *
 * @link http://www.aaronrussell.co.uk/blog/improving-wordpress-the_excerpt/
 * @link http://palehorseinformation.com/2009/12/23/fixing-the-wordpress-excerpt/
 *
 * @param string $text The excerpt. If set to empty an excerpt is generated.
 * @return string The excerpt.
 */
function improved_trim_excerpt( $text ) {
	if ( '' == $text ) {
		$permalink = get_permalink();	// my addition
		$text = get_the_content( '' );
		$text = strip_shortcodes( $text );
		$text = apply_filters( 'the_content', $text );
		$text = preg_replace( '@&lt;script[^&gt;]*?&gt; . *?&lt;/script&gt;@si', '', $text );
		$text = str_replace( ']]&gt;', ']]&gt;', $text );
		$text = strip_tags( $text, '&lt;b&gt;&lt;em&gt;&lt;i&gt;&lt;p&gt;&lt;strong&gt;&lt;sub&gt;&lt;sup&gt;' ); // allowed tags (change as needed)
		$excerpt_length = apply_filters( 'excerpt_length', 80 );
		$excerpt_more = apply_filters('excerpt_more', ' ' . '&lt;em&gt;&lt;a href="' . $permalink . '"&gt;[...]&lt;/a&gt;&lt;/em&gt;');	// my addition
		$words = explode( ' ', $text, $excerpt_length + 1 );
		if ( count( $words ) &gt; $excerpt_length ) {
			array_pop( $words );
			$text = implode( ' ', $words );
			$text = $text . $excerpt_more;
			$text = force_balance_tags( $text );	// my addition
		}
	}
	return $text;
}
remove_filter( 'get_the_excerpt', 'wp_trim_excerpt' );
add_filter( 'get_the_excerpt', 'improved_trim_excerpt' );
</pre>

<p>If you would like, you can change the &#8216;[...]&#8216; string and/or the HTML <code>&lt;em&gt;</code> tags in this line to your liking:</p>

<pre>
$excerpt_more = apply_filters('excerpt_more', ' ' . '&lt;em&gt;&lt;a href="' . $permalink . '"&gt;[...]&lt;/a&gt;&lt;/em&gt;');
</pre>

<p>That&#8217;s all for now. Let us know what you think, if you have any problems with our revisions, or if you have any improvements of your own by leaving a comment. Thanks for reading, and thanks especially to Aaron for his original solution<span class="eoa-image">!</span></p>]]></content:encoded>
			<wfw:commentRss>http://palehorseinformation.com/2009/12/23/fixing-the-wordpress-excerpt/feed/</wfw:commentRss>
		<slash:comments>28</slash:comments>
		</item>
		<item>
		<title>W3C Drops XHTML 2</title>
		<link>http://palehorseinformation.com/2009/07/03/w3c-drops-xhtml-2/</link>
		<comments>http://palehorseinformation.com/2009/07/03/w3c-drops-xhtml-2/#comments</comments>
		<pubDate>Fri, 03 Jul 2009 23:39:47 +0000</pubDate>
		<dc:creator>Duncan MacLeod</dc:creator>
				<category><![CDATA[Web Design]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[HTML 5]]></category>
		<category><![CDATA[XHTML 2]]></category>

		<guid isPermaLink="false">http://palehorseinformation.com/?p=25</guid>
		<description><![CDATA[<p>News came yesterday that W3C plans not to renew the XHTML 2 Working Group&#8217;s charter when it expires at the end of 2009, in order to increase resources on HTML 5, as (quietly) reported in the W3C news archive:</p>

<p>&#8230;[W]hen the XHTML 2 Working Group charter expires as scheduled at the end of 2009, the charter will not be renewed. By doing so, and by increasing resources in the HTML Working Group, W3C hopes to accelerate the progress of HTML 5 <em>( cont&#8217;d&#8230; )</em></p>]]></description>
			<content:encoded><![CDATA[<p><span class="drop_cap">N</span><span class="first_word">ews</span> came yesterday that W3C plans not to renew the XHTML 2 Working Group&#8217;s charter when it expires at the end of 2009, in order to increase resources on HTML 5, as (quietly) reported in the <a href="http://www.w3.org/News/2009#item119">W3C news archive</a>:</p>

<blockquote><p>&#8230;[W]hen the XHTML 2 Working Group charter expires as scheduled at the end of 2009, the charter will not be renewed. By doing so, and by increasing resources in the HTML Working Group, W3C hopes to accelerate the progress of HTML 5 and clarify W3C&#8217;s position regarding the future of HTML.</p></blockquote>

<p>An FAQ can be found <a href="http://www.w3.org/2009/06/xhtml-faq.html">here</a> to help the public understand the future development of XHTML. My heart goes out to the developers; so much hard work only to come in second&#8230;</p>

<p>Time to bone up on <a href="http://www.w3.org/TR/html5/">HTML5</a>. Frankly, I for one am relieved that a decision was made one way or the other. Now I won&#8217;t need to learn them both &#8212; that seemed excessive to me. How do you feel about the subject?</p>]]></content:encoded>
			<wfw:commentRss>http://palehorseinformation.com/2009/07/03/w3c-drops-xhtml-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Easily Post Code Snippets In WordPress</title>
		<link>http://palehorseinformation.com/2009/06/24/easily-post-code-in-wordpress/</link>
		<comments>http://palehorseinformation.com/2009/06/24/easily-post-code-in-wordpress/#comments</comments>
		<pubDate>Wed, 24 Jun 2009 13:52:40 +0000</pubDate>
		<dc:creator>Duncan MacLeod</dc:creator>
				<category><![CDATA[WordPress]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[how-to]]></category>

		<guid isPermaLink="false">http://www.palehorseinformation.com/?p=3</guid>
		<description><![CDATA[Posting code snippets in WordPress can be a frustrating ordeal. Was the code you posted in your latest "how-to" article executed on page load, making your site look like it was created by orangutans? Not to worry! We have come up with a set of functions to automatically convert any applicable characters into HTML character entities of any content wrapped in <code>&#60;code&#62;</code> or <code>&#60;pre&#62;</code> tags. This article will show you our simple solution to the problem of posting code in WordPress.]]></description>
			<content:encoded><![CDATA[<p><span class="drop_cap">P</span><span class="first_word">alehorse</span> Information Services invites you to celebrate the Grand Opening of our new location on the web! The reconstruction of the Palehorse Information website saw the move from a hand-coded site to WordPress, in order to leverage the simplicity of the blogging platform. We would like to mark this occasion by offering a door prize to all those who stop by.</p>

<p>Testing our new WordPress theme design revealed what many others have, no doubt, found; the publishing platform just <i>hates</i> raw code in a post. The <a href="http://codex.wordpress.org/Writing_Code_in_Your_Posts">WordPress Codex</a> puts it thusly:</p>

<blockquote><p>&#8230;putting code in your post that &#8220;looks&#8221; likes code, but &#8220;doesn&#8217;t behave&#8221; like code, is a frequent challenge for bloggers.</p></blockquote>

<p>Well, we just couldn&#8217;t let the finicky filters have the last word, and we didn&#8217;t want to have to rely on an <a href="http://lorelle.wordpress.com/2007/02/23/wordpress-plugins-that-help-you-write-code/">externally written plug-in</a>. Besides, most of the plug-ins we saw, despite all of their author&#8217;s hard work, were either too bloated, too cumbersome to work with easily, or simply didn&#8217;t work.</p>

<p>After a little brainstorming, we came up with a simple solution&#8211;a set of functions based on php&#8217;s native <code>htmlentities()</code> function to convert any applicable characters into HTML character entities of any content wrapped in <code>&#60;code&#62;</code> or <code>&#60;pre&#62;</code> tags. We will get to the functions we came up with in a moment, but if you would like to use this technique, a couple of things need to be turned off in your setup before the functions can be added.</p>

<p>In a default WordPress installation, the pesky <acronym title="What You See Is What You Get">WYSIWYG</acronym> editor and &#8220;<a href="http://codex.wordpress.org/Function_Reference/wpautop">wpautop()</a>&#8221; filter are always inserting <code>&lt;p&gt;</code> tags whenever a double line-break is entered. If you are like us, those were the first things on the list that just had to go. So, if you haven&#8217;t disabled these &#8220;features&#8221; yet, turn off the WYSIWYG editor in the admin control panel, and then drop this next bit of code in your theme&#8217;s <code>functions.php</code> file to turn off the <code>wpautop()</code> filter:</p>

<pre>
/* Disables WordPress automatic paragraph formatting */
remove_filter('the_content',  'wpautop');
remove_filter('the_excerpt', 'wpautop');
remove_filter('comment_text', 'wpautop');
</pre>

<p>Remember, you&#8217;ll now need to wrap your paragraphs with <code>&lt;p&gt;&lt;/p&gt;</code> tags again. You should be used to this anyway if you&#8217;ve done any amount of HTML coding. It was actually more troublesome for us to omit them than to have to add them. (Yes, we are aware that we are a bunch of geeks.)</p>

<p>What we needed then was something to &#8220;scrub&#8221; the code of characters that could possibly cause markup to be executed when the page is called. Namely the <code>'&lt;'</code> and <code>'&gt;'</code> characters. We could use a converter like Elliot Swan&#8217;s app <a href="http://www.elliotswan.com/postable/">Postable</a>, but that requires a trip to the converter, copy, paste, convert, copy and paste. Call us lazy, but we&#8217;d rather just write the damn code in the post and have it convert automatically.</p>

<p>To acheive this automatic conversion, we whipped up this set of functions, which will convert any troublesome characters, anytime they are wrapped in <code>&#60;code&#62;</code> or <code>&#60;pre&#62;</code> tags. Again, just drop these &#8220;code scrubber&#8221; functions into your theme&#8217;s <code>functions.php</code> file:</p>

<pre>
/**
*  This converts any applicable characters into HTML entities in content marked
*  with &lt;code&gt; or &lt;pre&gt; in order to allow for posting pure code, with minimal fuss.
*  Styles should be set correctly in the CSS file (re: whitespace, overflow, etc.).
**/
function code_scrubber($content) {
	return preg_replace_callback('!&lt;code([^&gt;]*)&gt;(.*?)&lt;/code&gt;!ims', 'code_scrubber_callback', $content);
}

function code_scrubber_callback($matches) {
	$scrubbedContent = $matches[2];
	$scrubbedContent = htmlentities($scrubbedContent, ENT_NOQUOTES, 'UTF-8', false);
	$result = "&lt;code&gt;$scrubbedContent&lt;/code&gt;";
	return $result;
}

function pre_scrubber($content) {
	return preg_replace_callback('!&lt;pre([^&gt;]*)&gt;(.*?)&#60;/pre&gt;!ims', 'pre_scrubber_callback', $content);
}

function pre_scrubber_callback($matches) {
	$scrubbedContent = $matches[2];
	$scrubbedContent = htmlentities($scrubbedContent, ENT_NOQUOTES, 'UTF-8', false);
	$result = "&lt;pre&gt;$scrubbedContent&#60;/pre&gt;";
	return $result;
}
add_filter('the_content', 'code_scrubber', '1');
add_filter('comment_text', 'code_scrubber', '1');
add_filter('the_content', 'pre_scrubber', '1');
add_filter('comment_text', 'pre_scrubber', '1');
</pre>

<p>Now, make sure that your theme&#8217;s <code>style.css</code> file has entries for &#8216;pre&#8217; and &#8216;code&#8217;, and style them appropriately. The whitespace value for <code>&#60;pre&#62;</code> should be something like this, in order to retain the formatting:</p>

<pre>
pre {
	white-space: pre;
	}
</pre>

<p>You&#8217;ll probably want to fiddle with the rest of the CSS values to suit your tastes and conform to your theme, so we&#8217;ll consider the CSS formatting outside the scope of this article.</p>

<p>Now, anytime you wrap your code in either <code>&#60;code&#62;</code> tags or <code>&#60;pre&#62;</code> tags, all characters which have HTML character entity equivalents will be translated into these entities automatically.</p>

<p>Bear in mind, though, this is not a perfect solution, and there are a couple of caveats. First, don&#8217;t wrap a single <code>&#60;code&#62;</code> or <code>&#60;pre&#62;</code> tag without manually converting the wrapped tag&#8217;s angle brackets, or it may mess up your layout. You may also run into a snag if you need to wrap a closing <code>&#60;code&#62;</code> tag or <code>&#60;pre&#62;</code> tag (e.g. <code>&#60;/pre&#62;</code>), so you&#8217;ll need to manually convert any angle brackets that give you trouble. But for the most part, the code scrubber should do the trick in most circumstances.</p>

<p>Ironically, the only trouble we&#8217;ve had so far was posting the code for this function, due to the closing <code>&#60;/pre&#62;</code> tags near the end of the function. But a quick manual replacement of the opening brackets of the two closing tags, and all was right again. You don&#8217;t need to worry about double encoding of existing HTML entities (reconverting existing entities, e.g. <code>&amp;amp;amp;</code>), because that feature has been set to <span class="blue"><code>false</code></span>, as explained in the PHP Manual&#8217;s <a href="http://us.php.net/htmlentities">htmlentities() entry</a>.</p>

<p>Also, wrapping one set of tags inside another is not necessary (e.g. <code>&#60;pre&#62;</code><code>&#60;code&#62;</code>&#8230;<code>&#60;/code&#62;</code><code>&#60;/pre&#62;</code>), nor is it advised. The <code>&#60;code&#62;</code> tag should be used for small, in-line bits of code, whereas the <code>&#60;pre&#62;</code> tag should be used for larger, multi-line blocks of code.</p>

<p>Second, automatic &#8220;smiley&#8221; conversion may also cause you trouble. You may need a more comprehensive solution than this one if you need to have smilies in your posts and comments&#8211;that is unless you know how to un-convert smilies for just the code blocks. In that case, leave a comment below on how to achieve this, we would appreciate it very much. We avoided it here by simply turning off smiley conversion altogether. We didn&#8217;t want them anyway (sledge-hammer mechanics to the rescue).</p>

<p>Another less defined caveat is the optional <code>quote_style</code> parameter, set here to <code>ENT_NOQUOTES</code>, which leaves both double and single quotes unconverted. If you would like to also convert quotes, change <code>ENT_NOQUOTES</code> to <code>ENT_QUOTES</code>. This will convert both double and single quotes, or use the default, <code>ENT_COMPAT</code>, which will convert double-quotes and leave single-quotes alone. We haven&#8217;t seen any reason to turn it on here; the <code>wptexturize()</code> filter seems to leave code blocks alone, but you can turn it on if you find you need it in your situation.</p>

<p>Our &#8220;Code Scrubber&#8221; approach may not be perfect, but it&#8217;s transparent to the end user (you), and is fully automatic. It may also be a good starting point for more ambitious projects in the future. We hope you find this useful, and, if you have any improvements, leave a comment and share your expertise with the rest of us. Happy coding<span class="eoa-image">!</span></p>]]></content:encoded>
			<wfw:commentRss>http://palehorseinformation.com/2009/06/24/easily-post-code-in-wordpress/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
