Archive for coding

Forcing WordPress to use HTTP_HOST

At times, it is convenient to force WordPress to use the hostname of the current virtual host context. By default, new versions of wordpress rely on DB configured values for siteurl, home, and url. If for instance, you run multiple codebases for testing, it might be convenient to not have it force the values to the configured ones. To accomplish this, add the following code to a custom wp plugin or just add it to the wp-config.php at the very bottom.



function set_codebase()
{
return 
'http://' $_SERVER['HTTP_HOST'];
}
remove_action('template_redirect''redirect_canonical');
add_filter 'pre_option_siteurl''set_codebase' );
add_filter 'pre_option_home''set_codebase' );
add_filter 'pre_option_url''set_codebase' );


This code can further be wrapped in a conditional so it only gets executed under explicit circumstances.

Using strtotime(…) in PHP to calculate the last day of the month



$unixtime 
strtotime(strftime('%Y-%m-01 - 1 second',
strtotime('next month')));

VIM Not Restoring Position

For some reason, vim stopped recording the last position in the files I was editting. I think it is related to a recent upgrade. Regardless, adding these lines to my ~/.vimrc restored the functionality



set viminfo
='10,\\"100,:20,%,n~/.viminfo
    au BufReadPost * if line("'
\\"") > 0|if line("'\\""

Robust PHP MogileFS Client

We’ve just released our MogileFS Client for PHP (5.1). It does a very good job of handling errors and is being used in production on Socialverse.

You might also want to consider this PECL module that implements the protocol. Personally, I don’t see many performance advantages to using this module. The native PHP MogileFS class above uses CURL for almost everything, which is an efficient library for HTTP communication all in C. CURL also comes standard with almost every PHP packing system that I’ve seen, so you don’t need to maintain yet another PECL.

PHP Exception Throwing Eval



  
function eeval($string)
  {
    $result = @eval($string);
    if( isset($php_errormsg) )
      throw new Exception($php_errormsg);
    return $result;
  }

PHP CURL PUT String

In the process of writing a MogileFS client in PHP, I discovered there’s no no straight forward way to issue a PUT request using CURL where the body of the request comes from a string, rather than a file. Luckily, PHP 5.2 introduces memory based streams…

 

   $fh 
fopen('php://memory''rw');
    fwrite($fh$dataToPut);
    rewind($fh);
   $ch curl_init();
    curl_setopt($chCURLOPT_INFILE$fh);
    curl_setopt($chCURLOPT_INFILESIZE$length);
    curl_setopt($chCURLOPT_TIMEOUT10);
    curl_setopt($chCURLOPT_PUT4);
    curl_setopt($chCURLOPT_URL$url);
    curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
    curl_setopt($chCURLOPT_HTTPHEADER, Array('Expect: '));
    $response curl_exec($ch);
    fclose($fh);

PHP set_error_handler wierdism with $this

Note, if you are setting an error handler for a class method within your own object, such as
Array( $this'error_handler')
you will effectively make your current object global in scope. Thus, it will only pass out of scope when the script exits or the object manually unset. As a result the deconstructor of your object will not be called. This can be a problem if in your deconstructor you call restore_error_handler.