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.

2 Comment(s) »

Comment by Tom

Posted on March 22, 2007

Hello there. It happens that my technorati tags disappear too! I use your plugin, I also have Jerome’s plugin but it is not activated anymore.. So do you really it’s coming from his plugins? Should I remove it completely even if it not activated to solve the problem? (see my blog in my email ty).

Comment by dobiho

Posted on August 19, 2007

Thanks, I have a same problem. I can fix it.

4 Trackbacks

Leave a comment




XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Comment moderation is in use! It may need up to a day until your comment appears.
Please don't submit it twice.