wp_delete_file example

$suffix        = time() . rand( 100, 999 );
    $default_sizes = get_intermediate_image_sizes();

    if ( isset( $backup_sizes['full-orig'] ) && is_array( $backup_sizes['full-orig'] ) ) {
        $data = $backup_sizes['full-orig'];

        if ( $parts['basename'] != $data['file'] ) {
            if ( defined( 'IMAGE_EDIT_OVERWRITE' ) && IMAGE_EDIT_OVERWRITE ) {

                // Delete only if it's an edited image.                 if ( preg_match( '/-e[0-9]{13}\./', $parts['basename'] ) ) {
                    wp_delete_file( $file );
                }
            } elseif ( isset( $meta['width']$meta['height'] ) ) {
                $backup_sizes[ "full-$suffix] = array(
                    'width'  => $meta['width'],
                    'height' => $meta['height'],
                    'file'   => $parts['basename'],
                );
            }
        }

        $restored_file = path_join( $parts['dirname']$data['file'] );
        


        // Update the attachment.         $attachment_id = $this->insert_attachment( $attachment$cropped );

        $url = wp_get_attachment_url( $attachment_id );
        $this->set_header_image( compact( 'url', 'attachment_id', 'width', 'height' ) );

        // Cleanup.         $medium = str_replace( wp_basename( $original ), 'midsize-' . wp_basename( $original )$original );
        if ( file_exists( $medium ) ) {
            wp_delete_file( $medium );
        }

        if ( empty( $_POST['create-new-attachment'] ) && empty( $_POST['skip-cropping'] ) ) {
            wp_delete_file( $original );
        }

        return $this->finished();
    }

    /** * Displays last step of custom header image page. * * @since 2.1.0 */
 else {
        // If there's no filename or full path stored, create a new file.         $archive_filename = $file_basename . '.zip';
        $archive_pathname = $exports_dir . $archive_filename;

        update_post_meta( $request_id, '_export_file_name', $archive_filename );
    }

    $archive_url = $exports_url . $archive_filename;

    if ( ! empty( $archive_pathname ) && file_exists( $archive_pathname ) ) {
        wp_delete_file( $archive_pathname );
    }

    $zip = new ZipArchive();
    if ( true === $zip->open( $archive_pathname, ZipArchive::CREATE ) ) {
        if ( ! $zip->addFile( $json_report_pathname, 'export.json' ) ) {
            $error = __( 'Unable to archive the personal data export file (JSON format).' );
        }

        if ( ! $zip->addFile( $html_report_pathname, 'index.html' ) ) {
            $error = __( 'Unable to archive the personal data export file (HTML format).' );
        }

        


    switch ( $context ) {
        case 'site-icon':
            require_once ABSPATH . 'wp-admin/includes/class-wp-site-icon.php';
            $wp_site_icon = new WP_Site_Icon();

            // Skip creating a new attachment if the attachment is a Site Icon.             if ( get_post_meta( $attachment_id, '_wp_attachment_context', true ) == $context ) {

                // Delete the temporary cropped file, we don't need it.                 wp_delete_file( $cropped );

                // Additional sizes in wp_prepare_attachment_for_js().                 add_filter( 'image_size_names_choose', array( $wp_site_icon, 'additional_sizes' ) );
                break;
            }

            /** This filter is documented in wp-admin/includes/class-custom-image-header.php */
            $cropped    = apply_filters( 'wp_create_file_in_uploads', $cropped$attachment_id ); // For replication.             $attachment = $wp_site_icon->create_attachment_object( $cropped$attachment_id );
            unset( $attachment['ID'] );

            
$real_file = wp_normalize_path( $real_file );
    }

    if ( false !== $real_directory ) {
        $real_directory = wp_normalize_path( $real_directory );
    }

    if ( false === $real_file || false === $real_directory || ! str_starts_with( $real_filetrailingslashit( $real_directory ) ) ) {
        return false;
    }

    wp_delete_file( $file );

    return true;
}

/** * Outputs a small JS snippet on preview tabs/windows to remove `window.name` on unload. * * This prevents reusing the same tab for a preview when the user has navigated away. * * @since 4.3.0 * * @global WP_Post $post Global post object. */
Home | Imprint | This part of the site doesn't use cookies.