Category Archives: PHP

Migration to WordPress and Multi-Site Issues

I finally bit the bullet and have now migrated my site from b2evolution to WordPress.  The b2evolution user interface was far too cumbersome, especially in terms of adding diagrams to my posts.  In WordPress it is just a matter of drag and drop or browse and upload all from within the edit window.  As far as the migration of the posts goes, I could not find easy way to automate the import of the b2evolution data and since I only have 29 posts, I just manually copied and pasted the entries, resetting the dates so that they would appear in the proper chronological order.  All diagrams were on my hard drive so adding them back into the posts was easy.  The whole process took maybe 3 hours.

The bare-bones WP installation normally just allows for a single blog within its base installation.  To get multiple independent blogs, you need to either install WP multiple times or enable networking.  This brought up the following problem,  since I had decided to configure networking and I installed WP in a sub-directory (two important decisions when setting up a site)….

Say you have two blogging sites, Bobs_Blog and Alices_Blog on your main site www.mainsite.com/blogs.  These blogs will be reachable over the following URLs (assuming WP was installed in the blogs sub-directory):

http://www.mainsite.com/blogs/Bobs_Blog/

and

http://www.mainsite.com/blogs/Alices_Blog/

The two sub-directories, /Bobs_Blog and /Alices_Blog do not really exist and are just used as variables for the php scripts in WP to be able to build the web pages dynamically.   So if you now try and point some sub-domains in DNS to these paths, the web browser will not find the directory and fail.  If you add the directories manually, the browser will not be able to find a home or index file (since there really  isn’t one) and give you an error.  For example… if you configure http://bob.mainsite.com to point to www.mainsite.com/blogs/Bobs_Blog/, this will not work.  The server will try and find the Bobs_Blog directory and will fail.

The prescribed solution to this problem is to install MU Domain Mapper plugin and use that to redirect the http requests to the correct location.  Unfortunately the MU Domain Mapper plugin only works if WP is installed in the root directory!

A second idea was to just ask my DNS hosting provider to map a CNAME to the path … for example:

bob.mainsite.com     IN      CNAME     www.mainsite.com/blogs/Bobs_Blog/

Unfortunately, this is not a valid DNS entry 🙁

So the idea I had was to add an index file to the sub-directory which would only redirect the http request to the real site. Based on our example, the index file would have the following php code (taken from http://www.cyberciti.biz/faq/php-redirect/ )…

<?php
/* Redirect browser */
header(“Location: http://www.mainsite.com/blogs/Bobs_Blog/”);
/* Make sure that code below does not get executed when we redirect. */
exit;
?>

When I added a sub-directory called blogs/Bobs_Blog and included this file, I ran into the problem that it created a redirect loop and generated yet another browser error.

The final solution was to create a new independant directory and add the redirect file there.  Then point the DNS sub-domain to this directory and voila… it worked!  The final configuration has http://bob.mainsite.com pointing to www.mainsite.com/blogs/Bobs_Blog_redir/and the sub-directory Bobs_Blog_redir contains an index.php file with the code shown above.  One Caveat… once you connect to the site, the real URL then appears (www.mainsite.com/blogs/Bobs_Blog) and is used throughout.  For me this is not a problem, my goal was to have a simple URL for people to use (and remember).  Once in the site, everything is automated anyway.

How to manage menus in web pages

One of the reasons I started working with CSS was to get rid of my old Frames based web pages.  One of the main benefits of frames was that you could maintain your menus in a single file/frame and the information was valid for all content.  Unfortunately, just be implementing CSS this problem is not solved (although it is often written that CSS is a replacement for frames based web pages).

Thanks now to having been exposed to PHP, I have figured out the last piece of the puzzle… in PHP there are two functions… include() and require().  These two functions will take the  content from another page and place it into your web page.  Without knowing much else, these can be inserted in any page (renamed with a .PHP extension and made executable) and all of the static menu configuration from each page can be placed in the file you call.  This way, one can maintain the menu items in a single file and it will be valid for all content pages.  See the following tutorial for more information… PHP Includes.

Variable passing with require() and include()

Something new… variables are passed to require() and include() from the main page.  So based on the variable you set in the main page, you can return different pieces of code, menu items, etc.

In the main page you set the variable, call the file, and display the output.

<?php
$x=2;
require(“menu.php”);
echo $message;
?>

In the called file, evaluate the variable, set the output variable and return it to the main page.

<?php
if ($x==1)
$message=”Have a nice weekend!”;
else
$message=”Have a \”nice\” day!”;
return $message;
?>

Note that nested quotes need a \ preceeding them.  The code above would put… Have a “nice” day! … in the page.

The switch() function would probably make more sense if more than two or three values are expected.