• Resolved lee718

    (@lee718)


    I need to find the url of the inserted image uploaded via front-end submission. When I do a print_r I just get the temp name and name for $_FILES, but not the url of the image. Here is the code relating to the image upload and further down the array of $_FILES. Thanks

    function insert_attachment($file_handler, $post_id, $setthumb = 'false') {
            if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) __return_false();
    
            require_once(ABSPATH . "wp-admin" . '/includes/image.php');
            require_once(ABSPATH . "wp-admin" . '/includes/file.php');
            require_once(ABSPATH . "wp-admin" . '/includes/media.php');
    
            $attach_id = media_handle_upload( $file_handler, $post_id );
    
            if ($setthumb) update_post_meta($post_id, '_thumbnail_id', $attach_id);
            return $attach_id;
        }
    
        if ($_FILES) {
            foreach ($_FILES as $file => $array) {
                $newupload = insert_attachment($file, $pid);
            }
        }
    
    //If I run a print_r on $_FILES I get this:
    
        Array
        (
            [file] => Array
                (
                     [name] => arrow-nav.png
                     [type] => image/png
                     [tmp_name] => /Applications/MAMP/tmp/php/phpaH5GZE
                     [error] => 0
                     [size] => 0
                )
    
        )
Viewing 1 replies (of 1 total)
  • Thread Starter lee718

    (@lee718)

    In case anyone needs this, here is my solution:

    if ($_FILES) {
    
            function insert_attachment($file_handler, $post_id, $setthumb = 'false') {
            if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) __return_false();
    
            require_once(ABSPATH . "wp-admin" . '/includes/image.php');
            require_once(ABSPATH . "wp-admin" . '/includes/file.php');
            require_once(ABSPATH . "wp-admin" . '/includes/media.php');
    
            $attach_id = media_handle_upload( $file_handler, $post_id );
    
            //get url
            $attachment_url = wp_get_attachment_url($attach_id);
            add_post_meta($post_id, '_file_paths', $attachment_url);
    
            $attachment_data = array(
                'ID' => $attach_id
            );
    
            wp_update_post($attachment_data);
    
            if ($setthumb) update_post_meta($post_id, '_thumbnail_id', $attach_id);
            return $attach_id;
    
        }
    
            foreach ($_FILES as $file => $array) {
                $newupload = insert_attachment($file, $pid);
                //getting the url for the new upload
                $attachment_url = wp_get_attachment_url($newupload);
            }
    
        }
Viewing 1 replies (of 1 total)
  • The topic ‘get the url of image attachment’ is closed to new replies.