How to Create a Custom Metabox Listing All Audio Files from the Media Library in WordPress

Home » Snippets » How to Create a Custom Metabox Listing All Audio Files from the Media Library in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Are you looking for a convenient way to manage and display all the audio files in your WordPress media library? By default, WordPress doesn’t provide a built-in feature to view and organize audio files separately. However, with the help of a custom metabox, you can easily create a dedicated section to list and access all the audio files in your media library. In this article, we will guide you through the process of adding a custom metabox that will display a convenient and organized list of all your audio files, making it easier for you to browse, select, and utilize them within your WordPress site. So, let’s dive in and enhance your audio file management experience in WordPress.

					function wpturbo_custom_audio_metabox() {
    add_meta_box(
        'wpturbo_audio_metabox',
        'Audio Files',
        'wpturbo_render_audio_metabox',
        'post',
        'normal',
        'default'
    );
}

function wpturbo_render_audio_metabox($post) {
    $args = array(
        'post_type' => 'attachment',
        'post_mime_type' => 'audio',
        'post_status' => 'inherit',
        'posts_per_page' => -1,
    );

    $attachments = get_posts($args);

    if ($attachments) {
        echo '<ul>';
        foreach ($attachments as $attachment) {
            echo '<li><a href="' . esc_url(wp_get_attachment_url($attachment->ID)) . '">' . esc_html(get_the_title($attachment->ID)) . '</a></li>';
        }
        echo '</ul>';
    } else {
        echo 'No audio files found.';
    }
}

add_action('add_meta_boxes', 'wpturbo_custom_audio_metabox');
				

The code snippet above shows how to create a custom metabox in WordPress that lists all audio files from the media library. This can be useful if you want to provide an easy way for users to select and attach audio files to a post.

The first function, wpturbo_custom_audio_metabox(), is responsible for adding the metabox to the post editor screen. It uses the add_meta_box() function to define the metabox. The parameters are as follows:

  • wpturbo_audio_metabox: A unique ID for the metabox.
  • 'Audio Files': The title of the metabox.
  • wpturbo_render_audio_metabox(): The callback function that will render the content of the metabox.
  • 'post': The screen or screens where the metabox should be displayed. In this case, it’s the post editor screen.
  • 'normal': The context in which the metabox should be displayed. In this case, it’s the main content area.
  • 'default': The priority of the metabox. In this case, it’s set to the default priority.

The wpturbo_render_audio_metabox() function is the callback function that is called to render the content of the metabox. Inside this function, we define an array of arguments to query for audio attachments in the media library. The arguments include the post type set to 'attachment', the post mime type set to 'audio', the post status set to 'inherit', and we set the number of posts per page to -1 in order to retrieve all audio attachments.

We use the get_posts() function with the arguments array to retrieve the audio attachments. If there are attachments found, we start by outputting an unordered list <ul>. Then, for each attachment, we output a list item <li> with a link <a> that displays the title of the attachment. The link points to the attachment URL obtained with wp_get_attachment_url(). We also sanitize the title using esc_html() and make the URL safe using esc_url().

If no audio files are found, we simply output a message saying "No audio files found.".

Finally, we use the add_action() function to hook the wpturbo_custom_audio_metabox() function into the add_meta_boxes action. This ensures that the custom metabox is added to the post editor screen.

Register an account to save your snippets or go Pro to get more features.