keekerdc.com

essays and pithy thoughts

The definitive Wordpress auto-update no-FTP fix

in codesmith

A plugin or Wordpress itself flags you down about an update that just came available. With glee in your eyes and a grin on your face, you click ‘Update!’, only to be met with a form to jam in a bunch of FTP credentials. Rage ensues.

Your PHP and Apache configurations are correct.

Your file permissions are true.

Your available memory is righteous.

So what’s the problem?

For me, the problem hinged around the function get_filesystem_method sitting in /wp-admin/includes/:

function get_filesystem_method($args = array(), $context = false) {
    $method = defined('FS_METHOD') ? FS_METHOD : false; //Please ensure that this is either 'direct', 'ssh', 'ftpext' or 'ftpsockets'

    if ( ! $method && function_exists('getmyuid') && function_exists('fileowner') ){
        if ( !$context )
            $context = WP_CONTENT_DIR;
        $context = trailingslashit($context);
        $temp_file_name = $context . 'temp-write-test-' . time();
        $temp_handle = @fopen($temp_file_name, 'w');
        if ( $temp_handle ) {
            if ( getmyuid() == @fileowner($temp_file_name) )
                $method = 'direct';
            @fclose($temp_handle);
            @unlink($temp_file_name);
        }
    }

    if ( ! $method && isset($args['connection_type']) && 'ssh' == $args['connection_type'] && extension_loaded('ssh2') && function_exists('stream_get_contents') ) $method = 'ssh2';
    if ( ! $method && extension_loaded('ftp') ) $method = 'ftpext';
    if ( ! $method && ( extension_loaded('sockets') || function_exists('fsockopen') ) ) $method = 'ftpsockets'; //Sockets: Socket extension; PHP Mode: FSockopen / fwrite / fread
    return apply_filters('filesystem_method', $method, $args);
}

The trouble is that pesky getmyuid() function, which is used later down the line to figure out who you are and whether you have permissions to edit the file. This approach can be problematic in some Linux flavors (while working generally on Windows boxes), and it seems Ubuntu is one of them.

A hat tip is deserved here towards this blog, which finally provided me a usable workaround to this problem. Simply put, the approach here is to replace all references for getmyuid() with posix_getuid() instead. This works brilliantly between updates to Wordpress; however the fix is overwritten each and every time Wordpress pushes out a new update. I’ve had to revisit this a handful of times over the life of this blog thus far.

A more permanent solution was provided by my colleague @lavallee, who suggested I jam the following into wp_config.php:

[php]define(‘FS_METHOD’, ‘direct’);[/php]

Aaaah. Works great, uses the PHP filesystem methods directly instead of other monkey business, and no more having to muck around in include files after every Wordpress update.

More on what this does can be found here, just make sure you’ve got your stuff properly configured before using it.