validate_file_to_edit() WordPress Function

The validate_file_to_edit() function is used to check if a file can be edited by the user. This is useful for making sure that a user can only edit files that they have permission to edit.

validate_file_to_edit( string $file, string[] $allowed_files = array() ) #

Makes sure that the file that was requested to be edited is allowed to be edited.


Description

Function will die if you are not allowed to edit the file.


Top ↑

Parameters

$file

(string)(Required)File the user is attempting to edit.

$allowed_files

(string[])(Optional) Array of allowed files to edit. $file must match an entry exactly.

Default value: array()


Top ↑

Return

(string|void) Returns the file name on success, dies on failure.


Top ↑

Source

File: wp-admin/includes/file.php

function validate_file_to_edit( $file, $allowed_files = array() ) {
	$code = validate_file( $file, $allowed_files );

	if ( ! $code ) {
		return $file;
	}

	switch ( $code ) {
		case 1:
			wp_die( __( 'Sorry, that file cannot be edited.' ) );

			// case 2 :
			// wp_die( __('Sorry, cannot call files with their real path.' ));

		case 3:
			wp_die( __( 'Sorry, that file cannot be edited.' ) );
	}
}


Top ↑

Changelog

Changelog
VersionDescription
1.5.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.