<?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>James Van Dyne &#187; Wordpress</title>
	<atom:link href="http://www.james-vandyne.com/category/wordpress/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.james-vandyne.com</link>
	<description>The Life and Times</description>
	<lastBuildDate>Sat, 08 Oct 2011 16:11:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Detect Language in WordPress in Multi-installs</title>
		<link>http://www.james-vandyne.com/detect-language-in-wordpress-in-multi-installs/</link>
		<comments>http://www.james-vandyne.com/detect-language-in-wordpress-in-multi-installs/#comments</comments>
		<pubDate>Sat, 14 Aug 2010 03:23:17 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Wordpress]]></category>

		<guid isPermaLink="false">http://www.james-vandyne.com/?p=120</guid>
		<description><![CDATA[Problem When using wordpress our theme (or some other component) might want to check a wordpress defined variable. In a regular install of WordPress (single site), you can access them directly because they are defined in wp-config.php. //wp-config.php define(&#8216;WPLANG&#8217;,'ja&#8217;); //somewhere &#8230; <a href="http://www.james-vandyne.com/detect-language-in-wordpress-in-multi-installs/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<h2>Problem</h2>

<p>When using wordpress our theme (or some other component) might want to check a wordpress defined variable. In a regular install of WordPress (single site), you can access them directly because they are defined in wp-config.php.</p>

<div class="code">
//wp-config.php
define(&#8216;WPLANG&#8217;,'ja&#8217;); 

//somewhere in my theme
echo WPLANG;
</div>

<p>We can access this on only the base site of our multi-site install. However, if we use it, we would have to litter our theme with inconsistent access methods, or worse, duplicate code depending on our site.</p>

<p>How can we access this in our multi-site install?</p>

<h2>Solution</h2>

<p>You can define variables in wordpress via the web interface. Strangely, these aren&#8217;t run through define(), hence the above doesn&#8217;t work. I end up with something like the following:</p>

<div class="code">
//base site
echo &#8220;Language: &#8221; .WPLANG;
// Language: en-US
// secondary site defined as ja in the settings..
echo &#8220;Language: &#8221; .WPLANG
// Language: 
</div>

<p>If you&#8217;re checking the language, nothing appears. As luck would have it, you can simple use the get_bloginfo() method to pull this same information.</p>

<div class="code">
if(strcmp(get_bloginfo(&#8216;language&#8217;),&#8217;ja&#8217;) == 0)
    echo &#8220;日本語&#8221;;
</div>

<h2>Conclusion</h2>

<p>This might not map for all variables that you can define for wordpress in wp-config.php. Moreover, the inconsistency of naming is rather frustrating. What is defined as WPLANG is accessed via get_bloginfo(&#8216;language&#8217;); Sometimes words are abbreviated and other times they aren&#8217;t.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.james-vandyne.com/detect-language-in-wordpress-in-multi-installs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Process Paypal IPN Requests Through WordPress</title>
		<link>http://www.james-vandyne.com/process-paypal-ipn-requests-through-wordpress/</link>
		<comments>http://www.james-vandyne.com/process-paypal-ipn-requests-through-wordpress/#comments</comments>
		<pubDate>Sat, 15 Aug 2009 15:38:41 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Paypal]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Wordpress]]></category>

		<guid isPermaLink="false">http://www.james-vandyne.com/?p=36</guid>
		<description><![CDATA[Introduction Paypal is perhaps the easiest way to send/receive money online and WordPress is perhaps the most popular blogging platform out there. Wouldn&#8217;t it be great if there was a way we could integrate them together? There are currently a &#8230; <a href="http://www.james-vandyne.com/process-paypal-ipn-requests-through-wordpress/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<h3>Introduction</h3>

<p>Paypal is perhaps the easiest way to send/receive money online and WordPress is perhaps the most popular blogging platform out there. Wouldn&#8217;t it be great if there was a way we could integrate them together?</p>

<p>There are currently a number of plugins available that allow you use WordPress with paypal for invoicing customers and such. However if you want to do anything custom you are going to need to process IPNs (Instant Payment Notifications) yourself.</p>

<h3>Problem</h3>

<p>One could easily just put a php file on their server to process the IPN independently of WordPress. However if you want to interact with your database at all you have three basic options:</p>

<ol>
<li>Hardcode your database settings in your IPN processor.  </li>
<li><em>include</em> your wp-config.php and pull the database settings from that manually.</li>
<li>Integrate with WordPress so you can use the <em>$wpdb</em> object in your plugin ( I assume that you are developing your own larger plugin and paypal support is part of that).</li>
</ol>

<p>Hardcoding your settings is fine for one off scripts, but if you change servers you could easily forget to update your settings breaking your website or worse: stopping people from giving you money!</p>

<p>Including your wp-config.php has a number of drawbacks. It makes certain assumptions that break your software. Namely it assumes that wp-config.php is a directory or two up in <em>../../wp-config.php</em> or something of that nature. As WordPress allows you to have you config file in other locations e.g. not ../../wp-config.php this is very risky business.</p>

<p>Integrating with WordPress allows the user to use the plugin without any extra configuration to access their database. If WordPress can access the database so can we. We also get access to some wordpress convenience methods &#8211; always a plus.</p>

<p>To be able to access the $wpdb object requires WordPress initialize and for the IPN somehow call our plugin. Obviously telling Paypal to send its requests to <em>/wp-content/plugins/my_plugin/ipn_listener.php</em> is a bad idea ( and I don&#8217;t think it would even work).</p>

<h3>Solution</h3>

<p>Let&#8217;s ponder for a moment <em>how</em> WordPress processes our requests. 
Usually we see the exact details of the request and they look pretty thanks to mod_rewrite. A typical request would look something like</p>


<div class="wp_syntax"><div class="code"><pre class="url" style="font-family:monospace;">http://example.com?/2009/08/01/delicious-cheese</pre></div></div>


<p>Or as WordPress sees it:</p>


<div class="wp_syntax"><div class="code"><pre class="url" style="font-family:monospace;">http://example.com?year=2009&amp;month=08&amp;day=01&amp;name=delicious-cheese.</pre></div></div>


<p>Understanding this, my solution was to build upon this and direct the IPN to index.php with some extra variables set.</p>


<div class="wp_syntax"><div class="code"><pre class="url" style="font-family:monospace;"> http://www.example.com?my_plugin=paypal.</pre></div></div>


<p>Now that we&#8217;ve got that far. Next we need to tell WordPress to be on the look out and accept more variable names from the request. We do this by adding a filter to query_vars. We have to do this because WordPress ignores them if we don&#8217;t in an effort to prevent processing unknown and possibly malicious inputs.<cite>1</cite></p>


<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> my_plugin_query_vars<span style="color: #009900;">&#40;</span><span style="color: #000088;">$vars</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// add my_plugin to the valid list of variables</span>
	<span style="color: #000088;">$new_vars</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'my_plugin'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$vars</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$new_vars</span> <span style="color: #339933;">+</span> vars<span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$vars</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
add_filter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'query_vars'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'my_plugin_query_vars'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>


<p>Now that WordPress accepts GET variables with the name my_plugin it&#8217;s time to do something with that. We add an action on parse_request which gives us first change to parse given request before WordPress does.</p>

<p>Notice that we check to make sure that our specified variable exists and is the value we are expecting. If we do not do this we end up processing all requests. <cite>2</cite> Once we have confirmed that both our GET variable and is the name we expect we call the function in our plugin that processes the IPN.</p>


<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> muy_plugin_parse_request<span style="color: #009900;">&#40;</span><span style="color: #000088;">$wp</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// only process requests with &quot;my_plugin=paypal&quot;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">array_key_exists</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'my_plugin'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$wp</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query_vars</span><span style="color: #009900;">&#41;</span> 
            <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$wp</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query_vars</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'my_plugin'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'paypal'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        my_plugin_proccess_paypal_ipn<span style="color: #009900;">&#40;</span><span style="color: #000088;">$wp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
add_action<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'parse_request'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'my_plugin_parse_request'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>


<p>As per processing the IPN you can start by simply copy/pasting the Paypal sample code in the function. You then access your database using the $wpdb object. Don&#8217;t forget that it&#8217;s global so you must first</p>


<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$wpdb</span><span style="color: #339933;">;</span></pre></div></div>


<p>before you can access it.</p>

<h3>Update</h3>

<p>I thought we were finished but we aren&#8217;t. The above works fine with Paypal&#8217;s Tester IPN as it doesn&#8217;t check URLs. Paypal does not allow you to active your IPN in Paypal if a given URL has GET arguments.</p>

<p>We can fix this with just a little more code and the magic of mod_rewrite. First you will need to enable mod_rewrite in WordPress (Options -> Permalinks) by selecting one that is non-default e.g. Day and name.</p>

<p>Then we need to add a hook in WordPress rewrite system and then create a rule that tell WordPress to forward all requests to</p>


<div class="wp_syntax"><div class="code"><pre class="url" style="font-family:monospace;">http://example.com/my_plugin/paypal</pre></div></div>


<p>to</p>


<div class="wp_syntax"><div class="code"><pre class="url" style="font-family:monospace;">http://example.com/?my_plugin=paypal</pre></div></div>


<p>The code to do this is as follows:</p>


<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">add_action<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'generate_rewrite_rules'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'my_plugin_rewrite_rules'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> my_plugin_rewrite_rules<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$wp_rewrite</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$new_rules</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'my_plugin/paypal'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'index.php?my_plugin=paypal'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$wp_rewrite</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">rules</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$new_rules</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$wp_rewrite</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">rules</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>


<h3>Notes</h3>

<p><cite>1</cite> If you wish to access the paypal and other variables (both GET and POST) via $wp->query_vars you must declare them in your query_vars filter as well. Since there are loads of them with paypal this may or may not be worth the effort.</p>

<p><cite>2</cite> It might not hurt to make the value a bit more obscure e.g. ei190A0F (random gibberish). Another side bonus is you could also add support for other services quite easily.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.james-vandyne.com/process-paypal-ipn-requests-through-wordpress/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

