0

For a site I'm building (for a client), I've written a shortcode, where the poster can enter [vine id="..."] followed by the ID of a Vine video. When the post is saved, my PHP functions will then go away and find the URL of the Vine (it's in the Meta Data of the Vine) and download it to the server. This was all great until my client decided to use Amazon S3 to host all Media files. The way I had originally implemented the downloading of the Vine (.mp4), it went straight to default upload location on the server, not the S3 Bucket. So I rewrote the script to do this, using media_handle_sideload(). Here it is:

function vineDL() {
    $post = get_post();
    $post_id = $post->ID;
    $post_content = get_post_field('post_content', $post_id);
    if (in_category("Vines")) {
        preg_match_all('/\[vine.+id=[\'"](.+?)[\'"]\]/i', $post_content, $matches); // find the [vine id=''] part and fetch value of ID
        $vineID = $matches[1][0];

        $url = vine($vineID); // the vine() function deals with grabbing the url of the .mp4 file
        $tmp = download_url( $url ); // put in a temp locaiton
        $file_array = array(
            'name' => basename( $videourl )  . "-" . $post->ID . ".mp4",
            'tmp_name' => $tmp
        ); // set up an array a bit like $_FILES

        if ( is_wp_error( $tmp ) ) {
            @unlink( $file_array[ 'tmp_name' ] );
            return $tmp;
        } // check for errors

        $id = media_handle_sideload( $file_array, 0 ); //download the file (0 represents the Post ID. (At this point in time I don't want it to be attached to a post so I've left it as 0)
        // Check for handle sideload errors.
        if ( is_wp_error( $id ) ) {
            @unlink( $file_array['tmp_name'] );
            return $id;
        }

        $attachment_url = wp_get_attachment_url( $id );
    }
}
add_action('save_post', 'vineDL');

Problem is, media_handle_sideload() doesn't seem to like Video files. When I change the extension part on name (in the $file_array) to .jpg instead of .mp4 it downloads fine and adds it to Media Library (obviously it doesn't work, but at least it downloads). But for mp4 it doesn't.

Does anyone have a way to fix this, or an alternate way to do it?

Thanks for any help :)

  • Have you checked what media_handle_sideload() returns when it fails? It should be a WP_Error of some type that will give you more information. – Otto Apr 30 '14 at 19:09
  • How would I return the value of media_handle_sideload()? I'm currently on the Edit screen for a post when the post is saved, so how would I get to be able to view the error? Also I've had the thought that I may need to change the post_mime_type or something, if the default is for an image and I need it to be for a .mp4 file (I think it would be video/quicktime but I'm not 100% sure on that). I think I'd use the $post_data array but I'm not sure of the syntax :/ – Tom Oakley Apr 30 '14 at 22:13
  • If you want to see what it is returning, just var_dump() the $id you get back and then die;. This is for debugging, not for use in production. If you're trying to solve an error, then you need to see what the error messages actually are. And the mime-type for an mp4 is video/mp4 and it's already handled internally by WordPress. – Otto May 1 '14 at 6:19

Your Answer

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

Browse other questions tagged or ask your own question.