public function add_to_media_library()
    {
        if ($this->upload_path === false) {
            return false;
        }

        $attachment = [
            'guid' => $this->upload_url,
            'post_mime_type' => $this->upload_type,
            'post_title' => \pathinfo($this->upload_path, PATHINFO_FILENAME),
            'post_content' => '',
            'post_status' => 'inherit',
        ];

        // Insert the attachment.
        $attach_id = \wp_insert_attachment($attachment);

        if ($attach_id == 0 || $attach_id instanceof \WP_Error) {
            return false;
        }

        // 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 meta for the attachment.
        $attach_data = \wp_generate_attachment_metadata($attach_id, $this->upload_path);

        // Update attachment meta.
        \wp_update_attachment_metadata($attach_id, $attach_data);
        \update_attached_file($attach_id, $this->upload_path);

        $this->attachment_id = $attach_id;

        return true;
    }
public function add_to_media_library()
    {
        if ($this->upload_path === false) {
            return false;
        }

        $attachment = [
            'guid' => $this->upload_url,
            'post_mime_type' => $this->upload_type,
            'post_title' => \pathinfo($this->upload_path, PATHINFO_FILENAME),
            'post_content' => '',
            'post_status' => 'inherit',
        ];

        // Insert the attachment.
        $attach_id = \wp_insert_attachment($attachment);

        if ($attach_id == 0 || $attach_id instanceof \WP_Error) {
            return false;
        }

        // 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 meta for the attachment.
        $attach_data = \wp_generate_attachment_metadata($attach_id, $this->upload_path);

        // Update attachment meta.
        \wp_update_attachment_metadata($attach_id, $attach_data);
        \update_attached_file($attach_id, $this->upload_path);

        $this->attachment_id = $attach_id;

        return true;
    }
public function add_to_media_library ( ) { if ( $ this -> upload_path === false ) { return false ; } $ attachment = [ 'guid' => $ this -> upload_url , 'post_mime_type' => $ this -> upload_type , 'post_title' => \ pathinfo ( $ this -> upload_path , PATHINFO_FILENAME ) , 'post_content' => '' , 'post_status' => 'inherit' , ] ; // Insert the attachment. $ attach_id = \ wp_insert_attachment ( $ attachment ) ; if ( $ attach_id == 0 || $ attach_id instanceof \ WP_Error ) { return false ; } // 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 meta for the attachment. $ attach_data = \ wp_generate_attachment_metadata ( $ attach_id , $ this -> upload_path ) ; // Update attachment meta. \ wp_update_attachment_metadata ( $ attach_id , $ attach_data ) ; \ update_attached_file ( $ attach_id , $ this -> upload_path ) ; $ this -> attachment_id = $ attach_id ; return true ; }
Add an uploaded file into the media library so it is usable in the WordPress admin.

@since 1.0.0

@return bool
Add an uploaded file into the media library so it is usable in the WordPress admin .