Uploading PDF using Media Uploader

by DeryckOE   Last Updated September 15, 2018 12:08 PM

I have been using a custom code to upload images to Wordpress using custom metaboxes. Here is the code:

<script type="text/javascript">

    jQuery(document).ready(function( $ ) {

        var formfield;

        $('.upload_button').click(function() {
            $('html').addClass('Image');
            formfield = $(this).prev('.upload_image'); // .attr('name');
            tb_show('', 'media-upload.php?type=image&TB_iframe=true');
            return false;
        });

        // user inserts file into post. only run custom if user started process using the above process
        // window.send_to_editor(html) is how wp would normally handle the received data

        window.original_send_to_editor = window.send_to_editor;
        window.send_to_editor = function(html){

            if (formfield) {
                fileurl = jQuery('img',html).attr('src');

                $(formfield).val(fileurl);
                tb_remove();
                formfield = '';
                $('html').removeClass('Image');

            } else {
                window.original_send_to_editor(html);
            }
        };

    });


</script>

My actual problem is that I have to upload now a PDF. I have try to use the .urlfield input in the media uploader with keeps the URL of the uploaded file.

I have changed fileurl = jQuery('img',html).attr('src'); to fileurl = jQuery('.urlfield',html).val(); but it does not work.

What can I do please?

An alternative tip I would like to know is how can I change the "Insert button" value text to something like "Use this file".

Thanks in advance.



Answers 1


please use this code....

(function( $ ) {
        $( '#uploadpdf' ).on( 'click', function() {
            tb_show('Upload the pdf', 'media-upload.php?type=file&TB_iframe=1');

            window.send_to_editor = function( html ) 
            {

                fileurl = $(html).attr('href'); 

                if(/\.pdf\b/.test(fileurl)){
                    $('#pdf').val(fileurl);

                }else{
                    alert('Not a valid pdf file');
                }
                tb_remove();

            }

            return false;
        });


    })(jQuery);
</script>
Sarjil
Sarjil
September 15, 2018 11:37 AM

Related Questions


Get image post ID in media upload

Updated June 10, 2015 06:03 AM


How to delete file uploaded by Plupload

Updated April 21, 2015 04:03 AM

want to add video upload option for front end user

Updated March 14, 2017 20:08 PM