Prevent WordPress from updating via FTP

Sometimes, you will have your WordPress installation on a server and you will not have the possibility to change the owner of the folders. Even if you change the writing and execution rights (chmod), if www-data is not the owner of the folder, you could have some issues updating automatically WordPress and your plugins. If your in this case, WordPress will probably ask you for the FTP credentials, and even with them, it could not managed to update.

Chmod rights

At first, you could think to change all your folders and files permissions to 775 instead of 755 and 644. For me, it didn’t worked. And even if it works, you would need to change them before and after each update, in other case you would have security issues.

WordPress FS_METHOD configuration constant

WordPress has a constant available to force to update without using FTP connection, but direct file I/O requests instead. You will need to add this constant to your wp-config.php file (at the end of file for example) with the value ‘direct‘.

As it is mentioned on the WordPress Codex, it’s sometimes due to the PHP extension and the configuration of the server :

Certain PHP installations shipped with a PHP FTP extension which is incompatible with certain FTP servers. Under these rare situations, you may need to define FS_METHOD to “ftpsockets”.

And here is the full documentation of the FS_method constant in the Codex :

FS_METHOD forces the filesystem method. It should only be “direct”, “ssh2”, “ftpext”, or “ftpsockets”. Generally, you should only change this if you are experiencing update problems. If you change it and it doesn’t help, change it back/remove it. Under most circumstances, setting it to ‘ftpsockets’ will work if the automatically chosen method does not. Note that your selection here has serious security implications. If you are not familiar with them, you should seek help before making a change.

  • (Primary Preference) “direct” forces it to use Direct File I/O requests from within PHP. It is the option chosen by default.
  • (Secondary Preference) “ssh2” is to force the usage of the SSH PHP Extension if installed
  • (3rd Preference) “ftpext” is to force the usage of the FTP PHP Extension for FTP Access, and finally
  • (4th Preference) “ftpsockets” utilises the PHP Sockets Class for FTP Access.

Source: Hongkiat