0

I want to make a custom post type of songs. Is there any way that i can provide an upload button to the admin to upload song as a post. So that it can be accessed via the_content().

I don't want to go for custom meta box option.

Thanks

1 Answer 1

0

Assuming that you have a Custom Post Type "myaudio":

Create a function to render the upload page

<?php 
function brg_upload_audio_page(){
    //display uplaod form
    if (empty($_POST)){ ?>
    <h2>Upload Audio</h2>
    <form action="" method="post" enctype="multipart/form-data">
    <p>Title of the song</p>
    <input type="text" name="post_title">
    <p>File</p>
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload" name="submit">
    </form>
<?php }

if ( ! function_exists( 'wp_handle_upload' ) ) require_once( ABSPATH . 'wp-admin/includes/file.php' );
if (!empty($_POST)){
$uploadedfile = $_FILES['fileToUpload'];
$upload_overrides = array( 'test_form' => false );
$movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
$title = $_POST['post_title'];
if ( $movefile ) {
    $uploaded_file = $movefile["file"];
    //create a post with the custom post type my_audio
    $post_args = array (
                'post_status'           => 'publish', 
                'post_type'             => 'myaudio',
                'post_title'            => $title,
                'post_content'          => '',
    $parent_post_id = wp_insert_post( $post_args );
    $filetype = wp_check_filetype( basename( $uploaded_file), null );
    // Get the path to the upload directory.
    $wp_upload_dir = wp_upload_dir();
    // Prepare an array of post data for the attachment.
      $attachment = array(
        'guid'           => $wp_upload_dir['url'] . '/' . basename( $uploaded_file ), 
        'post_mime_type' => $filetype['type'],
        'post_title'     => preg_replace( '/\.[^.]+$/', '', basename( $uploaded_file ) ),
    'post_content'   => '',
    'post_status'    => 'inherit'
);

// Insert the attachment to the post
$attach_id = wp_insert_attachment( $attachment, $uploaded_file, $parent_post_id );

// Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
require_once( ABSPATH . 'wp-admin/includes/image.php' );

// Generate the metadata for the attachment, and update the database record.
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );

wp_update_attachment_metadata( $attach_id, $attach_data );
} else {
    echo "File upload failed";
}
}
}
//create the menu item and the page for upload

function register_brg_upload_menu() {

add_menu_page( 'My Audio', 'My Audio', 'manage_options', 'brg_upload_audio_page', 'brg_upload_audio_page', 'dashicons-portfolio', 1.1);

$submenu_pages = array(

        // Avoid duplicate pages. Add submenu page with same slug as parent slug.
        array(
            'parent_slug'   => 'brg_upload_audio_page',
            'page_title'    => 'Upload',
            'menu_title'    => 'Upload',
            'capability'    => 'manage_options',
            'menu_slug'     => 'brg_upload_audio_page',
            'function'      => 'brg_upload_audio_page',// Uses the same callback function as parent menu. 
        )
);

// Add each submenu item to custom admin menu.
    foreach($submenu_pages as $submenu){

        add_submenu_page(
            $submenu['parent_slug'],
            $submenu['page_title'],
            $submenu['menu_title'],
            $submenu['capability'],
            $submenu['menu_slug'],
            $submenu['function']
        );
}
}
add_action( 'admin_menu', 'register_brg_upload_menu' );
2
  • Thanks just to be clear this will link the audio to my_audio post type ? So can i disable the editor feature for my_audio and i will be able to use the audio file with the_content() function ?
    – user59172
    Commented Mar 11, 2015 at 20:21
  • yes, actually the audio file is going to be an attachment of my_audio post type. You can always just query the attachment but with a custom post type my_audio you'll have more options. Commented Mar 12, 2015 at 0:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.