<?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>SofaRider &#187; code</title>
	<atom:link href="http://rider.sofarider.com/tag/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://rider.sofarider.com</link>
	<description>WordPress Themes, Widgets, Development &#124; WEB &#38; Graphic Design</description>
	<lastBuildDate>Wed, 14 Oct 2009 08:40:00 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How to trim the_content()</title>
		<link>http://rider.sofarider.com/blog/wordpress-tips/how-to-trim-the_content/</link>
		<comments>http://rider.sofarider.com/blog/wordpress-tips/how-to-trim-the_content/#comments</comments>
		<pubDate>Sun, 27 Sep 2009 09:12:21 +0000</pubDate>
		<dc:creator>Feeleep</dc:creator>
				<category><![CDATA[Wordpress Tips]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[get_the_content()]]></category>
		<category><![CDATA[the_content()]]></category>
		<category><![CDATA[trim HTML]]></category>

		<guid isPermaLink="false">http://rider.sofarider.com/?p=307</guid>
		<description><![CDATA[My latest work was a customization of Rooster theme for one of my clients who didn&#8217;t like the idea of using &#60;!&#8211;more&#8211;&#62; separator. He wanted to have a split content at certain number of words and the idea of using the_excerpt() didn&#8217;t work hence the fact that this function strips out all the HTML code. [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://rider.sofarider.com/wp-content/uploads/2009/09/hoof_trim-200x200.jpg" alt="Trim content" title="Trim content" width="200" height="200" class="alignnone size-thumbnail wp-image-308" />My latest work was a customization of <a href="http://rider.sofarider.com/work/wordpress-themes/rooster-power-blog-wordpress-theme/">Rooster theme</a> for one of my clients who didn&#8217;t like the idea of using &lt;!&#8211;more&#8211;&gt; separator. He wanted to have a split content at certain number of words and the idea of using <strong>the_excerpt()</strong> didn&#8217;t work hence the fact that this function strips out all the HTML code. So I was faced with the problem and major issue was how to trim HTML formatted text?<span id="more-307"></span> Why? Because HTML formatted text may contain tags which MUST BE CLOSED and in the same time HTML tags are counted as characters!<br />
As you guess, the task may look like this: </p>
<ul>
<li>Temporary remove all eventual HTML tags</li>
<li>Trim stripped content at desired words length</li>
<li>Get all HTML tags back on their initial positions</li>
<li>Watch for unclosed tags </li>
</ul>
<p>OK, I guess the task is quite clear. Now we gonna create a function, paste it at the bottom of &#8216;functions.php&#8217; so it can be (re)used whenever is needed. I didn&#8217;t want to bother with making widget or plugin coz it takes more time and does the same job. Maybe one day I will. Here is our function:</p>
<pre>
<code>
&lt;?php
function trim_the_content( $the_contents, $read_more_tag = ' READ MORE...', $perma_link_to = '', $all_words = 45 ) {
	// make the list of allowed tags
	$allowed_tags = array( 'a', 'abbr', 'b', 'blockquote', 'b', 'cite', 'code', 'div', 'em', 'fon', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'hr', 'i', 'img', 'label', 'i', 'p', 'pre', 'span', 'strong', 'title', 'ul', 'ol', 'li', 'object', 'embed' );
	if( $the_contents != '' ) {
		// process allowed tags
		$allowed_tags = '&lt;' . implode( '&gt;&lt;', $allowed_tags ) . '&gt;';
		$the_contents = str_replace( ']]&gt;', ']]&amp;gt;', $the_contents );
		$the_contents = strip_tags( $the_contents, $allowed_tags );
		// exclude HTML from counting words
		if( $all_words &gt; count( preg_split( '/[\s]+/', strip_tags( $the_contents ), -1 ) ) ) return $the_contents;
		// count all
		$all_chunks = preg_split( '/([\s]+)/', $the_contents, -1, PREG_SPLIT_DELIM_CAPTURE );
		$the_contents = '';
		$count_words = 0;
		$enclosed_by_tag = false;
		foreach( $all_chunks as $chunk ) {
			// is tag opened?
			if( 0 &lt; preg_match( '/&lt;[^&gt;]*$/s', $chunk ) ) $enclosed_by_tag = true;
			elseif( 0 &lt; preg_match( '/&gt;[^&lt;]*$/s', $chunk ) ) $enclosed_by_tag = false;
			// get entire word
			if( !$enclosed_by_tag &amp;&amp; '' != trim( $chunk ) &amp;&amp; substr( $chunk, -1, 1 ) != '&gt;' ) $count_words ++;
			$the_contents .= $chunk;
			if( $count_words &gt;= $all_words &amp;&amp; !$enclosed_by_tag ) break;
		}
                // note the class named 'more-link'. style it on your own
		$the_contents = $the_contents . '&lt;a class=&quot;more-link&quot; href=&quot;' . $perma_link_to . '&quot;&gt;' . $read_more_tag . '&lt;/a&gt;';
		// native WordPress check for unclosed tags
		$the_contents = force_balance_tags( $the_contents );
	}
	return $the_contents;
}
?&gt;
</code>
</pre>
<h2>How to use this code with your own theme templates?</h2>
<p>That&#8217;s easy, the only thing you need to do is to call our function (pasted in functions.php, remember?) and pass required arguments or parameters. For example&#8230;instead of using either <strong>the_excerpt()</strong> or <strong>the_content()</strong> WP&#8217;s functions you would write something like this:</p>
<pre>
<code>
&lt;?php
	// find out your post permalink
	$perma_link = get_permalink( $post-&gt;ID );
	// save original post content to variable
	$content = get_the_content();
	// prevent tags strip | it's a bug in WordPress!
	$content = apply_filters( 'the_content', $content );
	$content = str_replace( ']]&gt;', ']]&amp;gt;', $content );
	// throw out trimmed: content to process, read more tag, post permalink, words length
	echo trim_the_content( $content, 'READ MORE...', $perma_link, 45 );
?&gt;
</code>
</pre>
<p>Hopefully you find this function useful so share it with others PLEASE! Thanks!</p>
]]></content:encoded>
			<wfw:commentRss>http://rider.sofarider.com/blog/wordpress-tips/how-to-trim-the_content/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>WordPress &#8211; simple site map code</title>
		<link>http://rider.sofarider.com/blog/wordpress-tips/wordpress-simple-site-map-code/</link>
		<comments>http://rider.sofarider.com/blog/wordpress-tips/wordpress-simple-site-map-code/#comments</comments>
		<pubDate>Sun, 13 Sep 2009 06:41:49 +0000</pubDate>
		<dc:creator>Feeleep</dc:creator>
				<category><![CDATA[Wordpress Tips]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[how-to]]></category>
		<category><![CDATA[site map]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://rider.sofarider.com/?p=284</guid>
		<description><![CDATA[Recently I&#8217;ve been proposed to add a sitemap template page (read functionality) to one of my WordPress themes. I started to Google for it but none of available plugins or code snippets didn&#8217;t do well. What my client actually wanted to have was something similar to a tree system where folders would represent categories/sub-categories and [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://rider.sofarider.com/wp-content/uploads/2009/09/tree-200x200.png" alt="tree" title="tree" width="200" height="200" class="alignnone size-thumbnail wp-image-283" />Recently I&#8217;ve been proposed to add a sitemap template page (read functionality) to one of my WordPress themes. I started to Google for it but none of available plugins or code snippets didn&#8217;t do well. What my client actually wanted to have was something similar to a tree system where folders would represent categories/sub-categories and files would &#8216;become&#8217; Post titles. So, here&#8217;s the result of a really simple site map generator for any WordPress theme.<br />
I do not claim it&#8217;s perfect and I&#8217;m aware it needs certain improvements but I guess it&#8217;s quite good for that purpose.</p>
<h2>The code</h2>
<p>Please note that I have created new WordPress template file and save it as &#8217;sitemap.php&#8217; in theme&#8217;s root folder. For more info regarding custom WP template files and how to use them, please refer to <a href="http://codex.wordpress.org/Pages#Creating_Your_Own_Page_Templates" target="_blank">this page</a>.<br />
Also, another important info is that this code will work only for Custom Permalink structure set to <strong>/%category%/%postname%/</strong>.</p>
<pre>
<code>
&lt;?php
    // easy job for pages...
    echo '&lt;ul&gt;';
    echo '&lt;li&gt;&lt;h1&gt;PAGES:&lt;/h1&gt;&lt;/li&gt;';
    echo '&lt;li&gt;&lt;ul&gt;';
    wp_list_pages( 'title_li=&amp;echo=1' );
    echo '&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt;';

    // categories sitemap will be tough...get'em all
    $subcats = wp_list_categories( 'echo=0&amp;use_desc_for_title=0&amp;title_li=' );
    // anybody there?
    preg_match_all( '|&lt;a.*?href=[\'&quot;](.*?)[\'&quot;].*?&gt;|i', $subcats, $m );
	if( !$m[ 1 ] ) {
		// no categs, subcategs or whatsoever...
		echo &quot;There are no categories added to your WordPress site.&quot;;
	} else {
		$arr_categories = array();
		$arr_orig_urls  = array();
		foreach( $m[ 1 ] as $key =&gt; $value ) {
			// $value will keep the URL, extract category slug
			$arr_of_url = explode( '/', $value );
			array_pop( $arr_of_url ); // remove empty
			// push the last
			array_push( $arr_categories, $arr_of_url[ count( $arr_of_url ) - 1 ] );
			// get its URL
			array_push( $arr_orig_urls, $value );
		}
	}

	if( count( $arr_categories ) &gt; 0 ) {
		// now we have entire logical structure of all categories
		// let's try to get ID for each of these
		echo '&lt;ul&gt;';
		echo '&lt;li&gt;&lt;h1&gt;CATEGORIES:&lt;/h1&gt;&lt;/li&gt;';
		$i = 0;
		while( $i &lt; count( $arr_categories ) ) {
			// shortcut by WP built-in
			$idObj = get_category_by_slug( $arr_categories[ $i ] );
			$catid = $idObj-&gt;term_id;

			// check whether this is root category. If true, make an outset in list, otherwise leave inset.
			if( $idObj-&gt;category_parent &gt; 0 ) echo '&lt;li&gt;&lt;ul&gt;';

			// get category real name and link
			$cat_real_name = $wpdb-&gt;get_var( &quot;SELECT name FROM $wpdb-&gt;terms WHERE term_id=&quot; . $catid );
			echo '&lt;li&gt;&lt;h2&gt;&lt;a href=&quot;' . $arr_orig_urls[ $i ] . '&quot;&gt;' . $cat_real_name . '&lt;/a&gt;&lt;/h2&gt;&lt;/li&gt;';

			// generate list of posts
			echo '&lt;li&gt;&lt;ul&gt;';
			query_posts( 'cat=' . $catid  . '&amp;order=ASC' ); // change to DESC - if required
			while( have_posts() ) : the_post();
?&gt;

&lt;li&gt;&lt;a href=&quot;&lt;?php the_permalink(); ?&gt;&quot; title=&quot;&lt;?php the_title(); ?&gt;&quot;&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt;&lt;/li&gt;
&lt;?php
			endwhile;
			echo '&lt;/ul&gt;&lt;/li&gt;';
			if( $idObj-&gt;category_parent &gt; 0 ) echo '&lt;/ul&gt;&lt;/li&gt;';
			$i ++;
		}
	echo '&lt;/ul&gt;';
	}
?&gt;
</code>
</pre>
<p>Hopefully you will find this code useful. Any comment/suggestion regarding improvement is welcome!</p>
]]></content:encoded>
			<wfw:commentRss>http://rider.sofarider.com/blog/wordpress-tips/wordpress-simple-site-map-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
