[update: this bug seems to be fixed in WordPress 2.9 Beta 2]
[update: this bug is back in WordPress 3.0.1 and is still present in WordPress 3.4.2. It may have been in WordPress 3.0 and other versions as well]
After a couple of days of agony thinking that my WordPress database was seriously messed up, I discovered the bug that was causing enclosures to disappear turns out be considered a “fix/feature” by WordPress Org.
WordPress 2.8 not only places auto-enclosures like previous versions of WordPress, but it also auto-removes enclosures if there’s no link to the media file in the post body.
To keep your enclosures from disappearing in 2.8, you can place a link to the media file in your post body…of course this may be a redundant link or an unwanted link.
Why anyone would want auto-enclosures is beyond me. This functionality is really annoying and problematic. It also saves no time as entering the URL in the enclosure field and then using the enclosure to place a download link where the post is displayed is much more efficient…and logical…aren’t these templates we’re talking about? Why would we duplicate copy and code in every post?
So to fix this fix without having to place the redundant link to the media file, you need to edit the file at:
../wp-includes/functions.php
Simply delete or comment out lines 1164-1168. [Update: in 3.0.1 it’s lines 1184-1191] They look like:
foreach ( $pung as $link_test ) {
if ( !in_array( $link_test, $post_links_temp[0] ) ) { // link no longer in post
$wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = 'enclosure' AND meta_value LIKE (%s)", $post_ID, $link_test . '%') );
}
}
Unfortunately of course this code will be added back again if you update WordPress (unless they see the error of their ways).
I would love to offer this as a free plug-in, but I don’t think there are any hooks for this in the WordPress API. I could be wrong as I’m not experienced at all in plug-in development for WordPress.
UPDATE: WordPress 3.3
I didn’t check this with each version, but in version 3.3 it’s line 1219 to 1226 which has this code:
foreach ( $pung as $link_test ) {
if ( !in_array( $link_test, $post_links_temp[0] ) ) { // link no longer in post
$mid = $wpdb->get_col( $wpdb->prepare(“SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = ‘enclosure’ AND meta_value LIKE (%s)”, $post_ID, like_escape( $link_test ) . ‘%’) );
do_action( ‘delete_postmeta’, $mid );
$wpdb->query( $wpdb->prepare(“DELETE FROM $wpdb->postmeta WHERE meta_id IN(%s)”, implode( ‘,’, $mid ) ) );
do_action( ‘deleted_postmeta’, $mid );
}
}
UPDATE: WordPress 3.5
I didn’t check this with each version, but in version 3.4.2 it’s line 427 to 434 which has this code:
foreach ( $pung as $link_test ) {
if ( !in_array( $link_test, $post_links_temp[0] ) ) { // link no longer in post
$mids = $wpdb->get_col( $wpdb->prepare(“SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = ‘enclosure’ AND meta_value LIKE (%s)”, $post_ID, like_escape( $link_test ) . ‘%’) );
foreach ( $mids as $mid )
delete_metadata_by_mid( ‘post’, $mid );
}
}
To find the code each time, I’m searching for “$pung as $link_test” and then reading the entire block of code. It changes, but it’s pretty easy to spot and comment out. Also it’s worth noting that this file won’t necessarily be changed with incremental updates. Still, I wish they would fix this.
Discussions about this issue are taking place on WordPress.org.
There is a way to add this logic as a plugin to prevent WordPress from doing this behavior. We added the exact logic to the Blubrry PowerPress podcasting plugin. If you don’t specify that you want to make a podcast episode, then you don’t make a podcast episode, whether or not you have links to media in your posts.
I’ve left a comment on the WordPress.org trac system with some thoughts on the problem and included my own suggested solution. Here’s a link: http://core.trac.wordpress.org/ticket/10511
Thanks much Kevin! I had 3 episodes not feeding because of this and didn’t even realize it… awesome!
Tried this, and it made absolutely no difference, which I find baffling.
Hmmm…I’m not sure how that’s possible, but I’ve found that the WordPress team did listen to our complaints and fixed this in 2.9.x, so maybe just upgrading will help you.
well, it seems that this bug is back again with the latest update…..
Thanks Andreas…Grrr…this is frustrating. I’ve updated my post and verified that the fix works again. Just comment the same code out, but with the new line numbers.
Thanks for posting the code and saving me from going nuts! Already wasted a couple hours this afternoon trying to figure out why my custom enclosure fields were disappearing after upgrading from 2.8 to 3.0.
[…] got it working!!!! I would like to thank Kevin J Edwards!! Thanks for your post on this stupid error. You are the […]
Thank you! I spent my entire morning trying to fix this and bam you did it in 2 mins. Thank you
[…] got it working!!!! I would like to thank Kevin J Edwards!! Thanks for your post on this stupid error. You are the […]
well, and in 3.1 it will be lines 1207-1214…
we might see some positive developments planned for the future, though – as function do_enclose now starts with the following comment:
//TODO: Tidy this ghetto code up and make the debug code optional
Another workaround, at least working for me, is to add custom field ‘enclosure’, but do not update the post. (i.e. create a post, go edit it, but don’t save it) It works perfect for me since I don’t put anything else in a podcast post other than that link.
I will try your solution when I get a chance tomorrow, seems to be promising, thanks a lot!
These photos are absolutely beautiful. The same results just can’t be duplicated using Photoshop on a digital photo.
Thank you for this information ! Is almost started to freak out because of this enlcosure vanishing issue.
So glad I found this post! You’ve saved me a LOT of grief. Thank you!
OK, I’m back because I updated and can not get this thing to work anymore. Driving me insane. What’s the secret with 3.4.2 ?