Arne Brachhold

Fixing Jerome’s Keywords and WordPress 2.1

Filed under: Wordpress — arne on March 3, 2007

I’m using the Jerome’s Keywords tagging plugin on this site and noticed that all keywords (tags) for a post will go away if I approve its comments. After some search through the code I’ve found that the plugin is using the “edit_post” hook to update the keywords after you’ve changed them on the post screen.

The problem is that this hook is also executed when you approve a comment, but since you are not coming from the post form you’re not sending any post data (like the keywords) and the plugin assumes that the post has no keywords or you’ve deleted them. Mark Jaquith wrote about this behavior earlier this year.

To fix this bug, you have to edit the “keywords_update” function (line 614) of the plugin from:
[PHP]
function keywords_update($id) {

// remove old value
delete_post_meta($id, KEYWORDS_META);

// clean up keywords list & save
$keyword_list = “”;
$post_keywords = explode(“,”, $_REQUEST[‘keywords_list’]);
foreach($post_keywords as $keyword) {
if ( !empty($keyword ) ) {
if ( !empty($keyword_list) )
$keyword_list .= “,”;
$keyword_list .= trim($keyword);
}
}

if (!empty($keyword_list) )
add_post_meta($id, KEYWORDS_META, $keyword_list);
}
[/PHP]
to:
[PHP]
function keywords_update($id) {
//isset returns true if the value exists, even if it’s empty.
if(isset($_REQUEST[‘keywords_list’])) {
// remove old value
delete_post_meta($id, KEYWORDS_META);

// clean up keywords list & save
$keyword_list = “”;
$post_keywords = explode(“,”, $_REQUEST[‘keywords_list’]);
foreach($post_keywords as $keyword) {
if ( !empty($keyword ) ) {
if ( !empty($keyword_list) )
$keyword_list .= “,”;
$keyword_list .= trim($keyword);
}
}

if (!empty($keyword_list) )
add_post_meta($id, KEYWORDS_META, $keyword_list);
}
}
[/PHP]
The “isset” checks if the variable “keywords_list” exists and returns true even if it’s empty. If you’re coming from the post form, it exist and may be empty if you have deleted the keywords. If you’re moderating comments, it won’t exist and the keywords will not be accidentally updated.

Note: This is for version 1.9 of the plugin.