-1

I am looking for an option where a custom meta value will be saved when user uploads an attachment.

Lets say I need a meta key _example_meta_key and want to save meta value of this as ex087659bh (It will be a randomly generated number, I can take care of this.)

The problem is, I can't seem to find any filters to add custom meta value on upload.

There are some tutorials which shows the way to add fields in media edit form but I need this to be executed at the time of file upload.

1

Use added_post_meta and update_post_meta with the $post_id. For extended properties see this post and this for more image functions.

add_action('added_post_meta', 'wpse_20151218_after_post_meta', 10, 4);

function wpse_20151218_after_post_meta($meta_id, $post_id, $meta_key, $meta_value) {

    // _wp_attachment_metadata added
    if($meta_key === '_wp_attachment_metadata') {

        // Add Custom Field
        update_post_meta($post_id, '_example_meta_key', 'ex087659bh');

        // _wp_attached_file
        // _wp_attachment_metadata (serialized)
        // _wp_attachment_image_alt
        // _example_meta_key

        $attachment_meta = get_post_meta($post_id);
    }
}
  • Please do not post duplicates. If a question is a duplicate of another, flag it as such. – Pieter Goosen Dec 19 '15 at 18:41
-1

As @jgraup pointed out. You can use the filter wp_handle_upload. The final code will look like something what is pasted below. You might want to use only one of the two hooks or maybe both of them.

add_filter('wp_handle_upload_prefilter', 'startOfWpHandleUpload', 10, 2 );

function startOfWpHandleUpload( $file ) {

    /* Here you can access the following properties of the uploaded file
     * before it is processed by the wp_handle_upload function. 
     */

    /* Name of the uploaded file. */
    $file['name'];
    /* MIME Type of the uploaded file. */
    $file['type'];
    /* Temporary location of the stored file. */
    $file['tmp_name'];
    /* "0" for no errror. Don't know what is for error. */
    $file['error'];
    /* size in bytes of the uploaded file. */
    $file['size'];

    return $file;
}

add_filter('wp_handle_upload', 'endOfWpHandleUpload', 10, 2 );

function endOfWpHandleUpload( $file, $type ) {

    /* Here you can access the following properties of the uploaded file
     * before it is processed by the wp_handle_upload function.
     */

    /* Absolute path of the file on the filesystem of the operating system. */
    $file['file'];
    /* Public accessible URL of the file over the Internet. */
    $file['url'];
    /* MIME type of the file. */
    $file['type'];

    return $file;
}
  • Right, but then how would you add the custom field? – jgraup Dec 19 '15 at 15:01
  • @jgraup Why there is a requirement of removing comments for testing? – Omar Tariq Dec 19 '15 at 18:05
  • They are added intentionally in the body of the function to help explain the code to the author of the question. That's a good practice... – Omar Tariq Dec 19 '15 at 18:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.