<?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>WallOfScribbles &#187; Code</title>
	<atom:link href="http://wallofscribbles.com/tag/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://wallofscribbles.com</link>
	<description>The ramblings of a man</description>
	<lastBuildDate>Sat, 07 Jan 2012 01:14:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>setInterval(): the sneaky basterd child of JavaScript</title>
		<link>http://wallofscribbles.com/2011/setinterval-the-sneaky-basterd-child-of-javascript/</link>
		<comments>http://wallofscribbles.com/2011/setinterval-the-sneaky-basterd-child-of-javascript/#comments</comments>
		<pubDate>Thu, 07 Apr 2011 14:30:22 +0000</pubDate>
		<dc:creator>Corey Dutson</dc:creator>
				<category><![CDATA[Bad bad bad]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[clearInterval]]></category>
		<category><![CDATA[clearTimeout]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[setInterval]]></category>
		<category><![CDATA[setTimeout]]></category>
		<category><![CDATA[text demo]]></category>

		<guid isPermaLink="false">http://wallofscribbles.com/?p=1199</guid>
		<description><![CDATA[So I&#8217;ve mentioned at some point or another that a lot of my work at RY has been developing jquery plugins and the like to make our lives easier during the busy reporting season. Overall they&#8217;ve worked out pretty well, but every once in a while someone finds a bug with one (or all) that [...]]]></description>
			<content:encoded><![CDATA[<p>So I&#8217;ve mentioned at some point or another that a lot of my work at RY has been developing jquery plugins and the like to make our lives easier during the busy reporting season. Overall they&#8217;ve worked out pretty well, but every once in a while someone finds a bug with one (or all) that needs addressing. Since they&#8217;re my creations, It&#8217;s usually put to me to correct these things.</p>
<p>Most of the time, these bugs are a small oversight on my part, or just straight-up stupidity. I&#8217;m not perfect, and I&#8217;ll gladly fix these things as they come up. I don&#8217;t consider bugs in my code that people find to be an affront to my skill; in reality I find them an opportunity to get better at what I do.</p>
<p>Then you run into something like a bug using setInterval, and things just stop making sense.</p>
<p><span id="more-1199"></span></p>
<h2>Edit</h2>
<p><em>Wow. I didn&#8217;t expect nearly as much traffic on this article as it ended up getting. I feel I should just put the learnings, work-arounds and the like up here, just to speed things up and to clarify.</em></p>
<p>I don&#8217;t like how setInterval works because if you lose the id reference, <strong><em>you&#8217;re hosed</em></strong>. That&#8217;s pretty much my gripe here. It&#8217;s just not a good implementation. Id&#8217;s that start at 1 (or 0!) and increment would at least allow a brute-force loop. A way to get back a collection of intervals in action would be swell. A proper ECMA addition to access the intervals in an object-esque manue would be awesome. Basically anything that didn&#8217;t require me to store every id somewhere for use later.</p>
<p>None of those options exist, and that&#8217;s the point of this article. The setInterval function returns something akin to a thread id, which you need to hold onto if you plan on killing the interval later (by passing it to clearInterval). It&#8217;s effectively a random integer, and the fact that there&#8217;s no way to access the intervals in any other way is silly to me. I will continue to think so until the logic can be explained in a way that makes sense.</p>
<p>The result of all of this? Don&#8217;t use setInterval if you can avoid it. If you can&#8217;t avoid it, pay close goddamned attention to when you use it so you don&#8217;t end up losing those all important references</p>
<p>Here are some ways to avoid losing the reference(s) / getting around setInterval:</p>
<h3>Option one (original idea at the end of this post):</h3>
<p>Use setTimeout with an external trigger of sorts. Gross, but it works well when you need to kill the function, and you may have left a closure / need to kill the function from somewhere else in your code.</p>
<pre class="brush: jscript; title: ;">
//
//
var counter = 1, myTimeout;

function loopFunction () {
	if (counter &lt; 5) {
		counter++;
		myTimeout = setTimeout(loopFunction, 5000);
	}
}

myTimeout = setTimeout(loopFunction, 50000);
//
//
</pre>
<h3>Option 2:</h3>
<p>Push the ids into an array, and use the array to kill them all later. Cleaner, but slightly more complex. Solves a slightly different issue to the last one.</p>
<pre class="brush: jscript; title: ;">
//
//
var intervalArr = new Array();

function intervalOne () { alert ('foo');};
function intervalTwo () { alert ('bar');};

function killIntervals(){
	while(intervalArr.length &gt; 0)
		clearInterval(intervalArr.pop());
};

// adds the intervals
intervalArr.push(setInterval(intervalOne, 2000));
intervalArr.push(setInterval(intervalTwo, 2000));

// removes them
killIntervals();
//
//
</pre>
<h2>TLDR;</h2>
<p><a title="wallofscribbles.com - tech demos - setInterval bug" href="/techDemos/setIntervalBug.php">Click here to view the demo</a>. Look at the source code, you&#8217;ll see what I&#8217;m demonstrating with this post. Be wiser for the fact and you&#8217;re done.</p>
<h2>Some basics</h2>
<p>So some basics on JavaScript for those that don&#8217;t know. review the following code:</p>
<pre class="brush: jscript; title: ;">
//
//
var myVariable; // declare a blank variable

myVariable = 'Hello'; // sets the variable to 'Hello'
alert(myVariable); // will bring up an alert box that says 'Hello'

myVariable = 'Hi there'; // sets the variable to 'Hi there', overriding the old value
alert(myVariable); // will bring up an alert box that says 'Hi there'
//
//
</pre>
<p>That&#8217;s all basic JavaScript variable stuff. The reason I put this here is so you understand the concept that you can overwrite the value of a variable that was previously set. This is pretty much how variables work. It gets a little grey when assigning variable references but that&#8217;s not the point of this post.</p>
<p>Now there are two automating functions that people generally use: setInterval and setTimeout. I&#8217;m aware of web-workers and such, but I&#8217;m not getting into those here.</p>
<p>They generally work like this:</p>
<pre class="brush: jscript; title: ;">
//
//
var myTimeout, myInterval; // blank variables

myTimeout = setTimeout(function(){ alert('Hello'); }, 5000); // After 5 seconds, alert 'Hello'
myInterval = setInterval(function(){ alert('Hi there'); }, 10000); // Every 10 seconds, alert 'Hi there'
//
//
</pre>
<p>Now in this example, I set up a function instance right in the timeout and interval assignments. You can also choose to to reference a function (not to be confused with a function call, which they dislike but you can work around), but for the sake of keeping my examples small I am using a function instance.</p>
<p>Basically setTimeout will call the function once when the timer (set in milliseconds) ticks, whereas setInterval will call the function every time the timer ticks. The reason You store them in variables is so you can use the clearTimeout and clearInterval functions. These are supposed to remove the timeout or interval from existence, stopping the functionality from happening (or happening again, whichever).</p>
<pre class="brush: jscript; title: ;">
//
//
var myTimeout, myInterval; // blank variables

myTimeout = setTimeout(function(){ alert('Hello'); }, 5000); // After 5 seconds, alert 'Hello'
myInterval = setInterval(function(){ alert('Hi there'); }, 10000); // Every 10 seconds, alert 'Hi there'

clearTimeout(myTimeout); // kills the timeout, so it will never fire off
clearInterval (myInterval); //kills the interval, so it will never fire off.
//
//
</pre>
<p>Given this information, you may think that you can now go and do loads of timed stuff care-free! Well no, you can&#8217;t. You see I ran into an&#8230; oddity with setInterval and setTimeout. I couldn&#8217;t find any documentation showing this quirk, hence this blog post.</p>
<h2>How I found the issue</h2>
<p>So today, a colleague found a bug with a plugin I wrote. It&#8217;s a plugin that fades between slides. Pretty straight-forward. It has options to enable autoPlay, render paging buttons, etc. The autoplay functionality &#8211; that is, rotating between the slides &#8211; is being handled by setInterval.</p>
<p>Basically when it starts, I set an interval to rotate. If you skip slides (using a navigation) it stops the slider by clearing the interval. In theory, that is. Apparently somewhere down the line it stopped working, and I had to go hacking through the code to figure out what the hell was going on. As it works out I&#8217;ve found a rather odd issue with setInterval and setTimeout.</p>
<h2>The issue manifested</h2>
<p>Code:</p>
<pre class="brush: jscript; title: ;">
//
//
var myInterval;

function alertOne() {
	alert('One');
}

function alertTwo() {
	alert('Two');
}

myInterval = setInterval(alertOne, 2000);
myInterval = setInterval(alertTwo, 3000);
//
//
</pre>
<p>&nbsp;</p>
<pre class="brush: xml; title: ;">
&lt;!-- snip --&gt;
&lt;a href=&quot;javascript:clearInterval(myInterval);&quot;&gt;Kill stuff&lt;/a&gt;
&lt;!-- snip --&gt;
</pre>
<p>Now given the idea of how variables should work, if you were to run this code, you would expect the first interval call to be overwritten by the second. That&#8217;s what I expected anyway. As it turns out, his is not the case. You see apparently it actually stacks the calls into the variable. Sort of like an array, but without any of the actual array properties or functionality.</p>
<p>Given that it stacks them, you would think that the clearInterval on the anchor would then remove both intervals. I mean they&#8217;re both attached to the variable, so it stands to reason that clearing the variable with clearInterval would sort it out right? Nope! It actually only clears the last interval set to it.</p>
<p>Don&#8217;t believe me? run it yourself, or <a title="wallofscribbles.com - tech demos - setInterval bug" href="/techDemos/setIntervalBug.php">click here</a> for a running example. So I started trying to find ways of totally clearing it.</p>
<p>How about setting the variable to null? That won&#8217;t work because it treats the variable as a function reference, and not an instance of the interval system.</p>
<p>What about stacking the clearIntervals? That doesn&#8217;t work either. It only ever clears the last one that was assigned. The rest seem to become buried, and I haven&#8217;t found a way to surface them.</p>
<p>Oh and everything I just said about setInterval here? It also applies to setTimeout. I&#8217;m using setInterval though, because at least a timeout ends after the first call. That is, unless you made a self-replicating timeout function. You may as well use setInterval at that point unless you actually need to change the amount of time between calls, or you have external flags (see further down for an explanation of this).</p>
<p>So basically if you assign more than one interval or timeout to a variable, you&#8217;re screwed. You can&#8217;t clear them all with clearInterval, you can&#8217;t null the variable as it&#8217;s just a reference, and you can&#8217;t stack clearIntervals, because it doesn&#8217;t surface the previous assignments, even though they continue to run.</p>
<h2>Solution?</h2>
<p>Sadly there&#8217;s no sweet fix for this that I&#8217;ve found. Basically if you&#8217;re going to be using setInterval, make damned sure you use clearInterval before setting it again. If you don&#8217;t, you bury the old intervals and there&#8217;s no way to stop them from continuing..</p>
<p>A gross but possible solution would be to use a self-replicating setTimeout function that had an external flag that it could use to halt it&#8217;s loop.</p>
<p>Here&#8217;s a basic example:</p>
<pre class="brush: jscript; title: ;">
//
//
var counter = 1;
var myTimeout;

function loopFunction () {
	if (counter &lt; 5) {
		counter++;
		myTimeout = setTimeout(loopFunction, 5000);
	}
}

myTimeout = setTimeout(loopFunction, 50000);
//
//
</pre>
<p>If you do that, at least there&#8217;s a way to stop buried Timeouts. You don&#8217;t get such a luxury with setInterval.</p>
<p>This took me far too much time to figure out today, and either I missed a rather important class in JavaScript school* or not everyone knows about this. I went looking on the Googles, but didn&#8217;t come up with anything outlining the dangers of setInterval. Hopefully this will help someone out there. At the very least it&#8217;ll be a reminder to me to pay more attention when I use it.</p>
<p>* I don&#8217;t think there is an actual JavaScript school, even though that would be very, very useful.</p>
]]></content:encoded>
			<wfw:commentRss>http://wallofscribbles.com/2011/setinterval-the-sneaky-basterd-child-of-javascript/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>My jQuery plugin template is open season!</title>
		<link>http://wallofscribbles.com/2011/my-jquery-plugin-is-open-season/</link>
		<comments>http://wallofscribbles.com/2011/my-jquery-plugin-is-open-season/#comments</comments>
		<pubDate>Thu, 03 Mar 2011 15:00:55 +0000</pubDate>
		<dc:creator>Corey Dutson</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[JQuery]]></category>
		<category><![CDATA[plugins]]></category>
		<category><![CDATA[work]]></category>

		<guid isPermaLink="false">http://wallofscribbles.com/?p=1052</guid>
		<description><![CDATA[So I&#8217;ve been doing a lot of javascript development at work recently. I&#8217;ve basically created a lot of our reusable javascript &#8216;plugins&#8217; to cover the common requirements of the work that we do. In many cases, this resides mostly (but not entirely) in online annual-report creation. Anyone that&#8217;s in the business will know that there [...]]]></description>
			<content:encoded><![CDATA[<p>So I&#8217;ve been doing a lot of javascript development at work recently. I&#8217;ve basically created a lot of our reusable javascript &#8216;plugins&#8217; to cover the common requirements of the work that we do. In many cases, this resides mostly (but not entirely) in online annual-report creation.</p>
<p>Anyone that&#8217;s in the business will know that there are some common functional elements that you end up having to build with javascript: accordions, tabs, faders, sliders, lightboxes, filters, etc. Many of these items can bleed over into non-report sites, such as intranets or even public-facing sites.</p>
<p><span id="more-1052"></span></p>
<p>Now some of those are fairly straight forward: an accordion is just a simple animate of a content section, repeat per title; tabs? Same deal. So you might be pretty inclined to just write the script adhoc on a project to project basis, copy and pasting it as you go. We were doing that at my work when I started on, but I found that we kept on having to add something, remove something, add hooks at certain points etc. In some cases we used someone else&#8217;s plugin to do these things, but we ran into some issues when it came to having to tweak the plugin for our needs.</p>
<p>As I was hired on as the senior front-ender, I was given the task of trying to help simplify things in our working process. This required me to dive right into plugin development, because one spot we were spending a lot of time was reinventing the wheel project to project. Even more time was spent debugging random plugins, or lashing them together.</p>
<p>As my place of employment works with jQuery, I wanted to develop something that could be maintained in-house, and would be a good starting point for repeating functionality. I ended up developing a jQuery plugin template, which I am sharing today.</p>
<p>Now, while I can&#8217;t feature the plugins I&#8217;ve made for my work here on my site (I&#8217;m pretty sure my they&#8217;d be less than impressed with that), I feel it&#8217;s fairly safe to distribute this template (boiler plate?) to the internets. It&#8217;s a compilation of a couple tutorials, as well as my own exploration into javascript. I&#8217;d be more than happy to give credit where it&#8217;s due, but I cannot remember where the heck I got some of the snippets I integrated. If anyone reading this happens to know, leave a comment and I&#8217;ll be more than happy to give said credit.</p>
<p>Below you will find my jQuery plugin template code in it&#8217;s entirety. I&#8217;m open to suggestions on how to improve it, so feel free to comment.</p>
<pre class="brush: jscript; title: ;">
/*
*
    Name:      my.blank.plugin.js
    Purpose:   Shell to use for jQuery plugins
*/
(function ($) {
    // DONT FORGET TO NAME YOUR PLUGIN
    jQuery.fn.myPlugin = function (options, i) {
        // This handles multiple elements (like a class selector)
        if (this.length &gt; 1) {
            var a = new Array();
            this.each(
                function (i) {
                    a.push($(this).myPlugin(options, i));
                });
            return a;
        }
        var opts = $.extend({}, $().myPlugin.defaults, options);

        /* PUBLIC FUNCTIONS */

        /* reInit is a flag that you can pass in case you don't
           want to remove everything during the destroy phase. */
        this.Destroy = function (reInit) {
            var container = this;
            var reInit = (reInit != undefined) ? reInit : false;
            $(container).removeData(&quot;myPlugin&quot;);
            // this removes the flag so we can re-initialize
        };

        this.Update = function (options) {
            opts = null;
            opts = $.extend({}, $().myPlugin.defaults, options);
            this.Destroy(true);
            return this.Create();
        };

        /* iteration will give you the index of the item
           in the selection. If not part of a loop, I'm
           prett sure this will be null. You've been warned. */
        this.Create = function (iteration) {

            // this stops double initialization
            if ($(container).data(&quot;myPlugin&quot;) == true)
                return this;

            // call a function before you do anything
            if (opts.beforeCreateFunction != null &amp;&amp; $.isFunction(opts.beforeCreateFunction))
                opts.beforeCreateFunction(targetSection, opts);

             // reference to the object you're manipulating. To jQuery it, use $(container).
            var container = this;
            /* Failing that, you could just use 'this' without the var declaration,
               but if you are doing a lot of child looping, you'll be glad to have
               a reference to the target object. Also, performance improvements! */

            ////////////////////
            // DO STUFF HERE
            ////////////////////

            // Set a flag to show that this element has been plugin'd
            $(container).data(&quot;myPlugin&quot;, true);

            // call a function after you've initialized your plugin
            if (opts.afterCreateFunction != null &amp;&amp; $.isFunction(opts.afterCreateFunction))
                opts.afterCreateFunction(targetSection, opts);

            /* Make sure to return, otherwise you can't store the element in a varaible,
               which means you can't use any of the plubic functions (you may not need
               them, but it's still good practice.) */
            return this;
        };

        /* Example of a public function, this can be used if you store
           this object in a variable.

           e.g. var foo = $(&quot;#target&quot;).myPlugin();
                foo.PublicFunction();
        */

        this.PublicFunction = function () {
            // do something
            myPrivatefunction();
        };

        /* PRIVATE FUNCTIONS */

        /* These aren't accessibly externally, and so can only
           be called from within the enclosure code. */

        function myPrivatefunction() { };

        /* arguably, you could wrap your private (or public)
        functions in an array such as the following: */

        // Private helper functions
        helper = {
            firstHelper: function () { /* do something */ },
            secondHelper: function (somevalue) { /* do something with the variable passed in */ }
        };

        //Public helper functions
        this.Helper = {
            FirstHelper: function () { /* do something */ },
            SecondHelper: function (somevalue) { /* do something with the variable passed in */ }
        };

        /* Personally, I don't see the point of doing this for
           anything OTHER than helper functions, I could, and
           probably am, wrong here... or at least ignorant. */

        // Finally
        return this.Create(i);
    };

    // DONT FORGET TO NAME YOUR DEFAULTS WITH THE SAME NAME
    jQuery.fn.myPlugin.defaults = {
        foo: &quot;bar&quot;,
        something: &quot;else&quot;,

        // Remember: these are function _references_ not function calls
        beforeCreateFunction: null,
        afterCreateFunction: null
    };
})(jQuery);
</pre>
<p>I&#8217;ve tried to comment the hell out of the code, so that it&#8217;s as straight-forward as possible. I assume that anyone that uses this will strip out the comments, as well as any of the functionality that isn&#8217;t required.</p>
<p>Please feel free to use this, but If you&#8217;d be so kind as to give me credit, that&#8217;d be swell. I can&#8217;t force you, but we&#8217;re all friends out here, aren&#8217;t we?</p>
]]></content:encoded>
			<wfw:commentRss>http://wallofscribbles.com/2011/my-jquery-plugin-is-open-season/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WordPress plugins, I has them</title>
		<link>http://wallofscribbles.com/2009/wordpress-plugins-i-has-them/</link>
		<comments>http://wallofscribbles.com/2009/wordpress-plugins-i-has-them/#comments</comments>
		<pubDate>Mon, 12 Jan 2009 18:14:53 +0000</pubDate>
		<dc:creator>Corey Dutson</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[plugins]]></category>
		<category><![CDATA[PostDivider]]></category>
		<category><![CDATA[RandomQuotr]]></category>

		<guid isPermaLink="false">http://www.wallofscribbles.com/?p=382</guid>
		<description><![CDATA[The lolcat title reference aside, I have actually started producing WordPress plugins for myself. I&#8217;ve always wanted to make them, and so a while back I set aside a day (well two, once I got the hang of it) and learned. Granted I&#8217;ve still got a ways to go, but at least I&#8217;ve finally got [...]]]></description>
			<content:encoded><![CDATA[<p>The <a title="Lolcats!" href="http://icanhascheezburger.com/">lolcat</a> title reference aside, I have actually started producing WordPress plugins for myself. I&#8217;ve always wanted to make them, and so a while back I set aside a day (well two, once I got the hang of it) and learned. Granted I&#8217;ve still got a ways to go, but at least I&#8217;ve finally got my foot in the door with it all. The trick is to make sure to create things that are actually useful so that people will actually use them.</p>
<p>So as a result of my tooling around with WordPress, I&#8217;ve created two plugins for people to use.</p>
<h2><span id="more-382"></span></h2>
<h2>RandomQuotr</h2>
<p>In one of my older site lay­outs, I had a random quote dis­play­ing at the top of the page. The orig­i­nal logic was based very much on the Hello Dolly plugin that comes shipped with Word­Press. Even though I no longer use the func­tion within my layout, I still had the code and I felt that with a little revi­sion, this could be a psudo-​useful plugin.</p>
<p>So one morn­ing I did exactly that. <a title="Corey Dutson: RandomQuotr" href="/wordpress-plugins/randomquotr/">Ran­domQuotr</a> was born, and with it came the abil­ity to store all of the quotes within the WordPress data­base, as opposed to the PHP array that it started out as. Some more func­tion­al­ity was added to allow the selec­tion of spe­cific quotes.</p>
<h2>PostDivider</h2>
<p>I was a big advo­cate for using “the_excerpt()” for my sites layout, because it allowed me to split up my con­tent and work around my theme. the prob­lem is that using “the_excerpt()” sucks for many reasons:</p>
<ol>
<li>The field is HTML only.</li>
<li>The field is tiny and cannot be re-​sized.</li>
<li>It requires you to break up your most in a less than stel­lar way.</li>
</ol>
<p>Enter <a title="Corey Dutson: PostDivider" href="/wordpress-plugins/postdivider/">Post­Di­vider</a>.</p>
<p>Based on an <a title="WallOfScribbles.com: More with the More Tag" href="/2008/10/02/getting-more-with-the-more-tag/">old post of mine</a>, Post­Di­vider allows the user to get the con­tent before and after the &lt;!–MORE–&gt; tag sep­a­rately. With Post­Di­vider, hooks are pro­vided to allow the Theme to dis­play the text directly (echo­ing it), or return­ing the con­tent as a value from the func­tion. If no &lt;!–MORE–&gt; tag is pro­vided, it will return the_excerpt() and the_content() respectively.</p>
<p>So far those are the only ones I&#8217;ve developed. I&#8217;ve got a couple ideas for more, when I get around to building my portfolio and the like.</p>
<p>I&#8217;ve also <a title="Corey Dutson: WordPress Plugins" href="/wordpress-plugins/">created a page</a> that lists all of the plugins I have developed. They&#8217;re also hosted over on the <a title="Wordpress.org: Loveless" href="http://wordpress.org/extend/plugins/profile/loveless">wordpress.org plugins directory</a> if you want to keep watch there.</p>
<p>I&#8217;m always looking for feedback or suggestions, so feel free to email me or leave a comment.</p>
]]></content:encoded>
			<wfw:commentRss>http://wallofscribbles.com/2009/wordpress-plugins-i-has-them/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Colour Swapper in WordPress</title>
		<link>http://wallofscribbles.com/2008/colour-swapper-in-wordpress/</link>
		<comments>http://wallofscribbles.com/2008/colour-swapper-in-wordpress/#comments</comments>
		<pubDate>Thu, 09 Oct 2008 13:05:13 +0000</pubDate>
		<dc:creator>Corey Dutson</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[colour swapper]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[JQuery]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.wallofscribbles.com/?p=267</guid>
		<description><![CDATA[Who likes website colour swappers? Anyone? When I developed the theme I am currently using, I searched high and low for something I could steal take inspiration from. The funny thing is that most people don&#8217;t even know where the colour swapper is on my site. So few in fact that I had to add [...]]]></description>
			<content:encoded><![CDATA[<p>Who likes website colour swappers? Anyone?</p>
<p>When I developed the theme I am currently using, I searched high and low for something I could <span style="text-decoration: line-through;">steal</span> take inspiration from. The funny thing is that most people don&#8217;t even know where the colour swapper is on my site. So few in fact that I had to add a caption to it to help make it a little more obvious.</p>
<p>In any case I felt that I would share the method I used to create the swapping functionality on my website. I&#8217;m aware that most of it can be figured out with a little imagination and some source viewing, but I think that going through it step by step is more helpful.</p>
<p><strong>UPDATE &#8211; 10/09/2008 -</strong> In record time, a second pair of eyes looking at this has managed to refine the process. Thank you <a title="Bartek Gniado" href="http://bart.whahay.net">Bart</a> for pointing out that if the CSS files are named the same as the Title attribute of the Anchor, then you can drop the whole If statement. I&#8217;ve updated the tutorial accordingly</p>
<p><span id="more-267"></span></p>
<h2>Table of contents</h2>
<ul>
<li><a href="#article_idea">The idea</a></li>
<li><a href="#article_before">Before we begin</a></li>
<li><a href="#article_setup">Setting up our files</a></li>
<li><a href="#article_css">Setting up the CSS files</a></li>
<li><a href="#article_php">A dash of PHP</a></li>
<li><a href="#article_markup">The basic markup</a></li>
<li><a href="#article_java">The JavaScript</a>
<ul>
<li><a href="#article_java_import">Importing the required files</a></li>
<li><a href="#article_java_func">Adding functionality</a></li>
</ul>
</li>
<li><a href="#article_together">Pulling it all together</a>
<ul>
<li><a href="#article_together_flow">Flow</a></li>
</ul>
</li>
<li><a href="#article_conclusion">Conclusion</a></li>
</ul>
<h2 id="article_idea">The idea</h2>
<p>So you want to build a colour swapper? Easy as pie. You want to do it without causing a postback and sustains itself throughout the navigation? That&#8217;s a little more tricky, and what I wanted to do myself.</p>
<p>What, you&#8217;re too lazy to figure this out for yourself? No problem. Read on and be merry.</p>
<h2 id="article_before">Before we begin</h2>
<p>A few words before we begin:</p>
<ul>
<li>I am not a master of JavaScript, let alone <a title="JQuery" href="http://jquery.com/">JQuery</a>. I&#8217;m not a novice, but I am in no way a master of the language. There are probably many ways in which to improve my methods here. I&#8217;m going to take another crack at them and see what I can come up with. I&#8217;ll update the code to this post If I come up with anything.</li>
<li>In addition to the last note, I&#8217;m not going to explain how JQuery works. they do a <a title="JQuery: How it works" href="http://docs.jquery.com/How_jQuery_Works">fine job on their own</a>. If you don&#8217;t understand it, I suggest you take some time and become familiar with it.</li>
<li>This whole solution rides on the idea that your users allow JavaScript and Cookies. If the Cookies are disabled, the styles will still change, but will not persist between page refreshes/navigation. If JavaScript is disabled, nothing will work because I have not yet implemented a postback-friendly alternate. If I find myself with time I&#8217;ll do this as well and update this post.</li>
<li>This whole tutorial is based on the idea that you are applying this to a WordPress theme. If you&#8217;re using something else, you&#8217;ll need to alter this as you need.</li>
<li>I have noticed that in some browsers, the swapping is not instant, and you may notice somewhat of a flicker. In my case the colours go away, and then the new ones are applied. Some browsers do not do this. I have no idea why this happens.</li>
</ul>
<h2 id="article_setup">Setting up our files</h2>
<p>For this solution we&#8217;re going to need to <a title="JQuery: Featured Downloads" href="http://code.google.com/p/jqueryjs/downloads/list?q=label:Featured">get some files</a> from the JQuery website (the latest JQuery build), and create some files in our Theme folder.</p>
<p>You are going to need to make a JavaScript folder &#8211; I called mine &#8220;js&#8221; &#8211; and multiple CSS files. One will be a core style sheet that contains all of the universal CSS styles, and then alternate sheets that contain the CSS that is swapped around.</p>
<h3 id="article_setup_example">Example setup:</h3>
<ul>
<li>THEME FOLDER
<ul>
<li>style.css</li>
<li>Yellow Version.css</li>
<li>Red Version.css</li>
<li>Blue Version.css</li>
<li>Green Version.css</li>
<li>JS FOLDER
<ul>
<li>jquery.js</li>
<li>functions.js</li>
</ul>
</li>
</ul>
</li>
</ul>
<p>The rest of this example will assume that you have that setup.</p>
<p></p>
<h2 id="article_css">Setting up the CSS files</h2>
<p>First off, you&#8217;re going to need some CSS files to play around with. Generally you would have a core css file that would contain all the styles that wouldn&#8217;t be affected by your swapper. Depending on how you use your swapper, this may or may not be the case. Some people want to totally alter the website with their swapper; I just wanted a colour change.</p>
<p><em><strong>Note:</strong> I&#8217;m not going to paste all the CSS I used to format the actual tiles, because my layout requirements will be different than yours. Set the CSS code up however you need to make your selector HTML &#8211; we&#8217;ll see that in a bit &#8211; look right.</em></p>
<p>So you have your core css file (style.css for WordPress users). Do what you need to in that file and then put it away. We won&#8217;t be touching it very much. The only addition I put in my CSS files are the following:</p>
<pre>.red {background-color:#FF3333;}
.yellow {background-color:#FBDC00;}
.green {background-color:#1FF900;}
.blue {background-color:#00CCFF;}</pre>
<p>These four styles are in my core CSS file so that my swappers always have access to correct classes for them. What this means is that regardless of what CSS file is imported, the swapper tiles will always look the same.</p>
<p>Next up we have our swapping CSS files: Blue Version, Green Version, Red Version and Yellow Version.</p>
<p><em><strong>Note:</strong> I use Yellow Version as my default, but that doesn&#8217;t come in for a little bit yet.</em></p>
<p>So in each one of these CSS files, I have the same selectors:</p>
<pre>.specialBGColor {background-color:#FBDC00;}
a, .specialColourText, #left_bar .widget table th {color:#FBDC00;}
.specialTopBorder{border-top:20px solid #FBDC00;}</pre>
<p>All of your swappable CSS files will have the same selectors with whatever colour differences you need. In my case The hex colours are going to change to whatever is appropriate for the file (yellow is #FBDC00, red is #FF3333, etc.) &#8220;specialBGColor&#8221; is basically a class I apply to anything that is going to have a swappable background. I also have a &#8220;speciallTopBorder&#8221; that does something similar, and some selectors for links and table headers to change their colours.</p>
<p>All of these swappable CSS files should be in the same directory. Since I am using WordPress my CSS files are all kept together in the same directory: the theme directory. I dislike the lack of separation but I&#8217;ll manage.</p>
<p>Once you&#8217;ve got all those set up we can move on to setting up the PHP functions</p>
<h2 id="article_php">A dash of PHP</h2>
<p>We need to add a little function to our functions.php file. The function will attempt to grab a value from the users cookie cache and return it. Pretty simple overall. In my case, if the cookie is not found, it returns a default string that is the URL to my yellow style sheet. As previously stated, the Yellow CSS file is the same.</p>
<p>Add the following to your functions.php file:</p>
<pre>function getStyleCookie()
{
   if($_COOKIE["styleHref"] != null &amp;&amp; $_COOKIE["styleHref"] != "")
   {
      echo $_COOKIE["styleHref"];
   }
   else
   {
      $url = bloginfo('template_url') + "/Yellow Version.css";
      echo $url;
   }
}</pre>
<p>What this does is if it can get the cookies value &#8211; in this case &#8220;styleHref&#8221; cookie &#8211; this is explained in the JS &#8211; it will echo the value of the cookie. if it can&#8217;t it returns the default value of the Yellow Version.css location.</p>
<p><em><strong>Note:</strong> I could have made the function a bit more universal by passing in the cookie name, but as I&#8217;m only using it for this one instance, I didn&#8217;t bother.</em></p>
<h2 id="article_markup">The basic markup</h2>
<p>Alright lets set up the markup. Lets start with the header.</p>
<p>Locate the &lt;head&gt;&lt;/head&gt; section of your website (for WordPress users, this is usually the header.php file).</p>
<p>You&#8217;ll need to find where you have any current CSS files linked and paste the following over or after the CSS tags. I say over or after because depending how much of this you&#8217;re following, you may already have a core CSS file that you don&#8217;t want to remove.</p>
<pre>&lt;!-- THIS IS YOUR CORE CSS FILE. IGNORE THIS LINE IF YOU ALREADY HAVE THIS IN YOUR HEADER --&gt;
&lt;link rel="stylesheet" href="&lt;?php bloginfo('stylesheet_url'); ?&gt;" type="text/css"/&gt;

&lt;!-- some dynamic stuff that won't make any sense yet, but it will later --&gt;
&lt;?php if($_COOKIE["styleHref"] != null &amp;&amp; $_COOKIE["styleHref"] != "") : ?&gt;
   &lt;link id="altsheet" rel="stylesheet" href="&lt;?php echo $_COOKIE["styleHref"]; ?&gt;" type="text/css"/&gt;
&lt;?php else : ?&gt;
   &lt;link id="altsheet" rel="stylesheet" href="&lt;?php bloginfo('template_url'); ?&gt;/Yellow Version.css" type="text/css"/&gt;
&lt;?php endif; ?&gt;</pre>
<p><strong>What this does:</strong> try and get the URL to the currently used CSS file and render that CSS file. If nothing can be found, default to the Yellow Version style sheet. This may seem repetitive given that the functions.php code we put in also compensates for a lack of cookie. I&#8217;m one for having backup plans, and this was mine. If you don&#8217;t like it, you can simply remove the entire else section.</p>
<p></p>
<p>Next up is the actual swapper markup. I&#8217;m going to paste a simplified version of mine, because as I stated before, your swapper will look different than mine. As long as the base HMTL is the same, this solution works. how you style things up is totally up to you.</p>
<pre>&lt;div id="colour_swapper"&gt;
   &lt;ul&gt;
      &lt;li&gt;
         &lt;a href="javascript:;" class="green" title="Green Version"&gt;Swap to green&lt;/a&gt;
      &lt;/li&gt;
      &lt;li&gt;
         &lt;a href="javascript:;" class="yellow" title="Yellow Version"&gt;Swap to yellow&lt;/a&gt;
      &lt;/li&gt;
      &lt;li&gt;
         &lt;a href="javascript:;" class="blue" title="Blue Version"&gt;Swap to blue&lt;/a&gt;
      &lt;/li&gt;
      &lt;li&gt;
         &lt;a href="javascript:;" class="red" title="Red Version"&gt;Swap to red&lt;/a&gt;
      &lt;/li&gt;
   &lt;/ul&gt;
&lt;/div&gt;</pre>
<p>The important parts to this are two-fold.</p>
<p>First, the <strong>ID of the DIV</strong> element is going to be used in the JavaScript. I&#8217;m aware that the DIV isn&#8217;t really needed, but as I said, this is a simplified version of what I&#8217;m using, and as such I&#8217;ve actually stripped out additional elements contained in the DIV.</p>
<p>The second thing is the <strong>title of the ANCHOR items</strong>. The title will be used in the JavaScript to determine what CSS file to get.</p>
<p><em><strong>Note:</strong> I&#8217;m aware that having a href of &#8216;javascript:;&#8217; is a bad practice. In reality the href should offer a link that would manually post back the page, and have PHP  code that would look for that action and set the cookie that way. Doing so would allow for the swapper to work when JavaScript is disabled. </em></p>
<h2 id="article_java">The JavaScript</h2>
<h3 id="article_java_import">Importing the required files</h3>
<p>This is where everything is tied together and starts working.</p>
<p>First off we need to import some JS files. This solution imports JQuery, as well as a functions.js file that I created in a sub directory of my Theme folder called &#8220;js&#8221;. This was explained in the <a title="Setting up your files" href="#article_setup">Setup</a> section, but I&#8217;m still reiterating.</p>
<p>First we need to add a bit more markup within our &lt;head&gt;&lt;/head&gt; sections:</p>
<pre>&lt;script type="text/JavaScript" src="&lt;?php bloginfo('template_url'); ?&gt;/js/jquery.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript"&gt;
   var templateUrl = "&lt;?php bloginfo('template_url'); ?&gt;";
&lt;/script&gt;
&lt;script type="text/JavaScript" src="&lt;?php bloginfo('template_url'); ?&gt;/js/functions.js"&gt;&lt;/script&gt;</pre>
<p>What we&#8217;re doing here:</p>
<ol>
<li>Importing the JQuery framework.</li>
<li>Setting a JavaScript variable that contains the template_url value from WordPress. This makes life easier when we get to the actual cookie setting.</li>
<li>Importing our functions.js file that will contain all of our functionality.</li>
</ol>
<p><em><strong>Note:</strong> I&#8217;m not a JavaScript expert, and part of me thinks that there should be a way to pass in the template URL to the functions.js import call. I don&#8217;t know and if someone can comment on this and explain, I&#8217;d be more than happy to refine my work.</em></p>
<p>Next up we need to add some functionality to the functions.js file!</p>
<h3 id="article_java_func">Adding functionality</h3>
<p>I&#8217;ll pase the code first and then explain it:</p>
<pre>var $j = jQuery.noConflict();

$j(document).ready(function(){
   $j("#colour_swapper a").click( function(){
	var style_title = $j(this).attr("title");
	var style_href= "";
	var altSheet = $j("#altsheet");

	altSheet.attr({"href":templateUrl +"/" + style_title+".css"});

	style_href = altSheet.attr("href");

	var date = new Date();
	date.setTime(date.getTime()+(1*24*60*60*1000));
	var expires = "; expires="+date.toGMTString();
	document.cookie = "styleHref="+style_href+"; expires="+date.toGMTString()+"; path=/";
   });
 });</pre>
<p><strong>What the hell is going on:</strong></p>
<ol>
<li>Set JQuery to no conflict mode by assigning it to a different variable. I had to do this because of other plugins I use on my site. I maintain that this is a good idea when you are working in a plugin-heavy framework like WordPress.</li>
<li>For every link found within the element with the id of colour_swapper attach a new click function</li>
<li>Set a variable to the value of the links title (Yellow Version, Green Version, etc)</li>
<li>Grab the element with the id of &#8220;altsheet&#8221; (in our case this is a link element in our header) and change it&#8217;s href value to the url of the new style sheet. This url is made up of the template URL and the name of the style sheet (title of the anchor)</li>
<li>Set a variable with the URL the new CSS file to be used.</li>
<li>Create a date variable, and set it to the future (exact day eludes me at the moment. Part of me thinks that it&#8217;s either 24 hours into the future, or 1000 days. I can&#8217;t honestly remember at this point).</li>
<li>Convert the date to a GMTString which is used in the creation of cookies.</li>
<li>Create a cookie, &#8220;styleHref&#8221;, that stores the URL of the style sheet used. This cookie is set to expire in the future, and will apply to all pages within the website (that&#8217;s what the &#8220;/&#8221; is for)</li>
</ol>
<p>Whew! That took longer to explain than I thought.</p>
<h2 id="article_together">Pulling it all together</h2>
<p>So now that we&#8217;ve gone through all that, everything should work. To give you an idea on how it all comes together I&#8217;ll explain the general flow.</p>
<h3 id="article_together_flow">Flow</h3>
<ul>
<li>While the page is loading, try and grab the cookie and set the secondary style sheet. If none is found, apply a default (Yellow Version)</li>
<li>Import all of the JavaScript needed.</li>
<li>User clicks on a style swapping link</li>
<li>JavaScript figures out what style sheet to use</li>
<li>Changes the secondary sheets href to the new CSS file</li>
<li>Sets a cookie to store the URL of the new CSS file</li>
</ul>
<p>Now if a user navigates to another page in your website, the first step will apply the change right off the bat.</p>
<h2 id="article_conclusion">Conclusion</h2>
<p>I hope that this helps someone out there, and comments are more than appreciated to help improve the solution. I&#8217;ll try and refine the solution a little more when I have time.</p>
<p>Good luck and happy coding!</p>
]]></content:encoded>
			<wfw:commentRss>http://wallofscribbles.com/2008/colour-swapper-in-wordpress/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Getting more with the MORE tag</title>
		<link>http://wallofscribbles.com/2008/getting-more-with-the-more-tag/</link>
		<comments>http://wallofscribbles.com/2008/getting-more-with-the-more-tag/#comments</comments>
		<pubDate>Thu, 02 Oct 2008 13:05:26 +0000</pubDate>
		<dc:creator>Corey Dutson</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.wallofscribbles.com/?p=261</guid>
		<description><![CDATA[So who out there uses WordPress? I&#8217;ve been told it&#8217;s somewhat popular. I myself am a fan despite it&#8217;s assault on the CPU and database. It&#8217;s fast, it&#8217;s simple, and so long as your website isn&#8217;t gaining huge traffic (or you&#8217;re paying peanuts for CPU usage and storage) than it&#8217;s a great selection. It&#8217;s fairly [...]]]></description>
			<content:encoded><![CDATA[<p>So who out there uses WordPress? I&#8217;ve been told it&#8217;s <a title="Wordpress: Most popular CMS" href="http://publisherblog.automattic.com/2008/01/23/wordpress-most-popular-cms-in-technoratis-top-100/">somewhat popular</a>.</p>
<p>I myself am a fan despite it&#8217;s <a title="Coding Horror: Behold WordPress, destroyer of CPUs" href="http://www.codinghorror.com/blog/archives/001105.html">assault on the CPU and database</a>. It&#8217;s fast, it&#8217;s simple, and so long as your website isn&#8217;t gaining huge traffic (or you&#8217;re paying peanuts for CPU usage and storage) than it&#8217;s a great selection. It&#8217;s fairly customizable, <a title="Wordress: Extend" href="http://wordpress.org/extend/">has a huge support and user base</a>, and it&#8217;s just damned easy to use. I&#8217;ve been slowly getting more and more into customizing and extending what WordPress can do out of the box.There&#8217;s more in the code than people think.</p>
<p>Recently I <a title="Bartek Gniado" href="http://bart.whahay.net">a friend of mine</a> ripped a strip off of me for only using summaries in my RSS feed. He told me that he, along with other net-savvy users, didn&#8217;t have time to get teased by RSS summaries.</p>
<p><strong>UPDATE: As of December 3rd, 2008 I&#8217;ve turned this baby into a plugin! <a title="Corey Dutson: PostDivider" href="/wordpress-plugins/postdivider/">Check it out.</a></strong></p>
<p><span id="more-261"></span></p>
<p><em>For those wishing to skip the lengthy buildup, here is a little table of contents:</em></p>
<ul>
<li><a title="The Back story" href="#solution_history">The Back Story</a></li>
<li><a title="Solution" href="#solution">Solution</a></li>
<li><a title="Explanation" href="#solution_explanation">Explanation</a></li>
<li><a title="Installation and Usage" href="#solution_installation">Installation and Usage</a></li>
<li><a title="Further Notes" href="#solution_notes">Further Notes</a></li>
</ul>
<h3 id="solution_history">The Back Story &#8211; To Summarize or not to Summarize</h3>
<p>This is some what of a conundrum, as I want people to actually come to my site. On the flip side, I want people to read what I write. So I can force people to come to my site and gain page views, or I can increase my RSS readership. After careful consideration, I&#8217;ve opted to fix my RSS feed to display the entire content.</p>
<p>Here&#8217;s the issue: Until now, I&#8217;ve used &#8216;The Excerpt&#8217; field in WordPress. Basically this allows me to have a pretty excerpt instead of 55 characters truncated with a &#8220;READ MORE PLEAZE!&#8221; I use the custom excerpt for the top part of the post; the preview if you will. The problem with this is that I had to rip out the first couple paragraphs from the post area, add some HTML to make sure it worked, and then post. The result looked pretty, but had some unexpected side-effects on the RSS.</p>
<p>As it turns out, when you decide to use the EXCERPT field, that excerpt will become the summary in the RSS. That&#8217;s all well and good, but what If you want to have the whole post? One would hope that it would stitch the Excerpt and the Post together, but alas this was not the case.</p>
<p>I turned to the &lt;!&#8211;more&#8211;&gt; tag to assist me.</p>
<p>What they don&#8217;t document about the more tag is pretty much all the bitchy parts of it. When you use the &lt;!&#8211;more&#8211;&gt; tag, you split the content, allowing for the excerpt to be defaulted to all the content preceding the tag. This is great, except for how my layout works. You see The top have is the Excerpt, and the bottom half is the rest of the post. Just using the_content wouldn&#8217;t work, because I would be repeating all the pre-more content.</p>
<p></p>
<p>I thought about maybe using an hr tag, or reworking my entire layout, but I dismissed those due to the complexity of the markup. My only option was to carve into WordPress itself. I first checked Google to see if anyone had come up against what I was facing. As it turns out <a title="Wordpress: Support" href="http://wordpress.org/support/topic/184581">people have asked the question</a>, but no one has posted the answer.</p>
<p>I am fixing that right now.</p>
<h3 id="solution">Getting the Pre and Post &lt;!&#8211;more&#8211;&gt; content separately</h3>
<p>Here is my solution, in full:</p>
<blockquote><p>function the_formatted_pre_more_from_content ($body)<br />
{<br />
$returnVal = get_the_formatted_pre_more_from_content ($body);<br />
if ($returnVal !== FALSE)<br />
echo get_the_formatted_pre_more_from_content ($body);<br />
else<br />
the_excerpt();<br />
}</p>
<p>function get_the_formatted_pre_more_from_content ($body)<br />
{<br />
$moreTag = &#8216;&lt;!&#8211;more&#8217;;<br />
$content = FALSE;</p>
<p>$morePos = stripos($body, $moreTag);<br />
if ($morePos !== FALSE || $morePos &gt; -1)<br />
$content = substr($body, 0, $morePos);<br />
else<br />
return FALSE;</p>
<p>$content = apply_filters(&#8216;the_content&#8217;, $content);<br />
$content = str_replace(&#8216;]]&gt;&#8217;, &#8216;]]&gt;&#8217;, $content);</p>
<p>return $content;<br />
}</p>
<p>function the_formatted_post_more_from_content ($body)<br />
{<br />
echo get_the_formatted_post_more_from_content ($body);<br />
}</p>
<p>function get_the_formatted_post_more_from_content ($body)<br />
{<br />
$moreTag = &#8216;&lt;!&#8211;more&#8217;;<br />
$content = FALSE;</p>
<p>$morePos = stripos($body, $moreTag);</p>
<p>if ($morePos !== FALSE || $morePos &gt; -1)<br />
{<br />
$content = substr($body, $morePos + strlen($moreTag));<br />
$morePos = stripos($content, &#8216;&#8211;&gt;&#8217;); // reuse variable<br />
if ($morePos !== FALSE || $morePos &gt; -1)<br />
$content = substr($content, $morePos + 3); // strip off rest of more tag<br />
}<br />
else<br />
$content = $body;</p>
<p>$content = apply_filters(&#8216;the_content&#8217;, $content);<br />
$content = str_replace(&#8216;]]&gt;&#8217;, &#8216;]]&amp;gt;&#8217;, $content);</p>
<p>return $content;<br />
}</p></blockquote>
<h3 id="solution_explanation">Explanation</h3>
<p>The two important functions here are &#8216;get_the_formatted_pre_more_from_content&#8217; and &#8216;get_the_formatted_post_more_from_content&#8217;. Long names, I know, but at least their mission is clear.</p>
<p></p>
<p>The other two functions &#8216;the_formatted_pre_more_from_content&#8217; and &#8216;the_formatted_post_more_from_content&#8217; pretty much add a bit of logic and echo the content automatically. I chose this naming structure and function structure to emulate what is already in WordPress (e.g. &#8216;the_content&#8217; versus &#8216;get_the_content&#8217;).</p>
<h3 id="solution_installation">Installation and Usage</h3>
<p>To use this code, add it all to your functions.php file of your theme. I&#8217;m sure I, or some other enterprising person, could turn this into a plugin, but at the moment I don&#8217;t feel it&#8217;s warranted.</p>
<p>All of the functions take one parameter:<strong> $body</strong>.</p>
<p>When you call these functions you must be in The Loop.</p>
<p><em><strong>example usage</strong>: &lt;?php the_formatted_pre_more_from_content($post-&gt;post_content); ?&gt;</em></p>
<p><em><strong>explanation</strong>: This call will display the content preceding the &lt;!&#8211;more&#8211;&gt; tag.</em></p>
<p><em><strong>example usage</strong>: &lt;?php the_formatted_post_more_from_content($post-&gt;post_content); ?&gt;</em></p>
<p><em><strong>explanation</strong>: This call will display the content proceeding the &lt;!&#8211;more&#8211;&gt; tag.</em></p>
<h3 id="solution_notes">Further Notes</h3>
<p><strong>UPDATE: As of December 3rd, 2008 I&#8217;ve turned this baby into a plugin! <a title="Corey Dutson: PostDivider" href="/wordpress-plugins/postdivider/">Check it out.</a> I&#8217;ve also fixed the post_content bug!<br />
</strong></p>
<p>Note the <strong>$post-&gt;post_content</strong> that is passed into the function. This exists automatically when you are in The Loop. This will pass all of the posts content to the function without any formatting. The only thing that isn&#8217;t straight text &#8211; conveniently &#8211; is the &lt;!&#8211;more&#8211;&gt; tag. As a result the content becomes fairly straight forward.</p>
<p>As of right now you must pass $post-&gt;post_content to the functions. I tried to do it without passing the value, and they don&#8217;t seem to pick up the value.</p>
<p>I opted for using a substring functionality as opposed to an array split function simply because it was 2 am when I finally got this going. I don&#8217;t know which is more efficient, so someone who is more knowledgeable in PHP can comment on this and state which is better.</p>
<p>I only search for &#8216;&lt;!&#8211;more&#8217; because according to the tag documentation, there is text that can follow the MORE that changes some functionality. As a result, I have an additional if statement in the get_post function that will detect for the end of the tag and substring the content again to trim that out.</p>
<p>I hope that this helps out some people who may be in a similar boat as I was.</p>
]]></content:encoded>
			<wfw:commentRss>http://wallofscribbles.com/2008/getting-more-with-the-more-tag/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Respect the Process, Damnit</title>
		<link>http://wallofscribbles.com/2008/respect-the-process-damnit/</link>
		<comments>http://wallofscribbles.com/2008/respect-the-process-damnit/#comments</comments>
		<pubDate>Thu, 14 Feb 2008 05:15:00 +0000</pubDate>
		<dc:creator>Corey Dutson</dc:creator>
				<category><![CDATA[Design]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[diagrams]]></category>
		<category><![CDATA[functional specifications]]></category>
		<category><![CDATA[Internet]]></category>
		<category><![CDATA[process]]></category>
		<category><![CDATA[project]]></category>
		<category><![CDATA[UML]]></category>

		<guid isPermaLink="false">http://www.wallofscribbles.com/2008/02/14/respect-the-process-damnit/</guid>
		<description><![CDATA[I went to college.

Shocking, I know. I did though, and on the lovely diploma that I earned and gently stuffed in a drawer somewhere it says that I am both a computer programmer and a systems analyst. What that means is not only am I  (supposedly) competent at coding solutions, I am also (apparently) competent at looking at a system and figuring out how things should work.

I always chuckled in my Analysis classes. "Come on, this is all common sense!" I would proclaim. I took what the teacher said for a grain of salt and left it at that.]]></description>
			<content:encoded><![CDATA[
<a href="http://wallofscribbles.com/gallery/Misc. Images/Process.jpg" title="" class="thickbox" rel="singlepic537" >
	<img class="ngg-singlepic" src="http://wallofscribbles.com/gallery/cache/537__420x200_Process.jpg" alt="Process.jpg" title="Process.jpg" />
</a>

<p>Fast forward to my current job. I can assure you that there are a plethora of examples that I could choose from for the point I will be making here, but one strikes me as perfect. You see when we developers started there things were volatile, unstructured, and pretty much a mess. My now fellow programmers and myself were hired in the wake of a collection of other developers leaving for one reason or another. We were also thrust into a project pretty much from the moment we sat in our chairs.</p>
<p>This was all fine and good, and we build what we thought we were <em>supposed </em>to build. Three days before we showed it to the client, we went through a run-through with the manager, only to find out that what we built was not what the client wanted. Well okay parts of it were, but for the most part it was off the mark. The result was months of us working grueling hours of overtime, wherein time became an abstract concept and my world knew nothing but code.</p>
<p>What the hell happened?</p>
<h3>We didn&#8217;t have a process.</h3>
<p>We built what we were told to build at the start, but things changed. The functional spec. was updated and we were never told. On the flip side, we also never went looking. Communication was sub-par at best, and the short of it is that there was enough blame for everyone.</p>
<p>We thought we had a process, but the debacle that we ran headlong into demonstrated that whatever process was in place either wasn&#8217;t working or was never initiated. The result was everything blowing up in our face. As it turns out, if you do not learn from this mistake, it will happen again and again. I could pull from more examples, but I&#8217;ll refrain&#8230; sort of.<br />
<br />
I am on a new project, in fact I am the lead developer of said project. It&#8217;s my first time doing this and I&#8217;ll admit that it is a daunting task, but I set about doing my best. I was given a functional specification, which I thought odd because I&#8217;m pretty sure I&#8217;m the one that&#8217;s supposed to build that. I was told to add my section to it and that was that. I did so, to the best of my ability, with the information I had available to me.</p>
<p>It was okayed, and I started handing out tasks and the development cycle began.</p>
<p>Then I was told that the way things were supposed to work wouldn&#8217;t work that way anymore. This was after we had built stuff. This is generally a bad design move, because now any development that was already done now has to be reviewed and possibly scrapped. I updated the spec. again, and away we went. Then I realized that something wasn&#8217;t covered with the information I was given, so I asked a question. One question, I swear to God I only asked one but what was the end result? Another overhaul. Another attempt at the functional spec.</p>
<p>This has happened a  couple times, and is not how the process should work. In a perfect world, the functional spec. is completely done before a line of code is written. The world is not perfect of course, and we have to make do with what we got, but damnit, I wasn&#8217;t about to go and take another stab at anything only to have it change on me again. So I went back to my college roots, and I made a decision:</p>
<h3>I will make the process work again.</h3>
<p>How can I do that though, when everything has apparently been thrown to the wind? It&#8217;s easier than I thought&#8230; well sort of. It still requires effort, but I&#8217;d rather expend the effort where I did fixing the process then rebuilding anything three times.</p>
<p>I sat down with my other main developer, and we went through <em>everything</em>. We went through every aspect of the functional spec, screen designs, and data sources, and figured out how it all works. We asked all the &#8216;hows&#8217; and &#8216;what ifs&#8217; as well as all of the &#8216;what the hells&#8217; that subsequently came up. We went through every resource, created or not, and decided what was needed, what information had to be stored, etc.. We created use cases for the complex cases, and walked through more processes then I would care to count.</p>
<p>The end result? I spent <em>ten hours</em> (minimum) in a board room with a print-enabled white board and wrote everything down. I&#8217;m pretty sure we have at least 15 printouts of notes and questions and diagrams and use cases which helped clear up the utter ambiguity that surrounded the project. They get it now, and so do I. It&#8217;s no longer a matter of &#8220;how does this work?&#8221; but a matter of &#8220;what&#8217;s the next task?&#8221; Yes I have to update the functional spec. again, but at least now I know what to add, and where. The document is finally useful, and the project is understood. The process exists once again.</p>
<p>I&#8217;ve also taken it upon myself to write all of those tasks down in a shared location, so that whoever is on the project can take on new tasks as the run out of their assigned ones. That way no one has the excuse for not knowing what is next.</p>
<p>So yes we lost a day of development, but we have saved ourselves days of effort of unneeded rebuild time. Do it right the first time, and you&#8217;ll save time later.</p>
<p>On a side note, Sorry Mr. Miller turns out you were write all along about Analysis. My Bad.</p>
]]></content:encoded>
			<wfw:commentRss>http://wallofscribbles.com/2008/respect-the-process-damnit/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SharePoint 2007: What the hell, man?</title>
		<link>http://wallofscribbles.com/2008/sharepoint-2007-what-the-hell-man/</link>
		<comments>http://wallofscribbles.com/2008/sharepoint-2007-what-the-hell-man/#comments</comments>
		<pubDate>Thu, 31 Jan 2008 05:15:53 +0000</pubDate>
		<dc:creator>Corey Dutson</dc:creator>
				<category><![CDATA[Bad bad bad]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Features]]></category>
		<category><![CDATA[SharePoint 2007]]></category>

		<guid isPermaLink="false">http://www.wallofscribbles.com/2008/01/31/sharepoint-2007-what-the-hell-man/</guid>
		<description><![CDATA[So I just spent four hours of my life fighting with <a href="http://office.microsoft.com/en-us/sharepointserver/FX100492001033.aspx" title="Microsoft SharePoint 2007" target="_blank">SharePoint 2007</a>. I can't explain all the details because my employer pays me, which in turn pays the bills and they frown upon my telling of company secrets. I can, however, bitch about some things that have been irritating me over the past while. As it turns out everything I hate converged on me today.

Let the story begin!]]></description>
			<content:encoded><![CDATA[<p>So I&#8217;ve been charged with expanding on the functionality of a <a href="http://www.microsoft.com/technet/technetmag/issues/2007/01/Wiki/default.aspx" title="Microsoft TechNet" target="_blank">Wiki Library</a>. For those not in the know, a Wiki Library is part of SharePoint 2007 (not WSS) and allows for some nifty features such as version viewing, article linking, and&#8230; yeah that&#8217;s pretty much it. It does all of this pretty well in and of itself. Woe be upon the person (me) who tries to crack open this walnut of misery.</p>
<p>As it turns out, customizing a Wiki Library to do anything isn&#8217;t just difficult, it&#8217;s not even a chore. It&#8217;s a goddamn mission of epically frustrating scale. Let&#8217;s start off with some over-all items:</p>
<ol>
<li>I needed to create custom columns, some of which looked at lists.</li>
<li>I needed to create a content type that was based off of the Wiki content type.</li>
<li>I needed to customize the Wiki library to have said content type.</li>
<li>I needed to add custom-made web parts to the various views of the Wiki Library (History, Edit, etc)</li>
<li>I needed all of this to work through a feature</li>
</ol>
<p>Where to start? Lets start from the bottom of the list, because as it turns out this was the easiest and where I started.</p>
<p>You want to edit those page layouts eh? Well have fun because as it turns out all of those files are system files, which means they&#8217;re on the hard-drive of the server and therefor shared. That means you can&#8217;t mess with one without causing a server-wide change. The solution? Copy those layout pages and rename them. Now add them to your feature. I&#8217;m not going to explain how to get the feature to deploy, that&#8217;s a different story all together.</p>
<p>
<a href="http://wallofscribbles.com/gallery/Misc. Images/SharePoint Markup.jpg" title="" class="thickbox" rel="singlepic533" >
	<img class="ngg-singlepic" src="http://wallofscribbles.com/gallery/cache/533__200x200_SharePoint Markup.jpg" alt="SharePoint Markup.jpg" title="SharePoint Markup.jpg" />
</a>
 That&#8217;s sweet! Now how do you make <em>anything </em>use those pages? Well in terms of all the little widgets (Versions tool, History Link, Incoming Link) You will have to build your own versions of those controls. Why? Well the URL of the pages that they point to are <em>hard-coded</em>. Simple enough to get around, though annoying as hell. Just to demonstrate, to the left is a screen shot of what SharePoints&#8217; markup looks like just to recreate some of the controls in HTML. Seriously, that&#8217;s messed up. In the end <em>each link</em> was surrounded by <em>two more tables</em>. What the hell man?</p>
<p>Okay so you got all of the default pages redirected. What about when you edit an entry or make a new one? Those pages are tailored specifically for Wiki Pages (CreateWebPage.aspx) and so you&#8217;ll have to copy that one. As for redirecting it? Well you <em>should </em>be able to do it via an Event Receiver attached to the Feature that Installs the custom content type that this is all based from. please note my use of the word &#8216;should&#8217; because I&#8217;m still stuck there.</p>
<p></p>
<p>Let&#8217;s move onto the library for a moment. Now I have not been able to replicate the Wiki library properly, without making my list type use the basetype of &#8217;119&#8242;. As it turns out, this comes with a whole bunch of strings attached, like having hidden name columns and a lot of red-tape. I dare you to try and rename the &#8220;Name&#8221; column to anything with any sort of graceful code. I&#8217;ll leave that one there. For those that are wondering, the default Feature for Wiki Libraries is called &#8220;WebPageLib&#8221; or something of that nature. Try searching the 12 directory for the content type of Wikis. To find that, go looking for the ctypes Feature and look in there.</p>
<p>Creating the feature to house all of this turned out to be the easiest portion of it all, though all of the problems stemmed from it in some shape, way or form. I can&#8217;t bitch too much about the feature markup itself, because all it really lacks is some functionality that should&#8230; well really should just be there. I&#8217;ve had to rig extra code together just to get what I wanted, but read on and I&#8217;ll explain.</p>
<p>I managed to create the content type with little issues, though I had to add Remove references to the WikiPage content type (that does exist by the way, it&#8217;s just stored in the &#8216;_Hidden&#8217; group which is why you can&#8217;t touch it via the site). Adding the new content type to the list template was fun too, because you have to do it via the feature, since Wiki libraries do not allow you to edit the Content Types of the list at all. Like I said, Wikis are sealed, and don&#8217;t like to play with the other kids.</p>
<p>The problems really started when I created the site columns. Just a note to everyone: if you ever feel like creating columns or content types, then using them, and <em>then</em> trying to remove them via a feature, good luck. SharePoint will not remove anything if it&#8217;s being used. Just a helpful tip there. This could be fixed via some EventReceiver code, but I won&#8217;t get into that.</p>
<p>Anyways, I had a column. It was a lookup column. It wanted to look at a specific list, so I gave it the list id (though to be honest this is a bad way to do it because what happens when you want to deploy this feature somewhere else?). I deployed the column, and it worked! Then I tried to use that column in a sub-site, which ended up failing miserably. Took me forever to find out that you cannot specify a web property in the feature. webid and scopeid, yes, but nothing generic (refer to my listId comment here). The solution to this was to add more custom EventReceiver code that would do this work for me. Which worked great until I moved the feature to another site.</p>
<p>This is where my night went bollocks.</p>
<p>I installed the feature onto another dev-site and activated it (all through stsadm). Worked fine. Then I tried to deactivate said feature, and it would just sit there. I could uninstall it fine, but when I deactivated it the process would just hang there and I&#8217;d be forced to end it via Task Manager. It took me 6 ruined dev-sites to realize that I was missing the lists that my lookups were pointing to. Apparently if you tie a lookup column to a list that doesn&#8217;t exist via code, it will cripple the server when you try and remove that column from any content types that it was attached to. When I say cripple, I&#8217;m not kidding. stsadm.exe ran up 50 Mb of resident memory, and 100% of the CPU. The best part was that no error would be logged, it would just hang there forever. I admit I toyed with the idea of letting it run all night and going home.</p>
<p>I kid you not, I laughed like a madman when I finally figured it out. I cannot explain why, but that&#8217;s just how it is. The lesson I learned from this really was that I shouldn&#8217;t have expected SharePoint to have any sort of intelligence sitting behind it, and code for stupidity. Anything you think SharePoint should probably just do probably doesn&#8217;t actually happen, or it happens with a hope and some duct tape.</p>
<p>Oh and for the record, I&#8217;m still stuck on how to change the Edit/New pages properly.</p>
]]></content:encoded>
			<wfw:commentRss>http://wallofscribbles.com/2008/sharepoint-2007-what-the-hell-man/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

