I wrote the other day about how I ran a Wikimate bot over the WordPress Codex to create 1700-some-odd redirect pages. So now the Codex is directly searchable, which is great for my purposes. But the problem is getting the overworked site administrators at wordpress.org to put in a new search box. I wrote to the wp-docs mailing list, and basically got the response, “Yeah, I’ll get around to it if I feel like it. Its not really a priority now.”

Luckily, there are other ways to make searches possible. I use duckduckgo.com for most of my code search, because of the convenience of being able to use various site’s internal search engines where those are useful. If you haven’t used it before, check it out: visit duckduckgo.com and search for something like !php array_map, or !jquery replacewith, and compare the results you get with a typical Google search.

So I submitted the WordPress Codex to Duck Duck Go! (there’s a submit link at the bottom of this page), and lo and behold, a day later, it was live and accessible.

Try searching for any WordPress function there: !codex wp_query, !codex wp_list_pluck… anything. Sweet, huh?

So I got this bug in my head a while back that there really needed to be a better lookup function for WordPress code information. Maybe its true that everyone who knows how to just greps the source, and the Codex search is only for beginners who don’t care if it takes a while to find what they want. I personally don’t believe that. Yes, looking through the source with ack-grep (or having your IDE do it for you) is going to be the quickest way to find what a given function does, or what order the arguments it takes should go in.

But there’s a few weaknesses to depending on the inline documentation. First, I already mentioned the problem of beginners who may be perfectly capable of putting together a theme, but are intimidated from searching through the source. Second, there are some classes in WordPress that are so complex that even with very good inline documentation, its much easier to follow a Codex entry than to view the source. Compare, for example, the Codex entry for WP_List_Table with the source.

Basic issue, so many people put so much effort into improving the Codex documentation, and so many questions are asked and answered on support forums that could be solved by simply searching the Codex, that I figured making the Codex more searchable would be a win for all involved.

So… the process

First step was to get a list of the pages on the Codex where redirects would be useful. I looked at exporting the Codex to work with locally, as per Eric Mann’s answer here on Stack Exchange. But that seemed like too much overhead for me. I didn’t need page text or revision history, just the page title was enough for me. So, I went with a simpler scraper on scraperwiki which pulled the page titles of every public entry in the Codex.

Running this function over http://codex.wordpress.org/index.php?title=Special:AllPages takes a few minutes, and gives me 4117 pages. Very nice. Now, a lot of these pages are in foreign languages which I don’t want to mess with. Actually, this stuff is not in any real order, so I decided not to try to handle everything with one function. Its probably safer to deal with the “Function Reference” entries separately from the “Template Tags” or “Plugin API” entries. So, I’m searching for titles that contain “Function Reference” and don’t contain a colon, as in es:Function Reference/wp_blahblahblah. Here’s my first stab at getting just the articles that need to be redirected:


Query: select * from `codex_pages` where title like “%Function Reference/%” and title not like “%:%”

That seems workable enough for my purposes. Its possible that I’ve missed some, but this is really just a first pass… There is still a lot of hand-tuning necessary to fix the entries whose titles don’t translate well into Mediawiki’s naming formats (like __(), for one). But creating 1000 good redirects is still useful.

For those who are interested, here’s how I ran the bot. I used Wikimate, an unofficial PHP SDK for mediawiki.

login($username,$password))) {
	$error = $wiki->getError();
	var_dump( $error );
	exit;
}

$x = file_get_contents( 'https://api.scraperwiki.com/api/1.0/datastore/sqlite?format=jsondict&name=wp_codex&query=select%20*%20from%20%60codex_pages%60%20where%20title%20like%20%22%25Function%20Reference%2F%25%22%20and%20title%20not%20like%20%22%25%3A%25%22%20' );
$pages = json_decode( $x );

foreach ( $pages as $i => $page ) {

	$title = trim( $page->title, '/' );
	$href = trim( $page->href, '/' );

	$function_name = end( explode( '/', $title ) );
	$function_href = end( explode( '/', $href ) );

	if ( $function_name === $title || strstr( $title, '$' ) )
		continue;

	// check if the page exists or not

	$create_page = $wiki->getPage( strtolower( $function_name ) );
	if ( $create_page->exists() ) {
		echo "E\t$function_name\t$function_href\n";
		continue 1; 
	}

	// Create redirect page

	if (!$error = $create_page->setText( '#REDIRECT [['.$page->title.']]' ) ) {
		var_dump( $create_page->getError() );
	} else {
		echo "N\t$function_name\t$function_href > $href\n";
	}

}

So… does it work? Let’s see:



<form action="http://codex.wordpress.org/">
	<input type="text" name="search" />
	<input type="submit" value="Search" />
</form>

Try searching for a function name. You should get directed straight to the relevant Codex page, if one exists.

As I’ve been mentioning a lot lately, I’ve been using the BackPress library in more and more projects as time goes on. I’m playing around a bit these days with Django and CodeIgniter, both of which I really like, but my mind hasn’t really shifted to the MVC model yet, and when I need to get something done quickly, I find myself starting it up in BackPress.

I gave an Ignite talk on BackPress last weekend at WordCamp Seattle. It was fairly disjointed, and I’m a long way from being comfortable with speaking to a group that size, but here are my slides, for anyone who wants to be introduced to this library:
— Read more —

I recently answered a support question on one of the WordPress question and answer sites about forcing pdf links to download in Acrobat or another pdf application, rather than opening in the browser window. I came up with what I thought was a pretty elegant solution, which involved creating a pdf.php template which set the HTTP headers necessary to force a download, rather than allowing the browser to attempt to render the file.

— Read more —

Spent the day learning a little more about the walker class that handles WordPress 3.x’s nav_menu structure. I can’t say it was easy. Having to extend Walker_Nav_Menu and overwrite the instance methods just to do something as simple as adding a <span> element within a list item seems a little counterintuitive. But for now, this is the best and most extensible way of handling menus that the user may need to update, so it seems worth knowing about.

— Read more —

I was recently working on a WordPress project that required a specialized front-end panel for users to add new posts, including images and videos. Adding a post wasn’t a challenge, but handling file uploads was looking like a pain… at least trying to follow the codex documentation. After browsing the wp-includes core files for a few hours, though, I found a much easier way of handling it.

— Read more —

I recently got my first WordPress plugin indexed in the wordpress.org repository. It’s a piddling little widget, nothing too impressive, but the process itself was very satisfying, and one that got me appreciating in a much more direct way all the work that all the thousands of contributors put into this open source project.

— Read more —