<?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>Young Dutch Design &#187; mod_rewrite</title>
	<atom:link href="http://youngdutchdesign.com/tag/mod_rewrite/feed" rel="self" type="application/rss+xml" />
	<link>http://youngdutchdesign.com</link>
	<description>Young Dutch Design is a new platform for young designers with frequently new added postings about productdesign, webdesign and other related subjects.</description>
	<lastBuildDate>Tue, 14 Jun 2011 20:25:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Rewrite $_GET variables for WordPress plugins</title>
		<link>http://youngdutchdesign.com/rewrite-multiple-get-variables-for-wordpress-plugins</link>
		<comments>http://youngdutchdesign.com/rewrite-multiple-get-variables-for-wordpress-plugins#comments</comments>
		<pubDate>Wed, 18 Nov 2009 16:29:23 +0000</pubDate>
		<dc:creator>Crispijn</dc:creator>
				<category><![CDATA[WordPress]]></category>
		<category><![CDATA[mod_rewrite]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://youngdutchdesign.com/?p=1566</guid>
		<description><![CDATA[A while ago I was figuring out how to rewrite GET variables for a wordpress plugin. For example: http://mysite.com/mypage/?myvar=test rewriting to http://mysite.com/mypage/test After a long search on the WordPress forums and the whole internet I&#8217;ve found this topic: using an extra parameter in an URL but it wasn&#8217;t very sufficient in my opinion. So that&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignright size-full wp-image-1570" title="get" src="http://youngdutchdesign.com/wp-content/uploads/2009/08/get1.jpg" alt="get" width="200" height="100" />A while ago I was figuring out how to rewrite GET variables for a wordpress plugin. For example:</p>
<p style="padding-left: 30px;">http://mysite.com/mypage/?myvar=test</p>
<p>rewriting to</p>
<p style="padding-left: 30px;">http://mysite.com/mypage/test</p>
<p>After a long search on the WordPress forums and the whole internet I&#8217;ve found this topic: <a href="http://wordpress.org/support/topic/208830" target="_blank">using an extra parameter in an URL</a> but it wasn&#8217;t very sufficient in my opinion. So that&#8217;s why I&#8217;ll explain the use of multiple parameters in the url.<br />
<span id="more-1566"></span></p>
<h2>One $_GET variable</h2>
<pre class="php">function add_mypage_var($public_query_vars) {
$public_query_vars[] = 'myvar1';
return $public_query_vars;
}

//add a rewrite rule
function do_rewrite_mypage() {
add_rewrite_rule('brands/([^/]+)/?$', 'index.php?pagename=mypage&amp;myvar1=$matches[1]','top');
}

add_filter('query_vars', 'add_mypage_var');
add_action('init', 'do_rewrite_brands');</pre>
<p>You can use this variable in a plugin like this:</p>
<pre class="php">$slug = get_query_var('myvar');</pre>
<h2>Multiple $_GET variables</h2>
<p>There was just one thing that cost me a headache&#8230; I&#8217;d like to add multiple parameters to the url and this method I just described isn&#8217;t sufficient. I&#8217;ve checked the basic url rewrite trough mod_rewrite and this method needs several lines to declare all the possible amount of variables in the url. I&#8217;ve tried this method also for the url rewrites via WordPress and here we are: a simple working function:</p>
<pre class="php">function add_mypage_var($public_query_vars) {
	$public_query_vars[] = 'myvar1';
	$public_query_vars[] = 'myvar2';

	return $public_query_vars;
}

//add a rewrite rule
function do_rewrite_mypage() {
	add_rewrite_rule('brands/([^/]+)/?$', 'index.php?pagename=mypage&amp;myvar1=$matches[1]&amp;myvar2=$matches[2]','top');
	add_rewrite_rule('brands/([^/]+)/?$', 'index.php?pagename=mypage&amp;myvar1=$matches[1]','top');
}

add_filter('query_vars', 'add_mypage_var');
add_action('init', 'do_rewrite_mypage');</pre>
<p>You can check all the url rewrites that are used for your WordPress post.</p>
<pre class="php">function get_rewrite_urls(){
	global $wp_rewrite;

	return $wp_rewrite-&gt;wp_rewrite_rules(); /* Returns an array */
}

print_r(get_rewrite_urls());</pre>
<h2>flush_rules</h2>
<p>You need to flush your rules to install your plugin and make the rewrite work. Also it can be handy to flush the rules during your development phase. So, just detect if the plugin is going to be installed:</p>
<pre class="php">register_activation_hook(__FILE__,'do_flush_gear'); /* Place this at the bottom of your plugin file */
</pre>
<p>And in some cases you&#8217;d like to remove all your custom rewrites. Just use this function:</p>
<pre class="php">function do_flush_gear{
	global $wp_rewrite;

	$wp_rewrite-&gt;flush_rules();
}</pre>
]]></content:encoded>
			<wfw:commentRss>http://youngdutchdesign.com/rewrite-multiple-get-variables-for-wordpress-plugins/feed</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
	</channel>
</rss>
