WordPress at a glance

wp_delete_file() WP 4.2.0

Delete a file. This function is a wrapper for PHP's unlink() function — it uses wp_delete_file filter before deleting a file.

If the filter returns empty value, nothing will be deleted. Otherwise, unlink() will be applied to the returned value.

Hooks in function
Return

Nothing (null).

Usage

wp_delete_file( $file );
$file(string) (required)
The path to the file to delete.

Examples

#1 Delete text.txt from the uploads directory

$upload_info = wp_get_upload_dir();
$file        = $upload_info['basedir'] . '/test.txt';

wp_delete_file( $file );

#2 Forbid the deletion of the files with text.txt name using a filter

So the above example will not work anymore.

add_filter( 'wp_delete_file', function ( $file ) {
	if ( 'test.txt' === basename( $file ) ) 
		return '';

	return $file;
} );

Code of wp delete file: wp-includes/functions.php WP 5.2.2

<?php
function wp_delete_file( $file ) {
	/**
	 * Filters the path of the file to delete.
	 *
	 * @since 2.1.0
	 *
	 * @param string $file Path to the file to delete.
	 */
	$delete = apply_filters( 'wp_delete_file', $file );
	if ( ! empty( $delete ) ) {
		@unlink( $delete );
	}
}

Related Functions

From tag: File system (create delete files folders)

No comments
    Hello, !     Log In . Register