When you’re about to perform a filesystem operation, WordPress will check if you’re able to perform the changes before actually doing it.
This is defined on the get_filesystem_method()
function, from /wp-admin/includes/file.php.
What WordPress actually does is compare the file owner of the file itself and a temporal file created on the /wp-content/plugins folder.
$temp_file_name = $context . 'temp-write-test-' . str_replace( '.', '-', uniqid( '', true ) );
$temp_handle = @fopen( $temp_file_name, 'w' );
if ( $temp_handle ) {
// Attempt to determine the file owner of the WordPress files, and that of newly created files.
$wp_file_owner = false;
$temp_file_owner = false;
if ( function_exists( 'fileowner' ) ) {
$wp_file_owner = @fileowner( __FILE__ );
$temp_file_owner = @fileowner( $temp_file_name );
}
if ( false !== $wp_file_owner && $wp_file_owner === $temp_file_owner ) {
/*
* WordPress is creating files as the same owner as the WordPress files,
* this means it's safe to modify & create new files via PHP.
*/
$method = 'direct';
$GLOBALS['_wp_filesystem_direct_method'] = 'file_owner';
So, there it is. Next time that the FTP credentials screen shows up, this is what to check to quickly fix it.
You can check the full source file on Github for more context.