New Plugin: WordPress.org One-Click Install

The new button that will show up on WordPress.org plugin pages

The new button that will show up on WordPress.org plugin pages

If you’ve ever been sent a link to a plugin hosted on WordPress.org and wanted to install that plugin, you currently have two options:

  1. Download the plugin manually, upload it to your blog either via the admin interface or via FTP, and then activate it.
  2. Visit your admin area and find the plugin again via the Add New Plugin interface.

Both options are annoying, especially since you’re already staring at the plugin you want to install.

Well now there’s a much easier solution! Simply install this plugin to your blog and install it’s Greasemonkey script from the plugin’s page and you’re all set. You will then be able to literally one-click install any plugin directly from WordPress.org.

For additional details and a download link, check out the plugin’s homepage.

WordPress How-To: Force Direct Filewrites For Upgrades

Starting with WordPress 2.8, there is an easier way to do this. See the bottom of this post for an explanation of the easier method. You should still read this post in it’s entirety though so that you understand what’s going on.

Also 777 may not be required for files (folders probably still need 777). 666 may work instead and would be slightly more secure.

In order for WordPress to be able to upgrade your plugins and the WordPress core without any modifications, at least one of four situations needs be true:

  1. Your server runs Windows: file permissions are less restrictive under Windows, so PHP can directly write to and replace the existing plugin and core files.
  2. PHP (technically your HTTPD) is the owner of the files: with some hosting companies, such as MediaTemple, Apache/PHP runs as the same user as you. Files uploaded by you and files created by PHP (for example, uploads) are owned by the same user. That means PHP can write to or edit any file on the server.
  3. You have FTP access to your server: WordPress comes built in with a library allowing the server to connect to itself via FTP to replace files. File permissions don’t really matter in this case.
  4. Your version of PHP has been compiled with the SSH module: this is rare and only done on dedicated servers really. It also makes it somewhat of a pain to upgrade PHP.

Unfortunately, none of those situations applied to me and my server. I couldn’t do direct writes as I run Linux and PHP runs as it’s own user, FTP was disabled for security (I manage my server via SSH), and recompiling PHP is a major pain in the ass. So what to do? Enter a little bit of code, care of DD32. This code either goes in a plugin (that’s what I did) or your theme’s functions.php file (but if you switch themes, this “hack” will go away).

Please note that this code should only be used on a well managed, secure dedicated server where you keep all scripts (including WordPress!) up to date. If a hacker manages to gain access to a script on your server, for example via an insecure script, then they can literally delete or modify all of your files. Don’t use this code unless you know what you are doing!

The first thing to do is to tell WordPress to use the “direct write” method, which is used in situations 1 and 2 above. This is done via a filter:

add_filter( 'filesystem_method', create_function( '$a', 'return "direct";' ) );

The usage of create_function() is just a quick way of doing it. You could create a real function if you wished.

The second thing to do is to tell WordPress what permissions to use on any files and folders it creates. The permissions need to be 0777 (or similar) so that you can still modify the files/folders if need be. That is done via two constants:

define( 'FS_CHMOD_DIR', 0777 );
define( 'FS_CHMOD_FILE', 0777 );

Put that code altogether for a plugin and it’ll look something like this:

<?php /*

**************************************************************************

Plugin Name:  Force Direct Filewrites
Plugin URI:   http://www.viper007bond.com/2009/05/07/wordpress-how-to-force-direct-filewrites-for-upgrades/
Version:      2009.05.01
Description:  Forces the "direct" filesystem method to be used and tells WordPress what permissions to use.
Author:       Viper007Bond
Author URI:   http://www.viper007bond.com/

**************************************************************************/

add_filter( 'filesystem_method', create_function( '$a', 'return "direct";' ) );

define( 'FS_CHMOD_DIR', 0777 );
define( 'FS_CHMOD_FILE', 0777 );

?>

Lastly, you need to adjust the permissions on any existing plugins so that PHP can modify them. You need to chmod your /wp-content/plugins/ folder and everything inside of it to 0777 or similar (767 may work, or even 757). If you wish to be able to one-click upgrade your WordPress install as well, then you will need to make your entire WordPress folder writable as well. Please first read this warning about the dangers of 777 though.

And that’s it! Now when you go to do a plugin upgrade, WordPress will directly replace the files. No having to wait on the slow FTP method.

Update For WordPress 2.8+

Starting with WordPress 2.8, you no longer need a plugin or anything as the filesystem method can be controlled by a constant called FS_METHOD.

In short, all you need to do is to add the following 3 lines somewhere near the top of your wp-config.php file:

define( 'FS_METHOD', 'direct' );
define( 'FS_CHMOD_DIR', 0777 );
define( 'FS_CHMOD_FILE', 0777 );

Plugin Update: SyntaxHighlighter Evolved v2.1.0

I’ve updated my SyntaxHighlighter Evolved plugin to v2.1.0. It incorporates changes made to Alex Gorbatchev’s script. Here are the relevant changes with my comments in italics:

  • Major speed improvement thanks to a patch by Jose Prado. SyntaxHighlighter now can render 4-5k lines in just a second or two (tested on a MacBook Pro 2.4GHz).
  • Added wrap-lines parameter option to disable line wrapping. See demo. (This can be accessed in my plugin via the “wraplines” parameter.)
  • Toolbar is now activated on mouse over.
  • Added ActionScript3 brush (thanks to Peter Atoria).
  • Added JavaFX brush (thank to Patrick Webster).
  • Added PowerShell brush (thanks to B.v.Zanten, Getronics)

An update notice should appear in your admin area soon.

WordPress Code Snippet: Output Advanced Page Generation Statistics

Here’s a little bit of code to display the percent of a page’s generation time used by PHP and MySQL.

First, add this into your theme’s functions.php file wp-config.php file so that data about each MySQL query is stored, namely the time taken.

define( 'SAVEQUERIES', true );

Then in your footer.php, use this code to display it:

<?php

global $wpdb;

// Get the total page generation time
$totaltime = timer_stop( false, 22 );

// Calculate the time spent on MySQL queries by adding up the time spent on each query
$mysqltime = 0;
foreach ( $wpdb->queries as $query )
	$mysqltime = $mysqltime + $query[1];

// The time spent on PHP is the remainder
$phptime = $totaltime - $mysqltime;

// Create the percentages
$mysqlper = number_format_i18n( $mysqltime / $totaltime * 100, 2 );
$phpper   = number_format_i18n( $phptime / $totaltime * 100, 2 );

// Output the stats
echo 'Page generated in ' . number_format_i18n( $totaltime, 5 ) . " seconds ( {$phpper}% PHP, {$mysqlper}% MySQL )";

?>

That will output code something like this: Page generated in 0.24691 seconds ( 95.50% PHP, 4.50% MySQL )