I'm writing a function for a plugin that does a particular action when a new media file is uploaded, provided it is of particular types (specifically docx and doc, i.e. MSWord documents). However, it doesn't seem to detect the attachment type correctly.
I suspect this may be because the action hook (add_attachment) runs before the line is fully created in the posts table, and therefore the mime type is blank. That said, the attachment_id is available, so shouldn't the mime type be there as well?
When I upload a docx, a doc and an image, all three are processed by the function and I get three lines in my documents table... there should only be two.
The action hook is simply:
add_action('add_attachment', 'wle_record_document_upload');
The function looks like this (it adds a line to a custom table which I will be using as part of the plugin):
function wle_record_document_upload($attachment_id) {
global $wpdb;
$user_ID = get_current_user_id();
$type = get_post_mime_type($attachment_id);
if ($type = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' || $type = 'application/msword') {
$sql = "INSERT INTO {$wpdb->prefix}wle_documents (user_id, attachment_id, status_date)
VALUES ($user_ID, $attachment_id, NOW())";
$result = $wpdb->query($sql);
}
}
If anyone can see what's wrong, I'd appreciate it!