Skip to content
Eduardo Lira

WORDPRESS · INFRAESTRUTURA

Why WordPress asks for FTP to install a plugin, and what FS_METHOD changes

In 2012 I showed how to store the FTP details in wp-config.php or use FS_METHOD direct. Both work, but the FTP screen isn't about 755 permissions: it's about who owns the files.

In September 2012 I wrote here about that screen that shows up when installing a theme or plugin in WordPress, asking for an FTP host, username and password. Plenty of people called their hosting company just to ask for the FTP address. The post had two ways to make the screen go away, and both still work. What it lacked was an explanation of why it shows up.

Why WordPress asks for FTP

Before installing or updating anything, WordPress tests whether it can write to its own files. It creates a temporary file and compares the owner of that file with the owner of the WordPress files. If they are the same user, it writes directly. If they are different, it doesn’t trust direct writing and asks for FTP.

This happens when PHP runs as one user and the files belong to another. A classic case: Apache runs as www-data, and the files were uploaded by the account’s FTP user. On hosts that run PHP as the account’s own user (per-user PHP-FPM, suPHP), the owners match and the screen never appears.

That’s why changing permissions to 755 and 644 usually doesn’t help: WordPress’s question is about the owner, not the mode.

The two approaches I showed in 2012

The first stores the FTP details in wp-config.php:

define('FTP_HOST', 'ftp.seudominio.com.br');
define('FTP_USER', 'usuarioftp');
define('FTP_PASS', 'senhaftp');

The screen goes away because WordPress starts using those details on its own. I already warned that it wasn’t safe: anyone who can read wp-config.php gets the password. And plain FTP also sends that password unencrypted over the network.

The second tells WordPress to write directly, without testing:

define('FS_METHOD', 'direct');

What I would change today

Where the line goes. In 2012 I wrote to put FS_METHOD below require_once(ABSPATH . 'wp-settings.php'). The right place is before the comment /* That's all, stop editing! */, as the WordPress documentation asks (the end of that sentence changed between versions: in 2012 it was “Happy blogging”, today it is “Happy publishing”). After wp-settings.php, WordPress has already loaded everything, and several constants arrive too late.

The effect of direct. With different owners, direct only works if the PHP user can write to the folders, and the files WordPress creates will belong to that user, not to your FTP user. Later you may not be able to edit or delete those files over FTP. And leaving wp-content writable by the web server means a vulnerable plugin can also write PHP files there.

The alternative to plain FTP. If the host offers SSH, WordPress accepts FS_METHOD with the value ssh2 (it needs PHP’s ssh2 extension) and a key instead of a password, through the FTP_PUBKEY and FTP_PRIKEY constants. For encrypted FTP, there is the FTP_SSL constant.

In practice, if the screen shows up on shared hosting, the most useful question for support is which user PHP runs as on your account.

References

Who writes this

Eduardo Lira

Software developer in Rio de Janeiro. I started this blog in 2012, in technical support at a hosting company, writing down the fixes for the problems that came up every day. Today I work at Sofis Tecnologia, on critical healthcare systems and SQL Server database migrations.

Found a mistake in this post?

A command that no longer works, a broken link, wrong information: tell me what you saw. The message goes only to the author and is not shown on the site.