<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Gregory Grubbs &#187; wordpress-mu</title>
	<atom:link href="http://gregorygrubbs.com/category/wordpress/wordpress-mu-wordpress/feed/" rel="self" type="application/rss+xml" />
	<link>http://gregorygrubbs.com</link>
	<description>Attention in software development</description>
	<lastBuildDate>Mon, 12 Jul 2010 17:35:34 +0000</lastBuildDate>
	
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>One-button website publishing using WordPress-mu</title>
		<link>http://gregorygrubbs.com/wordpress/one-button-website-publishing-using-wordpress-mu/</link>
		<comments>http://gregorygrubbs.com/wordpress/one-button-website-publishing-using-wordpress-mu/#comments</comments>
		<pubDate>Tue, 31 Mar 2009 00:50:48 +0000</pubDate>
		<dc:creator>Gregory Grubbs</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[wordpress-mu]]></category>
		<category><![CDATA[automated]]></category>
		<category><![CDATA[creation]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[website]]></category>

		<guid isPermaLink="false">http://gregorygrubbs.com/?p=174</guid>
		<description><![CDATA[
I&#8217;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, [...]]]></description>
			<content:encoded><![CDATA[<p><a title="Create many from the one" href="http://flickr.com/photos/63989735@N00/2431138923"><img src="http://farm3.static.flickr.com/2299/2431138923_ff1f7aaa36.jpg" alt="" /></a></p>
<p>I&#8217;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&#8217;t use them as blogs at all.  So I&#8217;m calling them sites here.</p>
<p>By filling in three fields and clicking a button, my client creates a website that</p>
<ul>
<li> Associates metadata with the site that specifies filter criteria to select records from the shared database</li>
<li> Sets the permalink structure for the new site to a custom setting</li>
<li> Creates a key category for the new site, one that is used for the posts generated in a later step</li>
<li> Sets the theme</li>
<li> All the initial pages are created, including content.  The &#8220;slug&#8221; 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.</li>
<li> 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</li>
<li> The front page is set, because we are creating CMS sites, not blogs</li>
<li>Several hundred posts are generated out of the underlying shared non-WPMU database tables</li>
</ul>
<p>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.</p>
<p><span id="more-174"></span></p>
<p>Here&#8217;s how it is done in code.  All you need to do is register a function for the action hook that WordPress MU runs when a new blog is created.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> mypp_initialize_blog<span style="color: #009900;">&#40;</span><span style="color: #000088;">$blog_id</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$wp_rewrite</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// first, switch to the new blog; we will undo this at the</span>
  <span style="color: #666666; font-style: italic;">// end of the function with restore_current_blog()</span>
  switch_to_blog<span style="color: #009900;">&#40;</span><span style="color: #000088;">$blog_id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;">// add a blog option, here a filter with a default value</span>
  add_option<span style="color: #009900;">&#40;</span><span style="color: #000088;">$blog_id</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'campus_selection_criteria'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;CampusStateID='CA'&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;">// Set a custom permalink structure</span>
  <span style="color: #000088;">$wp_rewrite</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>set_permalink_structure<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/%category%/%postname%'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$wp_rewrite</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>flush_rules<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">function_exists</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'wp_create_category'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      wp_create_category<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'schools'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// switch theme - this one specifies a child theme</span>
  switch_theme<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'corporate'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'tweaked-corporate'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;">// Prepare options used by the pageMash plugin, auto-activated for new sites</span>
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span>get_option<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'exclude_pages'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000088;">$excludePagesList</span><span style="color: #339933;">=</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">else</span>
    <span style="color: #000088;">$excludePagesList</span> <span style="color: #339933;">=</span> get_option<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'exclude_pages'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//if it's empty set as an empty array</span>
  <span style="color: #666666; font-style: italic;">// add pages to support the basic page structure</span>
  <span style="color: #666666; font-style: italic;">// NOTE: the full code is not shown, standard use of wp_insert post</span>
  <span style="color: #666666; font-style: italic;">// for each page:</span>
  <span style="color: #000088;">$newid</span> <span style="color: #339933;">=</span> wp_insert_post<span style="color: #009900;">&#40;</span><span style="color: #000088;">$postdata</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$newid</span> <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> <span style="color: #339933;">!</span>is_wp_error<span style="color: #009900;">&#40;</span><span style="color: #000088;">$newid</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    add_meta<span style="color: #009900;">&#40;</span><span style="color: #000088;">$newid</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> IWANTOEXCLUDETHISPARTICULARPAGEFROMNAVIGATION <span style="color: #009900;">&#41;</span>
      <span style="color: #000088;">$excludePagesList</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$newid</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>THISPARTICULARPAGE <span style="color: #339933;">==</span> <span style="color: #0000ff;">'home'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      update_option<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'show_on_front'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'page'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      update_option<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'page_on_front'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$newid</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// error handling if insert post failed</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #666666; font-style: italic;">// end for each page</span>
  update_option<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'exclude_pages'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$excludePagesList</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;">// now generate posts for schools from the shared database</span>
  createSchoolAsPost<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #000088;">$blog_id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;">// we're done! restore our initial blog</span>
  restore_current_blog<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// function mypp_initialize_blog</span>
&nbsp;
add_action<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'wpmu_new_blog'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'mypp_initialize_blog'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://gregorygrubbs.com/wordpress/one-button-website-publishing-using-wordpress-mu/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How to get your command line PHP script working with WordPress-MU 2.7</title>
		<link>http://gregorygrubbs.com/wordpress/how-to-get-your-command-line-php-script-working-with-wordpress-mu-27/</link>
		<comments>http://gregorygrubbs.com/wordpress/how-to-get-your-command-line-php-script-working-with-wordpress-mu-27/#comments</comments>
		<pubDate>Sun, 29 Mar 2009 16:47:23 +0000</pubDate>
		<dc:creator>Gregory Grubbs</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[wordpress-mu]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[command line]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[web server]]></category>
		<category><![CDATA[wpmu]]></category>

		<guid isPermaLink="false">http://gregorygrubbs.com/?p=160</guid>
		<description><![CDATA[
	OK, this one is obscure. I only post about what I can&#8217;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 [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://flickr.com/photos/86639396@N00/125838823" title="To the moon, Ajax!"><img src="http://farm1.static.flickr.com/48/125838823_e1a4d43a62_m.jpg" /></a></p>
<p>	OK, this one is obscure. I only post about what I can&#8217;t find on Google!</p>
<p>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.  </p>
<p>These scripts worked with WordPress-MU until recently &#8212; now the scripts just exit with no output.  </p>
<p>The reason?  WPMU now initiates a redirect in the bootstrap process (search for &#8216;header( &#8220;Location: &#8220;&#8216;  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.  </p>
<p>It&#8217;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 <a href="http://wordpress.org/support/topic/190522">this thread</a>, 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.</p>
<p>So instead of kicking your script off with something like</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>php <span style="color: #660033;">-q</span> myscript.php</pre></div></div>

<p>You will create the script as a proper plugin and do something like</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>curl <span style="color: #660033;">-d</span> <span style="color: #007800;">mypp_cmd</span>=status http:<span style="color: #000000; font-weight: bold;">//</span>mywpmusite.com</pre></div></div>

<p>Sample plugin code used for the above example follows.  The prefix for the functions is &#8216;mypp&#8217;, which of course stands for &#8216;MY Plugin Prefix&#8217; 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.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
   * 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'
   */</span>
  <span style="color: #000000; font-weight: bold;">function</span> mypp_query_vars<span style="color: #009900;">&#40;</span><span style="color: #000088;">$qvars</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$qvars</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'mypp_cmd'</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$qvars</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// function mypp_query_vars</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/**
   * 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
   */</span>
  <span style="color: #000000; font-weight: bold;">function</span> mypp_template_redirect<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$wpdb</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$cmd</span> <span style="color: #339933;">=</span> get_query_var<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'mypp_cmd'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$response</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$cmd</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">switch</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cmd</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'status'</span><span style="color: #339933;">:</span>
	<span style="color: #000088;">$response</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'status'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'success'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
				    <span style="color: #0000ff;">'message'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Sample status message'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">default</span><span style="color: #339933;">:</span>
	<span style="color: #000088;">$response</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'status'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'success'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span>
				    <span style="color: #0000ff;">'msg'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Unknown command'</span><span style="color: #339933;">,</span>
				    <span style="color: #0000ff;">'cmd'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$cmd</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// switch $cmd</span>
      <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-type: text/x-json; charset=utf-8'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">print</span> <span style="color: #990000;">utf8_encode</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">json_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$response</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// $cmd is set</span>
  <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// function mypp_template_redirect</span>
&nbsp;
add_filter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'query_vars'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'mypp_query_vars'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
add_action<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'template_redirect'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'mypp_template_redirect'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://gregorygrubbs.com/wordpress/how-to-get-your-command-line-php-script-working-with-wordpress-mu-27/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
