Stuff & Nonsense

WP Akismet plugin behind proxy

The new HTTP Api in wordpress 2.9 is fantastic, it standardises http requests and allows you to define a proxy through which all http requests are routed.  Useful if, like me, you run a wordpress server on a corporate network.

You can configure your proxy server by adding the following lines to your ‘wp-config.php’

define('WP_PROXY_HOST', '192.168.250.1');
define('WP_PROXY_PORT', '800');
define('WP_USEPROXY','TRUE');

However there’s currently one problem with the default installation of wordpress when it comes to proxy support. Akismet (the spam filtering plugin) Doesn’t work behind the proxy. No matter what you put in for your proxy settings you’ll be told ‘unable to connect’.

The reason is that Akismet uses a ‘raw’ socket connection to do its http requests, rather than the spiffy new API. So, here’s how to fix it:

you need to edit the file ‘/wp-content/plugins/akismet/akismet.php’.

search for the function ‘akismet_http_post’ and replace the entire function (you can just rename the old function) with my newly crafted one:

function akismet_http_post($request, $host, $path, $port = 80, $ip=null){
        global $wp_version;
	$akismet_version = constant('AKISMET_VERSION');
        $args = array(
             'method'=>'POST',
             'user-agent'=>"User-Agent: WordPress/$wp_version | Akismet/$akismet_version",
             'body'=>$request
        );
         $url = "http://".$host.$path;

         if( !class_exists( 'WP_Http' ) )
         include_once( ABSPATH . WPINC. '/class-http.php' );
	 $http_request = new WP_Http;
         $http_response = $http_request->request($url,$args);
         if( is_wp_error( $http_response ) )
              return;
         $response[0] = $http_response['headers'];
         $response[1] = $http_response['body'];
	return $response;
}

And voila, working spam detection behind a proxy.

4 thoughts on “WP Akismet plugin behind proxy

  1. Thanks for that but could you be clear on which part to amend? Is it only the above section or does one include the section commented as “// use the WP HTTP class if it is available ” as well? If not, then in WP3.3.1 and Akismet current at Feb 2012, this no longer works. My host is taking no responsibility for anything server side so if there is an update to this that would be much appreciated. Many thanks.

    1. I would imagine that the section about using the WP-HTTP class if it’s available means that the Askimet plugin was updated to use the wordpress HTTP class, so this workaround is no longer required.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.