I’m writing a plugin called ‘Featured Releases’ for a client. The user fills out a short form in the admin area which inserts some text and an image id into the wordpress database via $wpdb->insert. On my development machine it works fine, but on the production server (which has over 500 posts of all sorts, mostly images), the value of the image id is being changed to 127 no matter what I do. 127 is the id of a post, but its not an image and certainly not the id that was passed from the form.
The image is uploaded via the native wordpress media uploader. The image’s post id (from the wp_posts table) is passed to a hidden input in the form. On submit, this function handles the $_POST data:
function fr_add_album() {
global $wpdb;
if ( isset($_POST) && (count($_POST) > 0) ) {
check_admin_referer('featured-releases-add_album');
$table_name = $wpdb->prefix . 'featured_releases';
$desired_keys = array('fr_band', 'fr_album', 'fr_album_description', 'fr_image_id', 'fr_shop_url');
$data = array();
foreach( $desired_keys as $key ) {
// Define and sanatize data
if ( isset($_POST["$key"]) ) {
$accepted_html = array('a' => array('href' => array(),
'title' => array()));
$data["$key"] = wp_kses($_POST["$key"], $accepted_html);
unset($_POST["$key"]);
} else {
$data["$key"] = '';
}
}
// Sanitize the shop url
if ( isset($data['fr_shop_url']) ) {
$data['fr_shop_url'] = esc_url($data['fr_shop_url']);
}
$data['fr_modified'] = date('Y-m-d h:i:sa');
error_log($data['fr_image_id']); // This returns the correct value!
$wpdb->insert($table_name, $data, array('%s','%s', '%s', '%d', '%s'));
// After the insert, the fr_image_id column always reads "127"
}
}
The insane thing is that there are a few images which do post correctly. These were the first images I uploaded when I created the wordpress install. Every other image from any other date, including any new image I upload is saved as 127 in the database. All the images have the same owners and same permissions. I also can’t see any difference in wp_posts between the new images and the old. Any ideas? I’m dyin’ here. Thanks for your help!