I am trying to create a function that uploads photos into the media library and then get the id, then send the url of the image to another page

Question

Here us what I have

function test_page()
{

// check if user is allowed access
if (!current_user_can('manage_options')) {
    return;
}

        test_handle_post();
    ?>
            <h1>Hello World!</h1>
            <h2>Upload a File</h2>
            <!-- Form to handle the upload - The enctype value here is very important -->
            <form  method="post" enctype="multipart/form-data">
                <input type='file' id='test_upload_pdf' name='test_upload_pdf'></input>
                <?php submit_button('Upload')?>
            </form>
    <?php

}
?>

<?php


function test_handle_post()
{
    // First check if the file appears on the _FILES array
    if (isset($_FILES['test_upload_pdf'])) {
        $pdf = $_FILES['test_upload_pdf'];

        // Use the wordpress function to upload
        // test_upload_pdf corresponds to the position in the $_FILES array
        // 0 means the content is not associated with any other posts
        $uploaded = media_handle_upload('test_upload_pdf', 0);
        // Error checking using WP functions
        if (is_wp_error($uploaded)) {
            echo "Error uploading file: " . $uploaded->get_error_message();
        } else {
            echo "File upload successful!";
            // $image_id = $uploaded;
            // echo $image_id;
            // collect_image_id($uploaded);


        }

        
        if(isset($uploaded)){
                           
            $id = $uploaded;
        

        }

        function get_url($image_url)
        {

            global $id;

            $image_url = wp_get_attachment_url($id);

            return $image_url;

        }
        add_filter('call_function_get', 'get_url');



    }



}
0
, Mary Olowu 2 years 2020-09-02T12:10:22-05:00 0 Answers 57 views 0

Leave an answer

Browse
Browse