<?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>Tobin Titus &#187; theme</title>
	<atom:link href="http://tobint.com/blog/tag/theme/feed/" rel="self" type="application/rss+xml" />
	<link>http://tobint.com</link>
	<description>You Are What You Don&#039;t Automate</description>
	<lastBuildDate>Fri, 03 Feb 2012 23:05:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Hacking jQuery Slider into WordPress Theme</title>
		<link>http://tobint.com/blog/hacking-jquery-slider-into-wordpress-theme/</link>
		<comments>http://tobint.com/blog/hacking-jquery-slider-into-wordpress-theme/#comments</comments>
		<pubDate>Wed, 23 Dec 2009 16:44:55 +0000</pubDate>
		<dc:creator>tobint</dc:creator>
				<category><![CDATA[technical]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[slider]]></category>
		<category><![CDATA[theme]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://tobint.com/?p=464</guid>
		<description><![CDATA[I have just finished converting most (all?) of my posts from various blogs around the intertubes into WordPress. My previous blog on this domain was running Oxite. I created a theme called &#8216;Titus&#8217; (yeah, I know) for Oxite that included a little jQuery &#8216;Slider&#8217; plugin that I wrote. The control shows my last &#8216;n&#8217; number [...]
Related posts:<ol>
<li><a href='http://tobint.com/blog/setting-up-iphone-wordpress-app/' rel='bookmark' title='Setting up iPhone WordPress app'>Setting up iPhone WordPress app</a></li>
<li><a href='http://tobint.com/blog/hacking-polar-watch-websync-software/' rel='bookmark' title='Hacking Polar Watch WebSync Software'>Hacking Polar Watch WebSync Software</a></li>
<li><a href='http://tobint.com/blog/reading-iis-net-blogs-or-any-rss-with-powershell/' rel='bookmark' title='Reading IIS.NET Blogs (or any RSS) with Powershell'>Reading IIS.NET Blogs (or any RSS) with Powershell</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Ftobint.com%2Fblog%2Fhacking-jquery-slider-into-wordpress-theme%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Ftobint.com%2Fblog%2Fhacking-jquery-slider-into-wordpress-theme%2F&amp;source=tobint&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>I have just finished converting most (all?) of my posts from various blogs around the intertubes into WordPress. My previous blog on this domain was running Oxite. I created a theme called &#8216;Titus&#8217; (yeah, I know) for Oxite that included a little jQuery &#8216;Slider&#8217; plugin that I wrote. The control shows my last &#8216;n&#8217; number of twitter posts, one at a time in a rotating fashion. I named it slider because I originally intended for the content to &#8216;slide&#8217; up from the bottom continuously. Instead, I decided to fade them in/out. I was too lazy to rename it after I decided on my desired effect. After moving to wordpress, I was slightly upset that I was losing my hard work. However, this was a jQuery plugin so I didn&#8217;t see why this couldn&#8217;t just plug into my WordPress theme. I&#8217;m NOT a PHP developer, nor do I pretend to know how to do anything in WordPress. That said, I forged forward in my attempt to get this plugin working. You should see this plugin working right now on this site (unless you are reading this through RSS).</p>
<p>First let&#8217;s define the content of my plugin. I have a Javascript file containing my plugin, a CSS file containing the styling I&#8217;m using on the site, a little HTML markup to add my &#8216;placeholder&#8217; for twitter feed, and of course the dependency on jQuery.</p>
<h2>Installing jQuery into your Theme</h2>
<p>First let&#8217;s tackle the jQuery item.  A current version of the WordPress distribution includes jQuery. The trick is to include jQuery in your site. After a little investigation I found that jQuery was already &#8216;registered&#8217; in WordPress code, but does not, by default, render to the browser.  Somewhere in /wp-includes/script-loader.php you&#8217;ll find:</p>
<pre id="codeSnippet">$scripts-&gt;add( <span style="color: #006080;">'jquery'</span>, <span style="color: #006080;">'/wp-includes/js/jquery/jquery.js'</span>, <span style="color: #0000ff;">false</span>, <span style="color: #006080;">'1.3.2'</span>);</pre>
<p>This line registers the script we want in a dictionary with the key &#8216;jquery&#8217;. After digging further, I found a funcation called &#8216;wp_enqueue_script&#8217; in /wp-includes/functions.wp-scripts.php.</p>
<pre id="codeSnippet"><span style="color: #008000;">/**</span>
<span style="color: #008000;"> * Enqueues script.</span>
<span style="color: #008000;"> *</span>
<span style="color: #008000;"> * Registers the script if src provided (does NOT overwrite) and enqueues.</span>
<span style="color: #008000;"> *</span>
<span style="color: #008000;"> * @since r16</span>
<span style="color: #008000;"> * @see WP_Script::add(), WP_Script::enqueue()</span>
<span style="color: #008000;">*/</span>
<span style="color: #0000ff;">function</span> wp_enqueue_script( $handle, $src = <span style="color: #0000ff;">false</span>,
                            $deps = <span style="color: #0000ff;">array</span>(),
                            $ver = <span style="color: #0000ff;">false</span>,
                            $in_footer = <span style="color: #0000ff;">false</span> ) {
   <span style="color: #0000ff;">global</span> $wp_scripts;
   <span style="color: #0000ff;">if</span> ( !is_a($wp_scripts, <span style="color: #006080;">'WP_Scripts'</span>) )
      $wp_scripts = <span style="color: #0000ff;">new</span> WP_Scripts();
   <span style="color: #0000ff;">if</span> ( $src ) {
      $_handle = explode(<span style="color: #006080;">'?'</span>, $handle);
      $wp_scripts-&gt;add( $_handle[0], $src, $deps, $ver );
      <span style="color: #0000ff;">if</span> ( $in_footer )
         $wp_scripts-&gt;add_data( $_handle[0], <span style="color: #006080;">'group'</span>, 1 );
   }
   $wp_scripts-&gt;enqueue( $handle );
}</pre>
<p>I put two-and-two together and with a little trial and error, I added the following line to /wp-content/themes/inove/header.php right before the call to wp_head():</p>
<pre>&lt;?php wp_enqueue_script('jquery'); ?&gt;</pre>
<p>This method gets the script location from the dictionary and renders a script tag with the location details.</p>
<h2>Installing the slider jQuery Plugin</h2>
<p>If the core did not register our script in the dictionary, we can provide the location details ourself with a second parameter. That is precisely what I needed to do to get the slider.js file added to the template. I added the following line just after the previous line I added in header.php.</p>
<pre id="codeSnippet">&lt;?php wp_enqueue_script(<span style="color: #006080;">'slider'</span>,
                      (get_bloginfo(<span style="color: #006080;">'template_url'</span>) . <span style="color: #006080;">'/js/slider.js'</span>) ); ?&gt;</pre>
<p>The contents of this file are:</p>
<pre>function($) {
   $.fn.twitterClient = $.fn.twitterClient = <span style="color: #0000ff;">function</span>(<span style="color: #0000ff;">params</span>) {
      <span style="color: #0000ff;">var</span> t = $.extend({}, $.fn.twitterClient.defaults, <span style="color: #0000ff;">params</span>);
      $(<span style="color: #0000ff;">this</span>).append(<span style="color: #006080;">'&lt;ul id="twitter_update_list"&gt;&lt;li&gt;&lt;/li&gt;&lt;/ul&gt;'</span>);
      $.getScript(<span style="color: #006080;">"http://twitter.com/javascripts/blogger.js"</span>);
      $.getScript("<span style="color: #006080;">http://twitter.com/statuses/user_timeline/"
</span>                   + t.userName
                   + <span style="color: #006080;">".json?callback=twitterCallback2&amp;count="
</span>                   + t.tweetCount,
<span style="color: #0000ff;">                   function</span>() {
<span style="color: #0000ff;">                      var</span> list = $(<span style="color: #006080;">"ul#twitter_update_list"</span>);
                      stopTick(list);
                      list.items = $(<span style="color: #006080;">"li"</span>, list);
                      list.items.not(<span style="color: #006080;">":eq(0)"</span>).hide().end();
                      list.currentitem = 0;
                      startTick(list);
                  }
      );
      startTick = <span style="color: #0000ff;">function</span>(list) {
         list.tick = setInterval(
            <span style="color: #0000ff;">function</span>() { tickFunction(list) },
            (t.delaySeconds * 1000)
         )};
      stopTick = <span style="color: #0000ff;">function</span>(list) {
         clearInterval(list.tick);
         };
      tickFunction = <span style="color: #0000ff;">function</span>(list) {
         <span style="color: #0000ff;">if</span> (list.pause) <span style="color: #0000ff;">return</span>;
         list.pause = <span style="color: #0000ff;">true</span>;
         $(list.items[list.currentitem]).fadeOut(<span style="color: #006080;">"slow"</span>,
            <span style="color: #0000ff;">function</span>() {
               $(<span style="color: #0000ff;">this</span>).hide();
               list.currentitem = ++list.currentitem % (list.items.size());
               $(list.items[list.currentitem]).fadeIn( <span style="color: #006080;">"slow"</span>,
                  <span style="color: #0000ff;">function</span>() {
                     list.pause = <span style="color: #0000ff;">false</span>;
                  });
        });
     };
     <span style="color: #0000ff;">this</span>.each( <span style="color: #0000ff;">function</span>() {
                 <span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">this</span>.nodeName.toLowerCase() != <span style="color: #006080;">"ul"</span>) <span style="color: #0000ff;">return</span>;
                }).addClass(t.cssClass)
             <span style="color: #0000ff;">return</span> $(<span style="color: #006080;">"ul#twitter_update_list"</span>);
     };
     $.fn.twitterClient.defaults = {
         userName: <span style="color: #0000ff;">null</span>,
         tweetCount: 10,
         delaySeconds: 5,
         cssClass: <span style="color: #006080;">"twitterClient"</span>
     };
})(jQuery);</pre>
<p>Next, I needed to add my styles for the twitter stream into my template. A method intuitively similar to that for scripts was found called &#8216;wp_enqueue_style&#8217; that allowed me to register my CSS for rendering:</p>
<pre>&lt;?php wp_enqueue_style('slider',
                     (get_bloginfo('template_url') . '/js/slider.css') ); ?&gt;</pre>
<p>The contents of this file were:</p>
<pre>/*ID:   slider  Elements: slider UL, slider LI, slider LI A */
#slider {
      position: relative; top: 5px;  width: 470px;  color: #bbbbbb;
}
#slider ul, #slider li{
   margin:0;  padding:0;  list-style:none;
}
#slider li {
   width:470px;  height:70px;  overflow:hidden;
}
#slider li a {
   text-decoration: none;
}</pre>
<p>I saved the header file and closed it.</p>
<h2>Add the jQuery plug-in Placeholder</h2>
<p>All of our infrastructure is in place. Now I need to add a placeholder, and tell jQuery to call my plugin against the placeholder. Luckily, this style already has the ability to add content to the header region of the template. I simply went into WordPress addmen, and went to &#8220;Current Theme Options&#8221; under &#8216;Appearance&#8217; and added the following to the &#8220;Banner&#8221; section:</p>
<pre>&lt;!-- Slider --&gt;
 &lt;div id="slider"&gt;&lt;div id="twitterClient"&gt;&lt;/div&gt;&lt;/div&gt;
 &lt;script type="text/javascript"&gt;
  jQuery(document).ready(function($) {
    $("#twitterClient").twitterClient({
                        userName: "tobint",
                        tweetCount: 10,
                        delaySeconds: 5
                        });
    });
 &lt;/script&gt;
&lt;!-- /Slider --&gt;</pre>
<p>I also checked the boxes above to display this content for registered users, commenters, and visitors. I saved the file header options and viewed my site. Much to my glee, everything worked just great. My next steps are to turn this into a widget so others can just add this to any registered sidebar for a given theme.</p>
<p>Let me know if you have any questions.</p>
<p>Related posts:<ol>
<li><a href='http://tobint.com/blog/setting-up-iphone-wordpress-app/' rel='bookmark' title='Setting up iPhone WordPress app'>Setting up iPhone WordPress app</a></li>
<li><a href='http://tobint.com/blog/hacking-polar-watch-websync-software/' rel='bookmark' title='Hacking Polar Watch WebSync Software'>Hacking Polar Watch WebSync Software</a></li>
<li><a href='http://tobint.com/blog/reading-iis-net-blogs-or-any-rss-with-powershell/' rel='bookmark' title='Reading IIS.NET Blogs (or any RSS) with Powershell'>Reading IIS.NET Blogs (or any RSS) with Powershell</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://tobint.com/blog/hacking-jquery-slider-into-wordpress-theme/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

