<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress.com" -->
<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/"
	>

<channel>
	<title>jeromes-keywords &amp;laquo; WordPress.com Tag Feed</title>
	<link>http://en.wordpress.com/tag/jeromes-keywords/</link>
	<description>Feed of posts on WordPress.com tagged "jeromes-keywords"</description>
	<pubDate>Fri, 04 Dec 2009 20:38:37 +0000</pubDate>

	<generator>http://en.wordpress.com/tags/</generator>
	<language>en</language>

<item>
<title><![CDATA[Authorization and intention/origination verification when using the edit_post hook]]></title>
<link>http://markjaquith.wordpress.com/2007/01/28/authorization-and-intentionorigination-verification-when-using-the-edit_post-hook/</link>
<pubDate>Sun, 28 Jan 2007 05:32:48 +0000</pubDate>
<dc:creator>Mark Jaquith</dc:creator>
<guid>http://markjaquith.wordpress.com/2007/01/28/authorization-and-intentionorigination-verification-when-using-the-edit_post-hook/</guid>
<description><![CDATA[There have been reports of plugins that have started erasing their managed Custom Fields upon action]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>There have been reports of plugins that have started erasing their managed Custom Fields upon actions like comment submission.  <a href="http://www.neato.co.nz/archives/2007/01/17/ultimate-tag-warrior-31415926/">UTW</a> was <a href="http://ocaoimh.ie/2007/01/23/the-new-wp-utw-gotcha/">bitten</a>, as was  Jerome&#8217;s Keywords and <a href="http://wordpress.org/support/topic/102423">some other plugins</a> that use custom fields.</p>
<p>The problem was brought to light with the release of WordPress 2.1, but circumstances exist in older WP versions that would trigger these issues in some plugins.</p>
<p>The plugins are doing this:</p>
<ol>
<li>A plugin inserts a special form field into the post edit form</li>
<li>The plugin monitors the form field by hooking into <code>edit_post</code></li>
<li>When the form value is empty or doesn&#8217;t exist, the plugin assumes the user deleted what was in it, and procedes to delete all the custom values the plugin had stored for that post</li>
</ol>
<p>The issue occurs because the plugins assume that every time <code>edit_post</code> is triggered, their inserted form field will be included in <code>$_POST</code>.  This isn&#8217;t the case.  <code>edit_post</code> is called for requests that do not originate from the post edit form and for requests that are not initiated by a privileged user.  Comment submission in WordPress 2.1 is one of these cases.  Editing of a post in 2.1 (and earlier versions) via XML-RPC is another case.</p>
<p><strong>Plugins cannot assume that the absence of a POST field means that POST field existed in an empty state, and plugins cannot assume that all calls to <code>edit_post</code> are performed by privileged users.</strong></p>
<p>Here are the two things that plugins must do:</p>
<ol>
<li>Verify that the user performing the action is authorized to perform the action by using the <code>current_user_can()</code> function or its siblings.</li>
<li>Verify intention of the user and the origination of the request by embedding a hidden form field with a nonce value, along with your usual custom field.</li>
</ol>
<p>Here is an example:</p>
<pre>function your_form_hook() {
	echo '&#60;input type="text" name="your-plugin" id="your-plugin"
			value="' . your_get_value() . '" /&#62;
		&#60;input type="hidden" name="your-plugin-verify-key" id="your-plugin-verify-key"
			value="' . wp_create_nonce('your-plugin') . '" /&#62;';
}

add_action('edit_form_advanced', 'your_form_hook');

function your_edit_post_hook($post_id) {
	// authorization
	if ( !current_user_can('edit_post', $post_id) )
		return $post_id;
	// origination and intention
	if ( !wp_verify_nonce($_POST['your-plugin-verify-key'], 'your-plugin') )
		return $post_id;
	your_update($post_id); // do the actual update here
	return $post_id;
}

add_action('edit_post', 'your_edit_post_hook');
</pre>
<p>This is a post aimed at plugin authors, so I&#8217;d appreciate it if we could save the comment space below for plugin authors who have questions about this topic.  If a particular plugin you&#8217;re using is erasing Custom Fields, please contact its author directly.</p>
<p><strong>Note:</strong> I&#8217;ve mentioned the <code>edit_post</code> hook, but there are other similar hooks that the above also applies to.  <code>publish_post</code> and <code>save_post</code> are two that come to mind.</p>
</div>]]></content:encoded>
</item>

</channel>
</rss>
