How I became a programmer (thank you, Captain Kirk)

With JJ Abrams rocking our worlds with new Star Trek energy, I thought I would give thanks to the Trek franchise for helping me find my vocational path.

Read More »

Posted in General Interest | Tagged , , , , , | 1 Comment

One-button website publishing using WordPress-mu

I’m kinda loving on WordPress MU. One of my recent projects required building web sites that pulled from a shared database. WordPress MU allowed me to create a one-button website builder for my client. WordPress MU calls them blogs, but my client will map a unique domain to each blog, and, well, doesn’t use them as blogs at all. So I’m calling them sites here.

By filling in three fields and clicking a button, my client creates a website that

  • Associates metadata with the site that specifies filter criteria to select records from the shared database
  • Sets the permalink structure for the new site to a custom setting
  • Creates a key category for the new site, one that is used for the posts generated in a later step
  • Sets the theme
  • All the initial pages are created, including content. The “slug” is set specifically to support the URL structure we want. Page template is also set here because our design calls for a hierarchy of pages.
  • If the PageMash plugin is active (it is auto-activated for all new blogs using Plugin Commander), certain pages are hidden, and a specific order is set so that page navigation comes out looking good
  • The front page is set, because we are creating CMS sites, not blogs
  • Several hundred posts are generated out of the underlying shared non-WPMU database tables

All of this takes something like 20 seconds, at which point the admin can visit the new site as a subdomain.  The theme has been applied, navigation works correctly, it is beauty.

Read More »

Posted in Development, wordpress, wordpress-mu | Tagged , , , , , | 1 Comment

How to get your command line PHP script working with WordPress-MU 2.7

OK, this one is obscure. I only post about what I can’t find on Google!

Until recently it has been possible to run command-line PHP scripts that include wp-config.php or wpmu-settings.php, in order to give those scripts access to WordPress globals like $wpdb, functions such as wp_insert_post etc.

These scripts worked with WordPress-MU until recently — now the scripts just exit with no output.

The reason? WPMU now initiates a redirect in the bootstrap process (search for ‘header( “Location: “‘ in wpmu-settings.php). A browser can follow that redirection, but a command line script cannot. So, if you want to initiate scripts that run from the command line, you will have to get the web server involved by invoking a text browser such as curl, wget, or links.

It’s best not to force your scripts to figure out where to find wp-config.php anyway. I have taken to using a method suggested in this thread, as shown in the code sample below. The good news is, you know that your script will work the same whether initiated from a plugin in a graphic browser, used in an Ajax call, or initiated from cron or the command line shell.

So instead of kicking your script off with something like

/usr/bin/php -q myscript.php

You will create the script as a proper plugin and do something like

/usr/bin/curl -d mypp_cmd=status http://mywpmusite.com

Sample plugin code used for the above example follows. The prefix for the functions is ‘mypp’, which of course stands for ‘MY Plugin Prefix’ to create a unique namespace. The code below returns results as JSON encoded, and calls die()/exit() at the end to prevent an entire page being created. For log files run from cron, you may choose to return plain text instead.

  /**
   * Add a query var for this plugin
   * This allows Ajax programs to operate without requiring file paths
   * Instead, Ajax functions look for the query var 'mypp_cmd'
   */
  function mypp_query_vars($qvars) {
    $qvars[] = 'mypp_cmd';
    return $qvars;
  } // function mypp_query_vars
 
  /**
   * Handle AJAX requests in the template_redirect action
   * We recognize our requests from the query var 'mypp_cmd'
   * Here we allow GET requests using $_REQUEST; to restrict to POST, use $_POST instead
   */
  function mypp_template_redirect() {
    global $wpdb;
    $cmd = get_query_var('mypp_cmd');
    $response = array();
    if ($cmd) {
      switch($cmd) {
      case 'status':
	$response['status'] = array('success' => true,
				    'message' => 'Sample status message');
	break;
      default:
	$response['status'] = array('success' => false,
				    'msg' => 'Unknown command',
				    'cmd' => $cmd);
	break;
      } // switch $cmd
      header('Content-type: text/x-json; charset=utf-8');
      print utf8_encode(json_encode($response));
      die();
    } // $cmd is set
  } // function mypp_template_redirect
 
add_filter('query_vars', array($this, 'mypp_query_vars'));
add_action('template_redirect', array($this, 'mypp_template_redirect'));
Posted in Development, wordpress, wordpress-mu | Tagged , , , , , , | Leave a comment

How to change the number of posts shown in the WordPress admin area without hacking the core

use the hook!

use the hook!

It is easy to go in and hack the core WordPress files to change the hard-coded number, but I wanted to find a way to do it with a hook.  That way, I could one day change the number of posts shown using controls in the interface.

Though there is no filter provided for the number of posts displayed, I hit upon a method of doing this which I would like to share.  Perhaps someone will point out a better way, but for now this is making me happy

The trick is to hijack the query string just for the query that produces the table of posts.  Here’s what I did:

/**  * Extend the number of posts displayed in the Edit Posts  */ 
function dapl_query_string($query_string) {   
    global $pagenow;   
    if (is_admin() && $pagenow == 'edit.php') {
        $query_string = str_replace('posts_per_page=15', 'posts_per_page=100', $query_string); 
    }
    return $query_string; 
}  
add_filter('query_string', 'dapl_query_string');
Posted in Development, wordpress | Tagged , , , , , | 4 Comments