The media_upload_video() WordPress PHP function handles uploading a video file.
Usage
media_upload_video();
Parameters
- None
More information
See WordPress Developer Resources: media_upload_video()
Examples
Uploading a video from a form
This example uploads a video from an HTML form to the WordPress media library.
<!-- HTML form for video upload --> <form action="video-upload.php" method="post" enctype="multipart/form-data"> <input type="file" name="video" accept="video/*"> <input type="submit" value="Upload Video"> </form>
// video-upload.php
require_once('wp-load.php');
// Check if the file is uploaded
if (isset($_FILES['video'])) {
  // Prepare the file for upload
  $uploaded_video = wp_handle_upload($_FILES['video'], array('test_form' => false));
  // Upload the video using media_upload_video
  if (!empty($uploaded_video['file'])) {
    media_upload_video();
  }
}
Uploading a video from a URL
This example uploads a video from a URL to the WordPress media library.
$url = "https://example.com/video.mp4";
$video_id = media_sideload_image($url, 0, 'Video description');
if (!is_wp_error($video_id)) {
  media_upload_video();
}
Displaying uploaded videos in a gallery
This example displays all uploaded videos in a gallery.
$args = array(
  'post_type' => 'attachment',
  'post_mime_type' => 'video',
  'numberposts' => -1,
);
$videos = get_posts($args);
echo '<div class="video-gallery">';
foreach ($videos as $video) {
  echo wp_get_attachment_link($video->ID);
}
echo '</div>';
Attaching an uploaded video to a post
This example attaches an uploaded video to a post.
$post_id = 1; // The post ID to attach the video to
if (isset($_FILES['video'])) {
  $uploaded_video = wp_handle_upload($_FILES['video'], array('test_form' => false));
  if (!empty($uploaded_video['file'])) {
    media_upload_video();
    // Set up attachment data
    $attachment_data = array(
      'guid' => $uploaded_video['url'],
      'post_mime_type' => $uploaded_video['type'],
      'post_title' => preg_replace('/\.[^.]+$/', '', $_FILES['video']['name']),
      'post_content' => '',
      'post_status' => 'inherit',
    );
    // Insert the attachment
    $attachment_id = wp_insert_attachment($attachment_data, $uploaded_video['file'], $post_id);
    // Update the attachment metadata
    wp_update_attachment_metadata($attachment_id, wp_generate_attachment_metadata($attachment_id, $uploaded_video['file']));
  }
}
Programmatically uploading a video from a local path
This example uploads a video from a local path to the WordPress media library.
// The path to the video file $video_file = '/path/to/video.mp4'; // Prepare the file for upload $filetype = wp_check_filetype(basename($video_file), null); $wp_upload_dir = wp_upload_dir(); // Move the video to the uploads directory $new_video_path = $wp_upload_dir['path'] . '/' . basename($video_file); move_uploaded