<?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>Sycha Web Design &#38; Development&#187; Magento, WordPress, jQuery, CSS | Web Development Resources</title>
	<atom:link href="http://www.sycha.com/resources/web-development/feed" rel="self" type="application/rss+xml" />
	<link>http://www.sycha.com</link>
	<description>Website Design &#38; Development</description>
	<lastBuildDate>Thu, 02 Sep 2010 21:11:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>WordPress Security Tip: Remove Generator Meta Tag (wp_generator)</title>
		<link>http://www.sycha.com/wordpress-security-tip-remove-generator-meta-tag</link>
		<comments>http://www.sycha.com/wordpress-security-tip-remove-generator-meta-tag#comments</comments>
		<pubDate>Thu, 25 Mar 2010 02:56:58 +0000</pubDate>
		<dc:creator>Charlie Evans</dc:creator>
				<category><![CDATA[WordPress Development]]></category>

		<guid isPermaLink="false">http://www.sycha.com/?p=947</guid>
		<description><![CDATA[Sometimes when a security hole is found in WordPress, the cyber-baddies will target specific versions of WordPress that are vulnerable to the exploit. There's no easier way for them to see which version a site is running, than by looking for the "generator" meta tag, which is added by the wp_head() function. ]]></description>
			<content:encoded><![CDATA[<p>Sometimes when a security hole is found in WordPress, the cyber-baddies will target specific versions of WordPress that are vulnerable to the exploit. There&#8217;s no easier way for them to see which version a site is running, than by looking for the &#8220;generator&#8221; meta tag, which is added by the wp_head() function. This tag specifies the WordPress version in it&#8217;s content attribute.</p>
<p>It looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;meta name=&quot;generator&quot; content=&quot;WordPress 2.9.0&quot; /&gt;</pre></div></div>

<p>To remove this meta tag, simply add the following line to your functions.php file in your theme:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">remove_action<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'wp_head'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'wp_generator'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.sycha.com/wordpress-security-tip-remove-generator-meta-tag/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Add Google Analytics to your Magento Store</title>
		<link>http://www.sycha.com/google-analytics-magento</link>
		<comments>http://www.sycha.com/google-analytics-magento#comments</comments>
		<pubDate>Fri, 12 Mar 2010 04:21:01 +0000</pubDate>
		<dc:creator>Charlie Evans</dc:creator>
				<category><![CDATA[Magento Development]]></category>

		<guid isPermaLink="false">http://www.sycha.com/?p=897</guid>
		<description><![CDATA[If you want to add Google Analytics to your Magento store, don't go diving into templates or setting fancy layout updates.. The good folks at Magento have made it nice and easy; simply enable Google Analytics in your Magento admin.]]></description>
			<content:encoded><![CDATA[<p>If you want to add Google Analytics to your Magento store, don&#8217;t go diving into templates or setting fancy layout updates.. The good folks at Magento have made it nice and easy; simply enable Google Analytics in your Magento admin.</p>
<ol>
<li>Sign up for your Google Analytics account here:<br />
<a href="http://www.google.com/analytics/sign_up.html" target="_blank">http://www.google.com/analytics/sign_up.html</a></li>
<li>Make note of your Google Analytics account number,<br />
e.g. UA-xxxxxx-x</li>
<li>Go to your Magento admin and visit the following screen:<br />
<em>System &gt; Configuration &gt; Sales &gt; Google API</em></li>
<li>Expand the Google Analytics block, Enable and enter your Account Number</li>
</ol>
<p><img class="alignnone size-full wp-image-905" title="Magento Google Analytics" src="http://www.sycha.com/wp-content/uploads/2010/03/magento-google-analytics.png" alt="" width="600" height="296" /></p>
<p>Sit back and watch those stats start rolling in..<br />
Doesn&#8217;t get much simpler than that &#8211; cheers Mage and Goog <img src='http://www.sycha.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<div class="notice">
<p>There&#8217;s a known javascript bug with the Google Analytics  implementation in Magento version 1.4.0.1<br />
<a href="http://www.magentocommerce.com/bug-tracking/issue?issue=8658" target="_blank">http://www.magentocommerce.com/bug-tracking/issue?issue=8658</a></p>
<p>As mentioned in the bug report, this will be fixed in the next  stable release, so until then the fix is simple:</p>
<p>Edit the following core file:<br />
<em>magento/app/code/core/Mage/GoogleAnalytics/Block/Ga.php</em></p>
<p>Add the following line at line at around line 180 just above _gaq.push</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> _gaq <span style="color: #339933;">=</span> _gaq  <span style="color: #339933;">||</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></pre></div></div>

</div>
]]></content:encoded>
			<wfw:commentRss>http://www.sycha.com/google-analytics-magento/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CSS clearfix: make a floated element automatically fill it&#8217;s parent container</title>
		<link>http://www.sycha.com/css-clearfix-floated-element-automatically-fill-parent-container</link>
		<comments>http://www.sycha.com/css-clearfix-floated-element-automatically-fill-parent-container#comments</comments>
		<pubDate>Fri, 26 Feb 2010 00:59:04 +0000</pubDate>
		<dc:creator>Charlie Evans</dc:creator>
				<category><![CDATA[HTML / CSS Resources]]></category>

		<guid isPermaLink="false">http://www.sycha.com/?p=966</guid>
		<description><![CDATA[Basically this trick 'fixes' the problem that occurs when a floated element doesn't automatically fill out it's parent container..]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m not claiming for a second that clearfix is my invention.. If you want some history of it&#8217;s creators, check out this link:<br />
<a href="http://www.positioniseverything.net/easyclearing.html" rel="nofollow" target="_blank">http://www.positioniseverything.net/easyclearing.html</a></p>
<p>As with a lot of tips and techniques on this site, I&#8217;m just making a record of a technique that I use all the time and a version of it that I&#8217;m confident works. This also ensures that I have an online reference that isn&#8217;t going to disappear someday..</p>
<p>So anyway, onto clearfix <img src='http://www.sycha.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Basically this trick &#8216;fixes&#8217; the problem that occurs when a floated element doesn&#8217;t automatically fill out it&#8217;s parent container..<br />
Here&#8217;s some example HTML and an image to illustrate this scenario:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;div style=&quot;border:1px solid blue;padding:5px;&quot;&gt;
	&lt;div style=&quot;float:right;width:49%;height:50px;background:green;&quot;&gt;
		&amp;nbsp;
	&lt;/div&gt;	
&nbsp;
	&lt;div style=&quot;width:49%;height:20px;background:red;&quot;&gt;
		&amp;nbsp;
	&lt;/div&gt;
&lt;/div&gt;</pre></div></div>

<p><img src="http://www.sycha.com/wp-content/uploads/2010/02/floated-element-clearing-problem.png" alt="" title="Floated element clearing problem" width="408" height="71" class="alignnone size-full wp-image-971" /><br />
As you can see, the green div is extending out of it&#8217;s parent container.<br />
i.e. it&#8217;s not expanding the container automatically.</p>
<p>To apply clearfix, simply use the following CSS:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.clearfix</span><span style="color: #3333ff;">:after </span><span style="color: #00AA00;">&#123;</span>
     <span style="color: #000000; font-weight: bold;">visibility</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">hidden</span><span style="color: #00AA00;">;</span>
     <span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">block</span><span style="color: #00AA00;">;</span>
     <span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
     <span style="color: #000000; font-weight: bold;">line-height</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
     <span style="color: #000000; font-weight: bold;">content</span><span style="color: #00AA00;">:</span> <span style="color: #ff0000;">&quot; &quot;</span><span style="color: #00AA00;">;</span>
     <span style="color: #000000; font-weight: bold;">clear</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">both</span><span style="color: #00AA00;">;</span>
     <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
     <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
     <span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.clearfix</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> inline-<span style="color: #993333;">block</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span>
<span style="color: #808080; font-style: italic;">/* start commented backslash hack \*/</span>
<span style="color: #00AA00;">*</span> html <span style="color: #6666ff;">.clearfix</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;"><span style="color: #cc66cc;">1</span>%</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.clearfix</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">block</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span>
<span style="color: #808080; font-style: italic;">/* close commented backslash hack */</span></pre></div></div>

<p>And then add a class=&#8221;clearfix&#8221; to the parent container in your markup:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;div class=&quot;clearfix&quot; style=&quot;border:1px solid blue;padding:5px;&quot;&gt;
	&lt;div style=&quot;float:right;width:49%;height:50px;background:green;&quot;&gt;
		&amp;nbsp;
	&lt;/div&gt;	
&nbsp;
	&lt;div style=&quot;width:49%;height:20px;background:red;&quot;&gt;
		&amp;nbsp;
	&lt;/div&gt;
&lt;/div&gt;</pre></div></div>

<p><img src="http://www.sycha.com/wp-content/uploads/2010/02/clearfix-floated-element-clearing-problem.png" alt="" title="Clearfix floated element clearing problem" width="408" height="76" class="alignnone size-full wp-image-972" /><br />
Now you&#8217;ll see the green div expands the parent container automatically.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sycha.com/css-clearfix-floated-element-automatically-fill-parent-container/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Magento: Add a custom field to the Contact Us Form</title>
		<link>http://www.sycha.com/magento-add-a-custom-field-to-the-contact-us-form</link>
		<comments>http://www.sycha.com/magento-add-a-custom-field-to-the-contact-us-form#comments</comments>
		<pubDate>Fri, 05 Feb 2010 05:22:19 +0000</pubDate>
		<dc:creator>Charlie Evans</dc:creator>
				<category><![CDATA[Magento Development]]></category>

		<guid isPermaLink="false">http://www.sycha.com/?p=925</guid>
		<description><![CDATA[Magento's default contact form asks the user for the essential contact info, e.g. name, email etc.. But you may need to ask for some additional info by adding some custom fields. Here's how to do it.]]></description>
			<content:encoded><![CDATA[<p>Magento&#8217;s default contact form asks the user for the essential contact info, e.g. name, email etc.. But you may need to ask for some additional info by adding some custom fields. Here&#8217;s how to do it.</p>
<p>Let&#8217;s use the following example scenario:<br />
You want to add a drop box select element to ask the user &#8216;Where did you hear about us?&#8217;. The options will be: Google, Magazine, Word of Mouth, Other.</p>
<h2>Update the Contact Us form template</h2>
<p>Firstly you need to edit the contact form template.<br />
If using the default theme, this code will be located in<br />
<em>/magento/app/design/frontend/base/default/template/contacts/form.phtml</em></p>
<p>Add the following markup to the template in the desired position:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;div class=&quot;input-box&quot;&gt;
    &lt;label for=&quot;where-hear&quot;&gt;&lt;?php echo Mage::helper('contacts')-&gt;__('Where did you hear about us?') ?&gt; &lt;span class=&quot;required&quot;&gt;*&lt;/span&gt;&lt;/label&gt;&lt;br /&gt;
    &lt;select name=&quot;where-hear&quot; id=&quot;where-hear&quot; style=&quot;width:300px&quot; class=&quot;required-entry&quot;&gt;
        &lt;option value=&quot;&quot; selected&gt;Please select...&lt;/option&gt;
        &lt;option value=&quot;Google&quot;&gt;Google&lt;/option&gt;
        &lt;option value=&quot;Magazine&quot;&gt;Magazine&lt;/option&gt;
        &lt;option value=&quot;Word of Mouth&quot;&gt;Word of Mouth&lt;/option&gt;
        &lt;option value=&quot;Other&quot;&gt;Other&lt;/option&gt;
    &lt;/select&gt;
&lt;/div&gt;</pre></div></div>

<p>Note the use of <strong>class=&#8221;required-entry&#8221;</strong> on the select element.. This tells the default javascript validation that this field is required. If the user tries to submit the form without choosing an option, then it will throw an error and highlight the field.</p>
<p>So that takes care of the form, now you need to set up a custom email template that includes the new field.</p>
<h2>Create a custom transactional email template</h2>
<p>Navigate to the following screen in your Magento admin:<br />
<em>System > Transactional Emails</em></p>
<ol>
<li>Click <strong>Add New Template</strong></li>
<li>In the top block (Load default template), select &#8220;Contact Form&#8221; from the drop box and click <strong>Load Template</strong></li>
<li>In the Template Information block, set the Template Name to &#8220;Custom Contact Form&#8221;</li>
<li>In the Template Content field, add the following line where you want it to appear in the email:

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">Where did you hear about us?: {{var data.where-hear}}</pre></div></div>

</li>
</ol>
<p><img src="http://www.sycha.com/wp-content/uploads/2010/02/custom-contact-form-email-template.png" alt="" title="Custom Contact Form Email Template" width="598" height="483" class="border-999 alignnone size-full wp-image-943" /></p>
<h2>Assign the custom email template</h2>
<p>Navigate to the following screen in your Magento admin:<br />
<em>System > Configuration > General > Contacts</em></p>
<p>In the Email Options block, set the Email Template to &#8220;Custom Contact Form&#8221;.</p>
<p><img src="http://www.sycha.com/wp-content/uploads/2010/02/assign-custom-email-template.png" alt="" title="Assign Custom Email Template" width="598" height="293" class="border-999 alignnone size-full wp-image-942" /></p>
<p>Click <strong>Save Config</strong> and you&#8217;re all done.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sycha.com/magento-add-a-custom-field-to-the-contact-us-form/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Magento: Add a custom State / Province / Region to a Country</title>
		<link>http://www.sycha.com/magento-add-custom-state-province-region-country</link>
		<comments>http://www.sycha.com/magento-add-custom-state-province-region-country#comments</comments>
		<pubDate>Sat, 30 Jan 2010 04:36:52 +0000</pubDate>
		<dc:creator>Charlie Evans</dc:creator>
				<category><![CDATA[Magento Development]]></category>

		<guid isPermaLink="false">http://www.sycha.com/?p=788</guid>
		<description><![CDATA[If you need to add a custom State / Province / Region to a Country in Magento, you’ll need to modify the database manually as there currently isn’t a means to do this from within the Magento admin. Fortunately it’s pretty simple.]]></description>
			<content:encoded><![CDATA[<p>If you need to add a custom State / Province / Region to a Country in Magento, you&#8217;ll need to modify the database manually as there currently isn&#8217;t a means to do this from within the Magento admin. Fortunately it&#8217;s pretty simple &#8211; there are 2 tables involved: <strong>directory_country_region</strong> and <strong>directory_country_region_name</strong>. </p>
<p>Adding a record to <strong>directory_country_region</strong> for a specified country_id will make that new entry show up in the State/Province drop box on the address forms. It will also make that a required field, so you need to make sure you add all the possible options.</p>
<p>You then need to add a corresponding record to <strong>directory_country_region_name</strong>, using the region_id generated when you inserted into directory_country_region. This entry will make the new region show up when a whole address is displayed on the screen or email, e.g. in an order summary.</p>
<h2>Example: Add North and South Island to New Zealand</h2>
<p>So let&#8217;s say that like me, you live in New Zealand and want to add 2 regions: North Island and South Island. The country id for New Zealand is <strong>NZ</strong>, the region code is a unique identifier so I&#8217;m going with <strong>NORTH</strong> and at the moment I&#8217;m only interested in the <strong>en_US</strong> locale. </p>
<p>First I will insert North Island into directory_country_region as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> <span style="color: #ff0000;">`directory_country_region`</span> 
<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`region_id`</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">`country_id`</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">`code`</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">`default_name`</span><span style="color: #66cc66;">&#41;</span> 
<span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'NZ'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'NORTH'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'North Island'</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Note the NULL entry for the region_id field which will auto_increment.  I need to find out this new region_id for my next insert so I can either browse the table manually, or this query will do the trick:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #ff0000;">`directory_country_region`</span> 
<span style="color: #993333; font-weight: bold;">WHERE</span> <span style="color: #ff0000;">`country_id`</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'NZ'</span> <span style="color: #993333; font-weight: bold;">AND</span><span style="color: #ff0000;">`code`</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'NORTH'</span> <span style="color: #993333; font-weight: bold;">AND</span> <span style="color: #ff0000;">`default_name`</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'North Island'</span>;</pre></div></div>

<p>In my case, the new region_id is 320, so with that I&#8217;ll now insert into directory_country_region_name as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> <span style="color: #ff0000;">`directory_country_region_name`</span> 
<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`locale`</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">`region_id`</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">`name`</span><span style="color: #66cc66;">&#41;</span> 
<span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'en_US'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'320'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'North Island'</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Now I just repeat those steps for South Island and I&#8217;m all set.</p>
<p>I&#8217;ve created some code below which automates this process, you just need to make sure <a href="/stand-alone-magento-scripts">Magento is properly initialised in your script</a> and modify the variables at the top as necessary.. Enjoy!</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// set up array containing regions to be added (region_code =&gt; region_name)</span>
<span style="color: #000088;">$new_regions</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
	<span style="color: #0000ff;">'NORTH'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;North Island&quot;</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'SOUTH'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;South Island&quot;</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// specify country code for new regions</span>
<span style="color: #000088;">$country_code</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'NZ'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// specify locale</span>
<span style="color: #000088;">$locale</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'en_US'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// create our core_write conection object</span>
<span style="color: #000088;">$connection</span> <span style="color: #339933;">=</span> Mage<span style="color: #339933;">::</span><span style="color: #004000;">getSingleton</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'core/resource'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getConnection</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'core_write'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// iterate our new regions</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$new_regions</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$region_code</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$region_name</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// insert region </span>
	<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;INSERT INTO `directory_country_region` (`region_id`,`country_id`,`code`,`default_name`) VALUES (NULL,?,?,?)&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$connection</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #339933;">,</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$country_code</span><span style="color: #339933;">,</span><span style="color: #000088;">$region_code</span><span style="color: #339933;">,</span><span style="color: #000088;">$region_name</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// get new region id for next query</span>
	<span style="color: #000088;">$region_id</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$connection</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">lastInsertId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// insert region name</span>
	<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;INSERT INTO `directory_country_region_name` (`locale`,`region_id`,`name`) VALUES (?,?,?)&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$connection</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #339933;">,</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$locale</span><span style="color: #339933;">,</span><span style="color: #000088;">$region_id</span><span style="color: #339933;">,</span><span style="color: #000088;">$region_name</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.sycha.com/magento-add-custom-state-province-region-country/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Magento Database Models, Helpers and Raw SQL Queries</title>
		<link>http://www.sycha.com/magento-database-models-helpers-raw-sql-queries</link>
		<comments>http://www.sycha.com/magento-database-models-helpers-raw-sql-queries#comments</comments>
		<pubDate>Fri, 15 Jan 2010 00:44:20 +0000</pubDate>
		<dc:creator>Charlie Evans</dc:creator>
				<category><![CDATA[Magento Development]]></category>

		<guid isPermaLink="false">http://www.sycha.com/?p=768</guid>
		<description><![CDATA[The best way to access and manipulate data in Magento is by using the provided objects and their associated functionality. If you really need to deal with raw queries you can do that too.]]></description>
			<content:encoded><![CDATA[<div class="notice">
<p>Before running any the snippets in this article,<br />
you&#8217;ll need to make sure <a href="/stand-alone-magento-scripts">Magento is initialised in your script</a>
</div>
<h2>Magento Database Models</h2>
<p>The best way to access and manipulate data in Magento is by using the provided objects and their associated functionality..<br />
For example, you can get a Magento category model like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$model_category</span> <span style="color: #339933;">=</span> Mage<span style="color: #339933;">::</span><span style="color: #004000;">getModel</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'catalog/category'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>With your category model you now retrieve data<br />
e.g. get all child categories of category ID 2 and echo the name</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$model_category</span> <span style="color: #339933;">=</span> Mage<span style="color: #339933;">::</span><span style="color: #004000;">getModel</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'catalog/category'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$model_category</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getCategories</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$category</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$category</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h2>Magento Helper Classes</h2>
<p>Magento has helper classes which are pretty handy also.<br />
For example, you can get a Magento category helper like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$helper</span> <span style="color: #339933;">=</span> Mage<span style="color: #339933;">::</span><span style="color: #004000;">helper</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'catalog/category'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Your helper will have a bunch of methods defined for performing common tasks.<br />
For example, you can get all your stores top level categories as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$helper</span> <span style="color: #339933;">=</span> Mage<span style="color: #339933;">::</span><span style="color: #004000;">helper</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'catalog/category'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$helper</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getStoreCategories</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$category</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$category</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Check this file to see what methods are available in the catalog category helper:<br />
magento/app/code/core/Mage/Catalog/Helper/Category.php</p>
<h2>Raw SQL Queries in Magento</h2>
<p>That&#8217;s a VERY brief intro into using Magento&#8217;s models and helpers. They should serve your needs most of the time, but if you really need to deal with raw queries here&#8217;s how to do it.</p>
<p>Depending on what sort of operation you wish to perform (read or write) you will use one of the following to get a database connection object:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$connection</span> <span style="color: #339933;">=</span> Mage<span style="color: #339933;">::</span><span style="color: #004000;">getSingleton</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'core/resource'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getConnection</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'core_read'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$connection</span> <span style="color: #339933;">=</span> Mage<span style="color: #339933;">::</span><span style="color: #004000;">getSingleton</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'core/resource'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getConnection</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'core_write'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>These connections are essentially Zend database objects so you can run any of the <a href="http://framework.zend.com/apidoc/core/Zend_Db/Adapter/Zend_Db_Adapter_Abstract.html" target="_blank" rel="nofollow">methods defined here</a>. Probably of most interest will be query(), fetchAll(), and fetchOne().</p>
<h2>Write directly to a Magento database table</h2>
<p>This example creates a core_write connection object, then directly inserts a row with raw SQL via the query() method.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// insert a new US State into the region table</span>
<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;INSERT INTO `directory_country_region` (`region_id`,`country_id`,`code`,`default_name`) VALUES (NULL,'US','XX','New US State')&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$connection</span> <span style="color: #339933;">=</span> Mage<span style="color: #339933;">::</span><span style="color: #004000;">getSingleton</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'core/resource'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getConnection</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'core_write'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$connection</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h2>Read directly from a Magento database table</h2>
<p>This example creates a core_read connection object, then uses the fetchAll() method to retrieve and iterate all the rows returned by a raw sql query.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// display all regions defined for the United States</span>
<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT * FROM directory_country_region WHERE country_id='US'&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$connection</span> <span style="color: #339933;">=</span> Mage<span style="color: #339933;">::</span><span style="color: #004000;">getSingleton</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'core/resource'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getConnection</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'core_read'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$connection</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetchAll</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$arr_row</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$arr_row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'default_name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.sycha.com/magento-database-models-helpers-raw-sql-queries/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Stand alone Magento scripts</title>
		<link>http://www.sycha.com/stand-alone-magento-scripts</link>
		<comments>http://www.sycha.com/stand-alone-magento-scripts#comments</comments>
		<pubDate>Tue, 29 Dec 2009 00:34:28 +0000</pubDate>
		<dc:creator>Charlie Evans</dc:creator>
				<category><![CDATA[Magento Development]]></category>

		<guid isPermaLink="false">http://www.sycha.com/?p=761</guid>
		<description><![CDATA[Before running any stand alone scripts that use core Magento objects and functionality, you need to make sure Magento is properly initialised. Add this snippet to the top of your script, updating the path according to your installation.]]></description>
			<content:encoded><![CDATA[<p>Before running any stand alone scripts that use core Magento objects and functionality, you need to make sure Magento is properly initialised. </p>
<p>Add this snippet to the top of your script, updating the path accordingly.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> 
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'PATH_TO_MAGENTO'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'/var/www/magento'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// update accordingly</span>
<span style="color: #b1b100;">require_once</span> PATH_TO_MAGENTO<span style="color: #339933;">.</span><span style="color: #0000ff;">'/app/Mage.php'</span><span style="color: #339933;">;</span>
Mage<span style="color: #339933;">::</span><span style="color: #004000;">app</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setCurrentStore</span><span style="color: #009900;">&#40;</span>Mage_Core_Model_App<span style="color: #339933;">::</span><span style="color: #004000;">ADMIN_STORE_ID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
<span style="color: #666666; font-style: italic;">// your php code here</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.sycha.com/stand-alone-magento-scripts/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WordPress Snippet: Display multiple loops on the same page</title>
		<link>http://www.sycha.com/wordpress-snippet-multiple-loops-same-page</link>
		<comments>http://www.sycha.com/wordpress-snippet-multiple-loops-same-page#comments</comments>
		<pubDate>Fri, 11 Dec 2009 22:07:00 +0000</pubDate>
		<dc:creator>Charlie Evans</dc:creator>
				<category><![CDATA[WordPress Development]]></category>

		<guid isPermaLink="false">http://www.sycha.com/?p=503</guid>
		<description><![CDATA[Let's say that on a category page of your WordPress blog, you want to display a secondary list of posts, for example the 3 latest posts from a "featured" category. You have a couple of good options here.]]></description>
			<content:encoded><![CDATA[<p>Let&#8217;s say that on a category page of your WordPress blog, you want to display a secondary list of posts, for example the 3 latest posts from a &#8220;featured&#8221; category.</p>
<p>You have a couple of good options here:<br />
1. create a new WP_Query object, or<br />
2. use the get_posts() function.</p>
<p><em>Note: Using query_posts() is a bad option as it will upset your &#8220;main loop&#8221;</em></p>
<p>Both take pretty much the same arguments as query_posts() &#8211; <a href="http://codex.wordpress.org/Template_Tags/query_posts#Parameters" target="_blank" rel="nofollow">see a list here.</a></p>
<h3>Example usage of WP_Query:</h3>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$featured_category_id</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$args</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'posts_per_page'</span><span style="color: #339933;">=&gt;</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'cat'</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$featured_category_id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> WP_Query<span style="color: #009900;">&#40;</span><span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">have_posts</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$query</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">the_post</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	the_title<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'&lt;h3&gt;'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&lt;/h3&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">endwhile</span><span style="color: #339933;">;</span></pre></div></div>

<h3>Example usage of get_posts():</h3>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$featured_category_id</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$args</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'numberposts'</span><span style="color: #339933;">=&gt;</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'cat'</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$featured_category_id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span>get_posts<span style="color: #009900;">&#40;</span><span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$post</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span>
	the_title<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'&lt;h3&gt;'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&lt;/h3&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">endforeach</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.sycha.com/wordpress-snippet-multiple-loops-same-page/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Javascript &amp; jQuery Cheat Sheet</title>
		<link>http://www.sycha.com/javascript-jquery-cheat-sheet</link>
		<comments>http://www.sycha.com/javascript-jquery-cheat-sheet#comments</comments>
		<pubDate>Sat, 05 Dec 2009 08:12:00 +0000</pubDate>
		<dc:creator>Charlie Evans</dc:creator>
				<category><![CDATA[jQuery / Javascript Resources]]></category>

		<guid isPermaLink="false">http://www.sycha.com/?p=197</guid>
		<description><![CDATA[Even the smartest cookies forget things from time to time.. Here's a collection of Javascript &#038; jQuery code snippets for your reference.]]></description>
			<content:encoded><![CDATA[<p>Even the smartest cookies forget things from time to time..<br />
Here&#8217;s a collection of Javascript &amp; jQuery code snippets for your reference. Enjoy <img src='http://www.sycha.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h3>Run jQuery code when page has finished loading (DOM ready)</h3>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">// code here</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// jQuery shortcut to DOM ready</span>
$<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">// code here</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h3>Run jQuery in &#8220;no conflict&#8221; mode</h3>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">jQuery.<span style="color: #660066;">noConflict</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// run code when DOM ready using $ shortcut</span>
jQuery<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">// code here</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h3>Get selected text from select element</h3>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#ID :selected&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<h3>Get multiple selected text from select element into array</h3>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> arr_text <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#ID :selected&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">,</span> selected<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	arr_text<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span>selected<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h3>Bind a click event</h3>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#ID&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;click&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Clickity Click&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.sycha.com/javascript-jquery-cheat-sheet/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WordPress Function: in_category_extended()</title>
		<link>http://www.sycha.com/wordpress-function-in-category-extended</link>
		<comments>http://www.sycha.com/wordpress-function-in-category-extended#comments</comments>
		<pubDate>Sun, 29 Nov 2009 22:49:50 +0000</pubDate>
		<dc:creator>Charlie Evans</dc:creator>
				<category><![CDATA[WordPress Development]]></category>

		<guid isPermaLink="false">http://www.sycha.com/?p=175</guid>
		<description><![CDATA[The core WordPress function in_category() is pretty handy but it’s also pretty basic. in_category_extended() is designed to extend the functionality of in_category() by examining descendant categories aswell as the current one.]]></description>
			<content:encoded><![CDATA[<p>The core WordPress function <code>in_category()</code> is pretty handy but it&#8217;s also pretty basic. </p>
<p><code><strong>in_category_extended()</strong></code> is designed to extend the functionality of <code>in_category()</code> in the following ways:</p>
<ol>
<li>test if the current post is assigned to the specified category <strong>OR</strong> any descendant categories</li>
<li>return <strong>TRUE</strong> if you are viewing the specified category page <strong>OR</strong> any descendant category pages</li>
</ol>
<p>Sure, you can do this a bunch of different ways, using multiple functions and code snippets, but I want <strong>a single boolean PHP function</strong> to use in my templates. </p>
<h3>Example Usage</h3>
<p>The main navigation consists of category tabs with on/off states.<br />
If I&#8217;m viewing a post OR a category page which is related to the &#8220;development&#8221; category, then I want to apply a <code>class="current"</code> to the development tab element, which I can then style accordingly with CSS.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">class=&quot;<span style="color: #000000; font-weight: bold;">&lt;?=</span> in_category_extended<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'development'</span><span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">'current'</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">''</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;</pre></div></div>

<h3>Function Definition</h3>
<p><code><strong>bool in_category_extended($category)</strong></code></p>
<p>returns TRUE if the current webpage is related to the specified category</p>
<h3>Function Parameters</h3>
<p><code>$category</code><br />
<em>(mixed) (required)</em> Category specified by ID (integer), or slug (string)</p>
<h3>Function Code</h3>
<p>This function has come in very handy for me &#8211; I hope you find it useful too!</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> in_category_extended<span style="color: #009900;">&#40;</span><span style="color: #000088;">$category</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// easy out</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #009900;">&#40;</span>is_category<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> or is_single<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// retrieve specified category (ID or slug)</span>
	<span style="color: #000088;">$obj_specified_category</span> <span style="color: #339933;">=</span> <span style="color: #990000;">is_numeric</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$category</span><span style="color: #009900;">&#41;</span> ? get_category<span style="color: #009900;">&#40;</span><span style="color: #000088;">$category</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> get_category_by_slug<span style="color: #009900;">&#40;</span><span style="color: #000088;">$category</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$obj_specified_category</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cat_ID</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// we're viewing a category</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>is_category<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$current_category_ID</span> <span style="color: #339933;">=</span> get_query_var<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'cat'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$obj_specified_category</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cat_ID</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$current_category_ID</span> or cat_is_ancestor_of<span style="color: #009900;">&#40;</span><span style="color: #000088;">$obj_specified_category</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cat_ID</span><span style="color: #339933;">,</span><span style="color: #000088;">$current_category_ID</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// we're viewing a post</span>
	<span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$wp_query</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$obj_post</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$wp_query</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_queried_object</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$obj_post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ID</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span> 
&nbsp;
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>in_category<span style="color: #009900;">&#40;</span><span style="color: #000088;">$obj_specified_category</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cat_ID</span><span style="color: #339933;">,</span> <span style="color: #000088;">$obj_post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ID</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">return</span> in_category<span style="color: #009900;">&#40;</span>get_term_children<span style="color: #009900;">&#40;</span><span style="color: #000088;">$obj_specified_category</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cat_ID</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'category'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$obj_post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.sycha.com/wordpress-function-in-category-extended/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
