<?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>stevenclark.com.au &#187; snippets</title>
	<atom:link href="http://stevenclark.com.au/category/snippets/feed/" rel="self" type="application/rss+xml" />
	<link>http://stevenclark.com.au</link>
	<description></description>
	<lastBuildDate>Tue, 22 May 2012 09:55:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>WP Dynamic Body ID and Current Page</title>
		<link>http://stevenclark.com.au/2008/09/04/wp-dynamic-body-id-and-current-page/</link>
		<comments>http://stevenclark.com.au/2008/09/04/wp-dynamic-body-id-and-current-page/#comments</comments>
		<pubDate>Thu, 04 Sep 2008 00:47:42 +0000</pubDate>
		<dc:creator>steven</dc:creator>
				<category><![CDATA[snippets]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://stevenclark.com.au/2008/09/04/wp-dynamic-body-id-and-current-page/</guid>
		<description><![CDATA[Referring back to an old post on using a Dynamic Body ID in WordPress you might notice that anything in WordPress that is not a page is actually a part of the blog &#8211; including search, single, archive and the posts page. The following code should refresh your memory on how to use a dyanamic [...]]]></description>
			<content:encoded><![CDATA[<p>Referring back to an old post on using a <a href="http://stevenclark.com.au/2008/02/24/dynamic-body-id-in-wordpress/">Dynamic Body ID in WordPress</a> you might notice that anything in WordPress that is not a page is actually a part of the blog &#8211; including search, single, archive and the posts page. The following code should refresh your memory on how to use a dyanamic body ID. If it&#8217;s not a page then give the body an ID of News, else if it is a page with the following titles then the appropriate body ID of the title is used, otherwise just insert the body tag.</p>
<p><code>&lt;?php if ((!is_page())) { ?&gt;<br />
<strong>&lt;body id="News"&gt;</strong><br />
&lt;?php } elseif ((is_page('Home')) || (is_page('Studio')) || (is_page('Support')) || (is_page('About')) || (is_page('Contact')))<br />
{ ?&gt;<br />
<strong>&lt;body id="&lt;?php the_title(); ?&gt;"&gt;</strong><br />
&lt;?php } else { ?&gt;<br />
<strong>&lt;body&gt;</strong><br />
&lt;?php } ?&gt;</code></p>
<p><span id="more-683"></span></p>
<p>This is a handy base for doing something useful so I thought a simple continued snippet or two might open your mind to the possibilities. For example, you might like to highlight the current page in your navigation. Even better, you could highlight the current page but keep the News (blog) link highlighted while users are on all the blog related pages such as search, archive etcetera. Sounds logical and it&#8217;s easy enough to achieve (This example comes from the <a href="http://hunterislandpress.org.au">Hunter Island Press</a> theme).</p>
<p>We&#8217;ll start by putting a few more IDs into the links on the horizontal navigation bar. In this case the navigation is made up of a simple unordered list of links. Note that the ID is placed directly on the unordered list and not on a containing div. You <a href="http://stevenclark.com.au/2008/07/31/correct-use-of-div-and-span/">don&#8217;t need</a> to put a block level element into a div container, it&#8217;s better to just work directly with the list.</p>
<pre><code>&lt;ul id="navbar"&gt;
    &lt;li&gt;&lt;a id="homelink" href="#"&gt;Home&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a id="studiolink" href="#"&gt;Studio&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a id="supportlink" href="#"&gt;Support&lt;/a&gt;&lt;/l&gt;
    &lt;li&gt;&lt;a id="aboutlink" href="#"&gt;About&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a id="newslink" href="#"&gt;News&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a id="contactlink" href="#"&gt;Contact&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</code></pre>
<p>The next task is to style the horizontal navigation bar without the current page tab highlighted. When you get this working you can worry about the current tab later.</p>
<p><code>ul#navbar {<br />
position: static;<br />
margin: 0;<br />
padding: 0;<br />
width: 100%;<br />
display: block;<br />
list-style-type: none;<br />
background-color: #000;}ul#navbar li { display: inline; }</code></p>
<p><code>ul#navbar li { display: inline; }</code></p>
<p><code>ul#navbar li a {<br />
margin: 0;<br />
padding: 9px 0;<br />
width: 16.6%;<br />
min-width: 70px;<br />
height: 40px;<br />
float: left;<br />
border-bottom: 4px solid #000;<br />
font-size: .9em;<br />
font-family: helvetica, verdana, arial, sans-serif;<br />
color: #fff;<br />
text-align: center;<br />
text-decoration: none; text-transform: uppercase;<br />
background: #000; }<br />
ul#navbar li a:hover {<br />
border-bottom: 4px solid #8F0000;<br />
background: #222; }</code></p>
<p>As a snippet goes you can take or leave a lot of what is in there (including the min-width that applies to the fluid layout of <a href="http://hunterislandpress.org.au">Hunter Island Press</a>). But all that&#8217;s left for us to achieve is the highlighting of the current tab &#8211; specifically so that users entering the news get a consistent tab for the section. We achieve this by associating the body ID with the link ID in the navigation bar. If any of the following selectors are present the background and bottom border will be changed to appear as the current tab.</p>
<p><code>body#Home li a#homelink,<br />
body#Studio li a#studiolink,<br />
body#Support li a#supportlink,<br />
body#About li a#aboutlink,<br />
body#News li a#newslink,<br />
body#Contact li a#contactlink {<br />
background: #222;<br />
border-bottom: 4px solid #8f0000; }</code></p>
<p>Hopefully you can take something about using <a href="http://www.wordpress.org">WordPress</a> away from this tutorial and extend the concept of the dynamic body ID further. Enjoy.</p>
]]></content:encoded>
			<wfw:commentRss>http://stevenclark.com.au/2008/09/04/wp-dynamic-body-id-and-current-page/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL ConnectToDatabase Function</title>
		<link>http://stevenclark.com.au/2008/08/23/sql-connecttodatabase-function/</link>
		<comments>http://stevenclark.com.au/2008/08/23/sql-connecttodatabase-function/#comments</comments>
		<pubDate>Sat, 23 Aug 2008 00:49:41 +0000</pubDate>
		<dc:creator>steven</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[snippets]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://stevenclark.com.au/2008/08/23/sql-connecttodatabase-function/</guid>
		<description><![CDATA[Carrying on from my last post about connecting to an SQL database using PHP, here are the functions I use to open and close database connections (Note: » just extends the line, you&#8217;ll need to remove that for this to work correctly). // to open the database function connectToDatabase () { $link = mysql_connect("localhost", "database_name", [...]]]></description>
			<content:encoded><![CDATA[<p>Carrying on from my last post about <a href="http://stevenclark.com.au/2008/08/22/sql-tip-open-connection-query-close/">connecting to an <acronym title="Structured Query Language">SQL</acronym> database</a> using <acronym title="Hypertext Preprocessor">PHP</acronym>, here are the functions I use to open and close database connections (Note: » just extends the line, you&#8217;ll need to remove that for this to work correctly).</p>
<pre>
<code>// to open the database
function connectToDatabase ()	{
$link = mysql_connect("localhost", "database_name", »
                                  "database_password");
    if ($link &amp;&amp; mysql_select_db("database_name"));
        return ($link);
	    return (FALSE);
}</code>

<code>// to close the database
function db_close($connecter)  {
    mysql_close($connecter);
}</code></pre>
<p>It&#8217;s a good idea to keep these database connection functions in one place and then reuse them as you need, rather than repeating them everywhere.</p>
<p><span id="more-676"></span></p>
<p>Maintainability of your code is important even on smaller websites, if you only need to change a password in one place you&#8217;ll find life a lot easier. This code is very close to that found in PHP docs for <a href="http://au2.php.net/mysql_connect">mysql_connect</a>, by the way. The connectToDatabase function returns a link identifier on success or FALSE on failure.</p>
<p>Further information in the mysql_connect documentation should take you an extra step as well.</p>
<p>So, in your PHP code where you want to run your SQL query all you need to do is something like the following snippet:</p>
<pre>
<code>
// connect to the database
include("down/in_some/folder/db_function_file.php");
$link = connectToDatabase();
    if(!link) {
        print "&lt;p&gt;database connection error&lt;/p&gt;";
        mysql_close($link);
	exit();
}
</code></pre>
<p>Now run your query and put the values into variables, then explicitly close the link, and you&#8217;re done. Hopefully this snippet of advice might be useful.</p>
]]></content:encoded>
			<wfw:commentRss>http://stevenclark.com.au/2008/08/23/sql-connecttodatabase-function/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>SQL for Comments and Pings in WordPress</title>
		<link>http://stevenclark.com.au/2008/08/17/sql-for-comments-and-pings-in-wordpress/</link>
		<comments>http://stevenclark.com.au/2008/08/17/sql-for-comments-and-pings-in-wordpress/#comments</comments>
		<pubDate>Sun, 17 Aug 2008 00:44:41 +0000</pubDate>
		<dc:creator>steven</dc:creator>
				<category><![CDATA[snippets]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://stevenclark.com.au/2008/08/17/sql-for-comments-and-pings-in-wordpress/</guid>
		<description><![CDATA[Just a snippet of SQL (Structured Query Language) that comes in handy for WordPress. If you have phpMyAdmin on your account you can just cut and paste these two queries into the SQL area and you&#8217;re away. What do they do? If you&#8217;re being hammered by spam you don&#8217;t want to have to revisit every [...]]]></description>
			<content:encoded><![CDATA[<p>Just a snippet of SQL (Structured Query Language) that comes in handy for <a href="http://wordpress.org">WordPress</a>. If you have <a href="http://www.phpmyadmin.net/">phpMyAdmin</a> on your account you can just cut and paste these two queries into the SQL area and you&#8217;re away. What do they do? If you&#8217;re being hammered by spam you don&#8217;t want to have to revisit every article and shut off comments and pings &#8211; in the past I&#8217;ve found the WordPress administration area gave unreliable results at times when turning them on and off. This method offers fine grained control.</p>
<p>So the first query just closes all comments and pings on every article. The second opens comments and pings on twenty articles &#8211; change the number on the end to suit your needs. So to close them:</p>
<p><code>update wp_posts set comment_status='closed', ping_status='closed' where comment_status='open' or ping_status='open'</code></p>
<p>and to reopen them:</p>
<p><code>update wp_posts set comment_status='open', ping_status='open' where comment_status='closed' or ping_status='closed' order by wp_posts.ID desc limit 20;</code></p>
]]></content:encoded>
			<wfw:commentRss>http://stevenclark.com.au/2008/08/17/sql-for-comments-and-pings-in-wordpress/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>More Accessible Popup Windows (If Coerced)</title>
		<link>http://stevenclark.com.au/2008/08/10/more-accessible-popup-windows-if-coerced/</link>
		<comments>http://stevenclark.com.au/2008/08/10/more-accessible-popup-windows-if-coerced/#comments</comments>
		<pubDate>Sun, 10 Aug 2008 05:44:33 +0000</pubDate>
		<dc:creator>steven</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[snippets]]></category>

		<guid isPermaLink="false">http://stevenclark.com.au/2008/08/10/more-accessible-popup-windows-if-coerced/</guid>
		<description><![CDATA[There&#8217;s something extremely annoying about pop-up windows and yet clients often seem to demand them as though they were the answer to that internal fear users will follow a link and never return. My advice from the beginning is if you have such a link remove it immediately &#8211; problem solved. There&#8217;s a lot to [...]]]></description>
			<content:encoded><![CDATA[<p>There&#8217;s something extremely annoying about pop-up windows and yet clients often seem to demand them as though they were the answer to that internal fear users will follow a link and never return. My advice from the beginning is if you have such a link remove it immediately &#8211; problem solved. There&#8217;s a lot to be said for never linking to sites far more interesting than your own.</p>
<p>In real life this fear just isn&#8217;t founded and the last thing you should be doing is trying to hold your users captive. If they want to leave then let them, for Christ&#8217;s Sake! It&#8217;s repeat visitors and conversions that make a successful site not keeping your window open in some sub-level dungeon on the task bar. You&#8217;ve probably figured already that pop-ups confront users with a back button that isn&#8217;t going to work. And sometimes the window is chromeless. It&#8217;s obtrusive, offends a large number of your users, and will confuse the hell out of sighted users let alone someone using assistive technology. And they should never EVER occur without a warning! OK the rant is over for now so we&#8217;ll carry on (we understand that pop-up windows are <a href="http://www.useit.com/alertbox/990530.html">bad for usability</a> and shitty and considered industry worst practice). About the only thing you should be popping up in a new window is a PDF (Portable Document Format) file in its native application.</p>
<p><span id="more-667"></span></p>
<p>But sometimes you&#8217;re forced with your back against the wall to suck air and implement the suckers! You&#8217;re handed deprecated <code>target="_blank"</code> attributes on external links that smack the users in the forehead [Steven takes forefinger and pokes each reader three or four times firmly on the forehead saying "How do you like that?!"]. Delete all those <code>target="_blank"</code> attributes for a start, its a behaviour not content and you&#8217;ll oneday have to go clean the suckers out of there eventually so now is the best time to do it. They&#8217;re even the most primitive way of achieving popup windows, so get the fuckers out of there now! Begone shite. Don&#8217;t you loathe the way <code>target="_blank"</code> instantly throws up a full sized new window &#8211; Whoa, where the friggen hell am I again? WTF (again)? How about letting me, the user, make the decision about whether or not to open a new window [ranting again!].</p>
<p>OK the torture is getting prolonged here. If coerced into doing this, you need to look at pop-ups as a behaviour layer issue. If it&#8217;s this or <code>target="_blank"</code>. Yes it&#8217;s <a href="http://www.smashingmagazine.com/2007/09/27/10-usability-nightmares-you-should-be-aware-of/">sucky</a>, but at least you can implement them in a sensible way if you&#8217;re ever coerced by the prospect of unemployment. So you&#8217;re looking for an <a href="http://onlinetools.org/articles/unobtrusivejavascript/">Unobtrusive JavaScript</a> solution, not a content solution to achieving your result. The benefit of this is you can at least provide for <a href="http://accessites.org/site/2007/02/graceful-degradation-progressive-enhancement/">graceful degradation</a> (if they don&#8217;t have JavaScript they get a normally functioning link to a new page), window size (definately not full screen) and browser features available to the user (preferably all of the features enabled).</p>
<p>I&#8217;m not sure where exactly this snippet came into my arsenal (mainly because I have hardly ever had a call to use it). But maybe it will come in handy for your coerced situation. There&#8217;s no crime in trying to make an intolerable thing out of an abject bad situation. Name this script popups.js (I should metion this code wasn&#8217;t written by me) and call it from an external file in the head section of your document. Then when you want your external link to open in a new window just include <code>rel="external"</code> as an attribute of the link. In that way, only links you specifically want to open in new windows will do so.</p>
<pre>
<code>function prepareLinks()
{
    if(!document.getElementsByTagName) return false;
    var links = document.getElementsByTagName("a");
        for(var i=0; i&lt;links.length; i++
        {
            if((links[i].getAttribute("rel") == "external") ||
               (links[i].getAttribute("rel") == "pdf"))
           {
                links[i].onclick = function()
               {
                   popUp(this.getAttribute("href"));
                   return false;
              }
          }
    }
}</code>
<code>function popUp(winURL)
{
    window.open(winURL, "external", "width=420,
    status=1,scrollbars=1,toolbar=1,
    location=1,menubar=1,
    directories=1,resizable=1");
}</code></pre>
<p>Roger Johansson has <a href="http://www.456bereastreet.com/archive/200605/using_javascript_instead_of_target_to_open_new_windows/">another script</a> using class instead of rel to trigger the opening of new windows. My preference goes to the rel attribute, but you&#8217;re free to choose. Either way these scripts fall back nicely for non-JavaScript enabled users. So, overlooking the suckiness of having to implement a feature that continuously turns up as one of the 10 biggest web design mistakes, and was identified as long ago as 1999 by Jakob Nielsen as having usability issues &#8211; there you go. You can run along and shoot your foot off with it.</p>
<p>If I recall where I picked this script up I&#8217;ll put a link here, it&#8217;s a rather short list of suspects. Best of luck. And doesn&#8217;t it strike you as odd that since popups have been criticised now for at least 9 years at a high professional level we&#8217;re still having to do this shite? I&#8217;m friggen bowled over. Another WTF moment brought to you by the letter K and the numbers 3, 4 and 25. Exit and run&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://stevenclark.com.au/2008/08/10/more-accessible-popup-windows-if-coerced/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Dynamic Body ID in WordPress</title>
		<link>http://stevenclark.com.au/2008/02/24/dynamic-body-id-in-wordpress/</link>
		<comments>http://stevenclark.com.au/2008/02/24/dynamic-body-id-in-wordpress/#comments</comments>
		<pubDate>Sun, 24 Feb 2008 09:49:55 +0000</pubDate>
		<dc:creator>steven</dc:creator>
				<category><![CDATA[snippets]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://stevenclark.com.au/2008/02/24/dynamic-body-id-in-wordpress/</guid>
		<description><![CDATA[As Elliot Jay Stocks is throwing a tidbit of WordPress tutorial out there today on creating dynamic body IDs and Classes it seems appropriate to follow the bandwagon. In short it depends a little on what you are out to achieve I suppose. My example is the recent Hunter Island Press redesign. &#60;?php if ((!is_page())) [...]]]></description>
			<content:encoded><![CDATA[<p>As <a href="http://elliotjaystocks.com/blog/archive/2008/wordpress-tutorialhow-to-apply-a-dynamic-body-class-or-id/">Elliot Jay Stocks</a> is throwing a tidbit of WordPress tutorial out there today on creating dynamic body IDs and Classes it seems appropriate to follow the bandwagon. In short it depends a little on what you are out to achieve I suppose. My example is the recent <a href="http://hunterislandpress.org.au">Hunter Island Press</a> redesign.</p>
<p><code>&lt;?php if ((!is_page())) { ?&gt; <br />
    <strong>&lt;body id="News"&gt;</strong><br />
&lt;?php } elseif ((is_page('Home')) || (is_page('Studio')) || (is_page('Support')) || (is_page('About')) || (is_page('Contact')))<br />
    { ?&gt; <br />
    <strong>&lt;body id="&lt;?php the_title(); ?&gt;"&gt;</strong><br />
&lt;?php } else { ?&gt;<br />
    <strong>&lt;body&gt;</strong><br />
&lt;?php } ?&gt;</code></p>
<p>What this snippet does in English is it says &#8211; if this isn&#8217;t a page (which would mean its a part of the news section &#8211; blog, archive, single, search) then the body id will always be News. This allows for me to keep the News tab highlighted in the horizontal navigation bar. Else if the page is Home, Studio, Support, About or Contact (which are other members of the horizontal navigation bar) then the body id will use the page title and that tab remains highlighted on the navigation bar. Otherwise if its any of the other pages (like the ones in the content rich footer) just use a plain old body element.</p>
<p>One thing about using <acronym title="Hypertext Preprocessor">PHP</acronym> in your WordPress templates is the power of skinning a cat whichever way you need to on the day. Simply understanding the available <a href="http://codex.wordpress.org/Conditional_Tags">WordPress Conditionals</a> puts some gusto into your possiblities. What was the last way you skun that dynamic body id in WordPress?</p>
]]></content:encoded>
			<wfw:commentRss>http://stevenclark.com.au/2008/02/24/dynamic-body-id-in-wordpress/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>WordPress: Page Within a Page</title>
		<link>http://stevenclark.com.au/2007/11/23/wordpress-page-within-a-page/</link>
		<comments>http://stevenclark.com.au/2007/11/23/wordpress-page-within-a-page/#comments</comments>
		<pubDate>Thu, 22 Nov 2007 20:58:34 +0000</pubDate>
		<dc:creator>steven</dc:creator>
				<category><![CDATA[snippets]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://stevenclark.com.au/2007/11/23/wordpress-page-within-a-page/</guid>
		<description><![CDATA[One of the limiting factors I see within many wordpress designs is the tendency to remain blog-like even though a site might require a different look and feel to the standard two column generic layout supplied by the default template. Merely changing a header image and filling in some basic colour changes doesn&#8217;t make a [...]]]></description>
			<content:encoded><![CDATA[<p>One of the limiting factors I see within many wordpress designs is the tendency to remain blog-like even though a site might require a different look and feel to the standard two column generic layout supplied by the default template. Merely changing a header image and filling in some basic colour changes doesn&#8217;t make a solid customisation. You can, in fact, make WordPress look exactly like you want with a few customisations of the templates themselves. If you can draw it you can most likely build it &#8211; something worth considering.</p>
<p>To that end I suggest you may find use for the following code snippet which allows you to call the content of any authored page on your WordPress site into a sup-part of another page &#8211; perhaps index.php could do with some static content.</p>
<p><code>&lt;?php global $wpdb;</code><br />
<code>$pages = $wpdb-&gt;get_results ("SELECT post_content FROM " . $wpdb-&gt;posts . " WHERE ID = 15"); </code><br />
<code>foreach ($pages as $page)</code><br />
<code>{</code><br />
<code>echo $page-&gt;post_content;</code><br />
<code>} ?&gt;</code></p>
<p>So why would you do this? Well I&#8217;ve found great use with clients who I only want to allow editing of a certain section of a page. For example, a portion where they offer a weekly special. I don&#8217;t want them playing in the code (no way) and I don&#8217;t want them going to a full page with other information they might inadvertently junk up on me. So I&#8217;ll jump into the template and call this snippet where the ID equals the page that contains the content your client can edit. Its that simple really. The client is given the name of that specific page (in this case it is called specials) and they just open that one page when they need to update specials on their site.</p>
<p>You should note that creating your new page will add it into your pages navigation list called by the wp-list-pages() function. You&#8217;ll probably want to exclude it with a parameter of <code>&amp;exclude=15</code> where the number is that of your specials page.</p>
]]></content:encoded>
			<wfw:commentRss>http://stevenclark.com.au/2007/11/23/wordpress-page-within-a-page/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

