<?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>EVOL.reverse &#187; PHP</title>
	<atom:link href="http://www.morgadinho.org/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.morgadinho.org</link>
	<description>Let Yourself Be Conducted By It</description>
	<lastBuildDate>Mon, 26 Jul 2010 14:56:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>WPMU posts return 404 not found</title>
		<link>http://www.morgadinho.org/2010/01/12/wpmu-posts-return-404-not-found/</link>
		<comments>http://www.morgadinho.org/2010/01/12/wpmu-posts-return-404-not-found/#comments</comments>
		<pubDate>Tue, 12 Jan 2010 16:53:38 +0000</pubDate>
		<dc:creator>Nuno Morgadinho</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Wordpress]]></category>

		<guid isPermaLink="false">http://www.morgadinho.org/?p=2234</guid>
		<description><![CDATA[I&#8217;ve installed WordPress MU on a fresh Apache httpd and I go to try it out. I start by clicking on the &#8216;hello world&#8217; post that comes as default and the page returns a 404 &#8220;not found&#8221; message. What is happening? This keeps happening to me all the time so I might as well share [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve installed WordPress MU on a fresh Apache httpd and I go to try it out. I start by clicking on the &#8216;hello world&#8217; post that comes as default and the page returns a 404<span> &#8220;not found&#8221; message. What is happening? This keeps happening to me all the time so I might as well share it here for others.<br />
</span></p>
<p>This can be fixed this by setting &#8220;AllowOverride to All&#8221; in the httpd.conf file, in the relevant Directory directive section where MU is installed.</p>]]></content:encoded>
			<wfw:commentRss>http://www.morgadinho.org/2010/01/12/wpmu-posts-return-404-not-found/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Authentication by POST/GET in WordPress</title>
		<link>http://www.morgadinho.org/2009/11/25/authentication-by-postget-in-wordpress/</link>
		<comments>http://www.morgadinho.org/2009/11/25/authentication-by-postget-in-wordpress/#comments</comments>
		<pubDate>Wed, 25 Nov 2009 18:07:10 +0000</pubDate>
		<dc:creator>Nuno Morgadinho</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Wordpress]]></category>

		<guid isPermaLink="false">http://evolreverse.wordpress.com/?p=2053</guid>
		<description><![CDATA[For a project I&#8217;m working on I needed to trigger an action in a remote WordPress installation. I had a bit of trouble understanding how I could authenticate myself against the remote site. Eventually I found a clean and nice way and I want to share it here with you. Of course, if you&#8217;re not [...]]]></description>
			<content:encoded><![CDATA[<p>For a project I&#8217;m working on I needed to trigger an action in a remote WordPress installation. I had a bit of trouble understanding how I could authenticate myself against the remote site. Eventually I found a clean and nice way and I want to share it here with you. Of course, if you&#8217;re not into WordPress you can safely skip this post.</p>
<p><strong>The Problem</strong></p>
<p>Where I first stumbled was when from my plugin I tried to access directly the other plugin in the remote site. The code I was using for doing this was something like:</p>
<p><code><br />
	// create curl resource<br />
	$ch = curl_init();<br />
	// set url<br />
	curl_setopt($ch, CURLOPT_URL, "http://wordpress.mu/wp-admin/admin.php?page=myplugin");<br />
	//return the transfer as a string<br />
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);<br />
	// $output contains the output string<br />
	$output = curl_exec($ch);<br />
	// close curl resource to free up system resources<br />
	curl_close($ch);<br />
	// print the result of the whole request:<br />
	print "CONTENT = ".$output;<br />
</code></p>
<p>And this wouldn&#8217;t work because although I was authenticated in the remote site via the browser, the session that the curl request creates is not, and so this request results in us being redirected to the login page.</p>
<p><strong>What to do?</strong></p>
<p>Ideally we want to do a POST request that we execute before and that authenticates us. I searched around on how to do this but couldn&#8217;t find anything. What I found was one of the many third-party applications that allow talking to WordPress remotely. I then looked at how they work and how they authenticate themselves.</p>
<p>Instead of doing a POST request to the plugin page like before, they do a POST request to the site&#8217;s index.php. A plugin is registered to catch a particular POST request. Then you simple use the WordPress function user_pass_ok(), that authenticates the user against the database.</p>
<p>Here I demonstrate how to do this using a GET request, because its simpler, but the same thing would work with a POST request.<br />
<code><br />
add_action('plugins_loaded', 'unpackimport_createblog', -1);<br />
function unpackimport_createblog() {<br />
	if(isset($_GET['myplugin'])) {<br />
			if(user_pass_ok($_REQUEST['username'], $_REQUEST['password']))<br />
				echo 'Authentication successful';<br />
}<br />
</code></p>
<p>And the client request:<br />
<code><br />
curl_setopt($ch, CURLOPT_URL, "http://wordpress.mu/index.php?myplugin=true&amp;username=test&amp;password=testpass");<br />
</code></p>
<p><strong>Screencast</strong></p>
<p>I wanted to show a working example rather than just saying this works so I did this screencast. You can also download the plugins yourself and try them out.</p>
<p><span class="youtube">
<object width="425" height="355">
<param name="movie" value="http://www.youtube.com/v/Ort-IMmpo4s&amp;color1=d6d6d6&amp;color2=f0f0f0&amp;border=0&amp;fs=1&amp;hl=en&amp;autoplay=0&amp;showinfo=0&amp;iv_load_policy=3&amp;showsearch=0?rel=1" />
<param name="allowFullScreen" value="true" />
<embed wmode="transparent" src="http://www.youtube.com/v/Ort-IMmpo4s&amp;color1=d6d6d6&amp;color2=f0f0f0&amp;border=0&amp;fs=1&amp;hl=en&amp;autoplay=0&amp;showinfo=0&amp;iv_load_policy=3&amp;showsearch=0?rel=1" type="application/x-shockwave-flash" allowfullscreen="true" width="425" height="355"></embed>
<param name="wmode" value="transparent" />
</object>
</span><p><a href="http://www.youtube.com/watch?v=Ort-IMmpo4s">www.youtube.com/watch?v=Ort-IMmpo4s</a></p></p>
<p><strong>Download</strong></p>
<p><a href="http://www.morgadinho.org/static/unpack_import_demo.tar.gz">unpack_import_demo.tar.gz</a><br />
<a href="http://www.morgadinho.org/static/dump_pack_demo.tar.gz">dump_pack_demo.tar.gz</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.morgadinho.org/2009/11/25/authentication-by-postget-in-wordpress/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WordPress Media Buttons</title>
		<link>http://www.morgadinho.org/2009/10/16/wordpress-media-buttons/</link>
		<comments>http://www.morgadinho.org/2009/10/16/wordpress-media-buttons/#comments</comments>
		<pubDate>Fri, 16 Oct 2009 21:30:45 +0000</pubDate>
		<dc:creator>Nuno Morgadinho</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Wordpress]]></category>

		<guid isPermaLink="false">http://www.morgadinho.org/?p=1912</guid>
		<description><![CDATA[<p>
If you know Wordpress, you know it is a great publishing platform. For a particular plugin I wanted to build I started looking at how to add one of those little icons you have access while writing a post, e.g. &#8216;Add an Image&#8217;, etc.
</p>
<p>
I was looking at how other plugins do this and found this code:
</p>

<pre class="code">
function wp_myplugin_media_button() {
	$context = __('Add media: %s');
	$wp_myplugin_media_button_image = '/path/media_button.gif';
	$wp_myplugin_media_button = '&lt;a href=&quot;media-upload.php?type=wp_myplugin&amp;amp;TB_iframe=true&quot; class=&quot;thickbox&quot;&gt;
  &lt;img src='&quot;.$wp_myplugin_media_button_image.&quot;' /&gt;&lt;/a&gt;&quot;.' %s';
	return sprintf($context, $wp_myplugin_media_button);
}
add_filter('media_buttons_context', 'wp_myplugin_media_button');
</pre>

<p>
But when I tried this in my plugin something weird happened. If both my plugin and the one from where I took this code were active, then only the button from the other plugin would appear. If I disabled the second one then it worked. Why, oh why? After struggling a bit I found the answer.
</p>
<p>
The way the function is receiving the context is not correct. Both functions are asking for the default &#8220;context&#8221; (this is the media buttons themselves) and then adding something to it. The correct way to receive the context is to have it as an argument of the function. In that way it can be updated without a problem. Example follows:
</p>

<pre class="code">
function wp_myplugin_media_button($context) {
	$wp_myplugin_media_button_image = '/path/media_button.gif';
	$wp_myplugin_media_button = '&lt;a href=&quot;media-upload.php?type=wp_myplugin&amp;amp;TB_iframe=true&quot; class=&quot;thickbox&quot;&gt;
  &lt;img src='&quot;.$wp_myplugin_media_button_image.&quot;' /&gt;&lt;/a&gt;&quot;.' %s';
	return sprintf($context, $wp_myplugin_media_button);
}
add_filter('media_buttons_context', 'wp_myplugin_media_button');
</pre>

<p>
Update: if you want to add the button to the end of the media button list just append the %s first, e.g.:
</p>

<pre class="code">
function wp_myplugin_media_button($context) {
	$wp_myplugin_media_button_image = '/path/media_button.gif';
	$wp_myplugin_media_button = ' %s' . '&lt;a href=&quot;media-upload.php?type=wp_myplugin&amp;amp;TB_iframe=true&quot; class=&quot;thickbox&quot;&gt;
  &lt;img src='&quot;.$wp_myplugin_media_button_image.&quot;' /&gt;&lt;/a&gt;&quot;;
	return sprintf($context, $wp_myplugin_media_button);
}
add_filter('media_buttons_context', 'wp_myplugin_media_button');
</pre>

<p>
Hope it helps someone. Cheers.
</p>]]></description>
			<content:encoded><![CDATA[<p>
If you know Wordpress, you know it is a great publishing platform. For a particular plugin I wanted to build I started looking at how to add one of those little icons you have access while writing a post, e.g. &#8216;Add an Image&#8217;, etc.
</p>
<p>
I was looking at how other plugins do this and found this code:
</p>

<pre class="code">
function wp_myplugin_media_button() {
	$context = __('Add media: %s');
	$wp_myplugin_media_button_image = '/path/media_button.gif';
	$wp_myplugin_media_button = '&lt;a href=&quot;media-upload.php?type=wp_myplugin&amp;amp;TB_iframe=true&quot; class=&quot;thickbox&quot;&gt;
  &lt;img src='&quot;.$wp_myplugin_media_button_image.&quot;' /&gt;&lt;/a&gt;&quot;.' %s';
	return sprintf($context, $wp_myplugin_media_button);
}
add_filter('media_buttons_context', 'wp_myplugin_media_button');
</pre>

<p>
But when I tried this in my plugin something weird happened. If both my plugin and the one from where I took this code were active, then only the button from the other plugin would appear. If I disabled the second one then it worked. Why, oh why? After struggling a bit I found the answer.
</p>
<p>
The way the function is receiving the context is not correct. Both functions are asking for the default &#8220;context&#8221; (this is the media buttons themselves) and then adding something to it. The correct way to receive the context is to have it as an argument of the function. In that way it can be updated without a problem. Example follows:
</p>

<pre class="code">
function wp_myplugin_media_button($context) {
	$wp_myplugin_media_button_image = '/path/media_button.gif';
	$wp_myplugin_media_button = '&lt;a href=&quot;media-upload.php?type=wp_myplugin&amp;amp;TB_iframe=true&quot; class=&quot;thickbox&quot;&gt;
  &lt;img src='&quot;.$wp_myplugin_media_button_image.&quot;' /&gt;&lt;/a&gt;&quot;.' %s';
	return sprintf($context, $wp_myplugin_media_button);
}
add_filter('media_buttons_context', 'wp_myplugin_media_button');
</pre>

<p>
Update: if you want to add the button to the end of the media button list just append the %s first, e.g.:
</p>

<pre class="code">
function wp_myplugin_media_button($context) {
	$wp_myplugin_media_button_image = '/path/media_button.gif';
	$wp_myplugin_media_button = ' %s' . '&lt;a href=&quot;media-upload.php?type=wp_myplugin&amp;amp;TB_iframe=true&quot; class=&quot;thickbox&quot;&gt;
  &lt;img src='&quot;.$wp_myplugin_media_button_image.&quot;' /&gt;&lt;/a&gt;&quot;;
	return sprintf($context, $wp_myplugin_media_button);
}
add_filter('media_buttons_context', 'wp_myplugin_media_button');
</pre>

<p>
Hope it helps someone. Cheers.
</p>]]></content:encoded>
			<wfw:commentRss>http://www.morgadinho.org/2009/10/16/wordpress-media-buttons/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
