Updated WordPress Documentation API


I just updated the Documentation API and created a more lightweight design at http://lookup.hitchhackerguide.com/ in order to create a better integration in IDEs such as Coda. This lookup tool now also will give you some results for native PHP functions.

WP-Cron-Control lets you take control over wp-cron execution

Did you ever wonder when your scheduled wp-cron tasks get executed or ran into trouble when your scheduled posts did not get published?

If that’s the case then you might want to take wp-cron-control for a spin.

This small plugin allows you to disable the normal wp-cron processing that is usually hooked into the sanitize_comment_cookies filter and setup a regular system cron job that calls wp-cron.php with a customizable secret string instead.

Initialize WordPress from a PHP CLI script

I sometimes run scripts from the command line and still need WordPress functionality and the regular WordPress environment. Here’s a very basic script that I usually use as a structure for this kind of actions.

<?php
/** 
 * Dummy CLI script that loads the WordPress environment
 * Author: Thorsten Ott 
 * Author URI: http://hitchhackerguide.com
 */
set_time_limit( 0 );
ini_set( "memory_limit", "64M" );
$_SERVER['HTTP_HOST'] = 'wp_trunk'; // set this to the apache vhost name of your WordPress install

ob_start();
require_once( dirname( __FILE__ ) . '/wp-load.php' ); // you need to adjust this to your path
require_once( ABSPATH . 'wp-admin/includes/admin.php' );
ob_end_clean();

$cli_script = new WordPress_CLI;
$cli_script->set_required_arg( 'blog_id', 'blog_id of blog to use' );
$cli_script->set_required_arg( 'action', 'what shall I do' );

$cli_script->set_argument_validation( '#^blog_id$#', 'cli_init_blog', 'blog_id invalid' );
$cli_script->init();

abstract class WordPress_CLI_Factory {
	public $args;
	private $validate_args = array();
	private $required_args = array();
	
	public function __construct() {
		$this->args = $this->get_cli_arguments();
	}
	
	public function init() {
		if ( !$this->validate_args() )
			exit;
		$this->dispatch();
	}

	abstract function dispatch();
	
	public function set_required_arg( $name, $description='' ) {
		$this->required_args[$name] = $description;
	}
	
	public function set_argument_validation( $name_match, $value_match, $description='argument validation error' ) {
		$this->validate_args[] = array( 'name_match' => $name_match, 'value_match' => $value_match, 'description' => $description );
	}

	private function validate_args() {
		$result = true;
		if ( empty( $this->args ) && !empty( $this->required_args ) ) {
			$this->show_help();
			$result = false;
		} else { 
			foreach( $this->required_args as $name => $description ) {
				if ( !isset( $this->args->$name) ) {
					$this->raise_required_argument_error( $name, $description );
					$result = false;
				}
			}
		}
		foreach( $this->validate_args as $validator ) {
			foreach( $this->args as $name => $value ) {
				$name_match_result = preg_match( $validator['name_match'], $name );
				if ( ! $name_match_result ) {
					continue;
				} else {
					$value_match_result = $this->dispatch_argument_validator( $validator['value_match'], $value );
					if ( ! $value_match_result ) {
						$this->raise_argument_error( $name, $value, $validator );
						$result = false;
						continue;
					}
				}
			}
		}
		
		return $result;
	}
	
	private function dispatch_argument_validator( $match, $value ) {
		$match_result = false;
		if ( is_callable( array( &$this, $match ) ) ) {
			$_match_result = call_user_func( array( &$this, $match ), $value );
		} else if ( is_callable( $match ) ) {
			$_match_result = call_user_func( $match, $value );
		} else {
			$_match_result = preg_match( $match, $value );
		}
		return $_match_result;
	}
	
	private function raise_argument_error( $name, $value, $validator ) {
		printf( "Validation of %s with value %s failed: %s\n", $name, $value, $validator['description'] );
	}
	
	private function raise_required_argument_error( $name, $description ) {
		printf( "Argument %s is required: %s\n", $name, $description );
	}
	
	private function show_help() {
		printf( "Please provide the following arguments: %s\n", print_r( $this->required_args, true ) );
	}
	private function cli_init_blog( $value ) {
		$blog_address = get_blogaddress_by_id( (int) $value );
		if ( $blog_address == 'http://' )
			return false;
		switch_to_blog( (int) $value );
		return true;
	}
	
	private function get_cli_arguments() {
		$_ARG = new StdClass;
		$argv = $_SERVER['argv'];
		array_shift( $argv );
		foreach ( $argv as $arg ) {
			if ( preg_match( '#--([^=]+)=(.*)#', $arg, $reg ) ) 
				$_ARG->$reg[1] = $reg[2];
			elseif( preg_match( '#-([a-zA-Z0-9])#', $arg, $reg ) ) 
				$_ARG->$reg[1] = 'true';
		}
		return $_ARG;
	}

}

class WordPress_CLI extends WordPress_CLI_Factory {
	
	public function dispatch() {
		print_r( get_bloginfo() );
	}

}

WordPress 3.2 (Gershwin) and Hitchhackerguide update

One day after the Gershwin release (3.2) of WordPress was announced we updated our documentation and the WordPress Mode for Coda to reflect over sixty new functions since the 3.1 release along with the updated functions.

The focus for this release was making WordPress faster and lighter,  it comes with  the fully HTML5 new Twenty Eleven theme and an improved full-screen button in the editing toolbar which lets you enter the new distraction-free writing or zen mode, where the widgets, menus, buttons, and interface elements get out of your way for a Zen like editing experience.

__clear_multi_author_cache

Definition:
function __clear_multi_author_cache() {}

Helper function to clear the cache for number of authors.

Source code

function __clear_multi_author_cache() {

	wp_cache_delete('is_multi_author', 'posts');

}

15486