The media_upload_audio WordPress PHP function handles the process of uploading an audio file.
Usage
To use the media_upload_audio function, simply call it in your theme or plugin code:
media_upload_audio();
Parameters
This function does not have any parameters.
More information
See WordPress Developer Resources: media_upload_audio
Examples
Basic audio upload
In this example, the media_upload_audio function is called to handle the audio upload process.
// Add a new audio file to the media library media_upload_audio();
Custom audio upload form
Create a custom audio upload form using the wp_handle_upload function. After successfully uploading the file, use the media_upload_audio function to add the file to the media library.
// Process the audio file upload
if (isset($_POST['submit_audio'])) {
// Set upload options
$upload_overrides = array('test_form' => false);
// Process the uploaded file
$uploaded_audio = wp_handle_upload($_FILES['audio_file'], $upload_overrides);
// Add the uploaded file to the media library
if ($uploaded_audio && !isset($uploaded_audio['error'])) {
media_upload_audio();
}
}
Audio upload in a custom meta box
Create a custom meta box for audio uploads in a post or custom post type. Use the media_upload_audio function to handle the audio upload process.
// Add a meta box for audio uploads
function my_custom_audio_meta_box() {
add_meta_box(
'my_audio_upload',
'Upload Audio',
'my_audio_upload_meta_box_callback',
'post',
'side'
);
}
add_action('add_meta_boxes', 'my_custom_audio_meta_box');
// Display the meta box content function my_audio_upload_meta_box_callback($post) {
// Add an nonce field to verify the submission wp_nonce_field('my_audio_upload', 'my_audio_upload_nonce');
// Add the file input field
echo '<input type="file" name="audio_file" id="audio_file" />';
// Add the submit button
echo '<input type="submit" name="submit_audio" id="submit_audio" class="button button-primary" value="Upload Audio" />';
}
// Save the uploaded audio file
function my_save_audio_upload($post_id) {
// Verify the nonce
if (!isset($_POST['my_audio_upload_nonce']) || !wp_verify_nonce($_POST['my_audio_upload_nonce'], 'my_audio_upload')) {
return;
}
// Check for the uploaded file
if (isset($_FILES['audio_file'])) {
media_upload_audio();
}
}
add_action('save_post', 'my_save_audio_upload');