<?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>Flav36rs &#187; function</title>
	<atom:link href="http://flav36rs.com/tag/function/feed/" rel="self" type="application/rss+xml" />
	<link>http://flav36rs.com</link>
	<description>Just another technical blog</description>
	<lastBuildDate>Mon, 12 Sep 2011 16:56:36 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>WP dbDelta function cannot modify unique keys</title>
		<link>http://flav36rs.com/2010/04/02/wp-dbdelta-function-cannot-modify-unique-keys/</link>
		<comments>http://flav36rs.com/2010/04/02/wp-dbdelta-function-cannot-modify-unique-keys/#comments</comments>
		<pubDate>Fri, 02 Apr 2010 21:12:39 +0000</pubDate>
		<dc:creator>Steve</dc:creator>
				<category><![CDATA[WordPress]]></category>
		<category><![CDATA[dbdelta]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[query]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://36flavours.com/?p=685</guid>
		<description><![CDATA[Whilst making some updates to one of my WordPress plugins earlier today, I discovered an small issue when trying to make use of the dbDelta function. Although it&#8217;s advised you avoid creating tables unless absolutely necessary, it seemed like the &#8230; <a href="http://flav36rs.com/2010/04/02/wp-dbdelta-function-cannot-modify-unique-keys/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Whilst making some updates to one of my WordPress plugins earlier today, I discovered an small issue when trying to make use of the dbDelta function.</p>
<p>Although it&#8217;s advised you avoid creating tables unless absolutely necessary, it seemed like the correct method to store the data used by this particular plugin.<span id="more-685"></span></p>
<p>This method of storage does have it&#8217;s downsides, especially when it comes to modifying the table structure. Luckily the dbDelta function comes in really handy in aiding the update process.</p>
<p>There is however one issue that I came across, discovering that is unable to modify a unique key (in this situation a composite index).</p>
<p>After browsing through the source code for the dbDelta function I spotted that something was missing. When including an index in the query, it is not dropped before attempting to recreate it and is therefore ignored when performing an update.</p>
<p>Take for example the following code snippet:</p>
<pre lang="php" escaped="true">global $wpdb;
$table = $wpdb-prefix.'mytable';
$sql = "CREATE TABLE $table (
	id mediumint(9) NOT NULL AUTO_INCREMENT,
	calendar varchar(255) NOT NULL default 'default',
	year smallint(5) NOT NULL,
	month tinyint(3) NOT NULL,
	day tinyint(3) NOT NULL,
	PRIMARY KEY (id),
	UNIQUE KEY date (year,month,day)
);";
require_once(ABSPATH.'wp-admin/includes/upgrade.php');
dbDelta($sql);</pre>
<p>If we make some changes to the SQL statement to add an additional column to the unique index, the index would become something like &#8220;<em>UNIQUE KEY date (year,month,day,calendar)</em>&#8220;.</p>
<p>This will work if the the table does not already exist, but if it attempts to update the table then the changes to the unique key are ignored.</p>
<p>The short and simple solution is to simply check the table exists and then drop the index manually before calling the dbDelta function.</p>
<pre lang="php" escaped="true">if ($wpdb-&gt;get_var("SHOW TABLES LIKE $table") == $table) {
	$wpdb-&gt;query("ALTER TABLE $table DROP INDEX date");
}</pre>
<p>I&#8217;m not sure if this is actually a bug with the function or whether it wasn&#8217;t supposed to allow for updates on indexes in the first place, either way the problem can be quite easily resolved.</p>
]]></content:encoded>
			<wfw:commentRss>http://flav36rs.com/2010/04/02/wp-dbdelta-function-cannot-modify-unique-keys/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WP &#8211; Add class name to first paragraph in blog post</title>
		<link>http://flav36rs.com/2009/08/20/wp-add-class-name-to-first-paragraph-in-blog-post/</link>
		<comments>http://flav36rs.com/2009/08/20/wp-add-class-name-to-first-paragraph-in-blog-post/#comments</comments>
		<pubDate>Thu, 20 Aug 2009 15:04:27 +0000</pubDate>
		<dc:creator>Steve</dc:creator>
				<category><![CDATA[Blogging]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[formatting]]></category>
		<category><![CDATA[function]]></category>

		<guid isPermaLink="false">http://36flavours.com/?p=273</guid>
		<description><![CDATA[When re-designing my blog I wanted to display the first paragraph of each blog post in bold text. A CSS3 selector could have been used, but wouldn&#8217;t work in older browsers such as IE6. Instead I decided to write a &#8230; <a href="http://flav36rs.com/2009/08/20/wp-add-class-name-to-first-paragraph-in-blog-post/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>When re-designing my blog I wanted to display the <strong>first paragraph</strong> of each blog post in <strong>bold text</strong>. A CSS3 selector could have been used, but wouldn&#8217;t work in older browsers such as <strong>IE6</strong>.</p>
<p>Instead I decided to write a very simple function to add a class name to the first paragraph by filtering the content, then setting the styles accordingly in my theme CSS file.<span id="more-273"></span></p>
<pre lang="php" escaped="true">function first_paragraph($content){
	return preg_replace('/&lt;p([^&gt;]+)?&gt;/', '&lt;p$1 class="first"&gt;', $content, 1);
}
add_filter('the_content', 'first_paragraph');</pre>
<p>By default this will append <em>class=&#8221;first&#8221;</em> to the first p tag in your post.</p>
<p>If you want to use this function on your own blog, just add it to your (or create a) <strong> functions.php</strong> file within your themes folder.</p>
]]></content:encoded>
			<wfw:commentRss>http://flav36rs.com/2009/08/20/wp-add-class-name-to-first-paragraph-in-blog-post/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Simple jQuery string padding function</title>
		<link>http://flav36rs.com/2009/02/27/simple-jquery-string-padding-function/</link>
		<comments>http://flav36rs.com/2009/02/27/simple-jquery-string-padding-function/#comments</comments>
		<pubDate>Fri, 27 Feb 2009 12:27:57 +0000</pubDate>
		<dc:creator>Steve</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[str_pad]]></category>

		<guid isPermaLink="false">http://sha.re.it/?p=142</guid>
		<description><![CDATA[I&#8217;ve written a very simple jQuery function to return a string padded to a specified length, similar to the php equivalent str_pad. $.strPad = function(i,l,s) { var o = i.toString(); if (!s) { s = '0'; } while (o.length < &#8230; <a href="http://flav36rs.com/2009/02/27/simple-jquery-string-padding-function/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve written a very simple <a href="http://jquery.com/" target="_blank">jQuery</a> function to return a string padded to a specified length, similar to the php equivalent <a href="http://uk2.php.net/manual/en/function.str-pad.php" target="_blank">str_pad</a>.</p>
<pre lang="javascript" line="1">$.strPad = function(i,l,s) {
	var o = i.toString();
	if (!s) { s = '0'; }
	while (o.length < l) {
		o = s + o;
	}
	return o;
};</pre>
<p>Example Usage:</p>
</pre>
<pre lang="javascript">$.strPad(12, 5); // returns 00012
$.strPad('abc', 6, '#'); // returns ###abc</pre>
<p>This version only supports left padding, which is why it is labelled as only a simple version <img src='http://flav36rs.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
]]></content:encoded>
			<wfw:commentRss>http://flav36rs.com/2009/02/27/simple-jquery-string-padding-function/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

