Virtual Multiblog — how to use unique headers on blogs running the same theme

One of the most common questions I get regarding the Virtual Multiblog system for WordPress comes from people wanting to have multiple blogs running on the same theme, but with a different header for each blog. It can be done, and it’s fairly easy to do. Once you have it set up, you could use it to call any distinct file on a per-blog basis — a different CSS file, or a different footer perhaps.

I’ve never been too active about showing this to people, because the function required to do it is not one I consider completely stable — I din’t want to show people how to do it only to have it break the next time I update the VMB system. In response to the requests, however, I am adding a function to the upcoming release (currently in private beta). You can have that ability right now, in a way that should not cause problems with later updates — just add a short function to your existing vmb-functions.php file.

It may a good idea to upgrade to the latest version of VMB first, but if you’re running a fairly recent version you’ll probably be okay. Open up that file in a text editor, and scroll to the bottom. You will see a function called get_virtual_user(). Replace it with the following.

[Updated: “global” line was missing from function]

function get_virtual_user( $clean = false ) {
        global $vmb;
        if ( $clean )
                return $vmb->clean_vuser();
        else
                return VUSER;
}

Do not add this to your theme’s functions.php; add it directly to the Virtual Multiblog file, or you’ll get an error that a function is being declared twice. Replace the existing function of the same name. (The reason I say this will not cause problems down the road is that future versions of Virtual Multiblog will contain this function or a compatible one.)

When you’re done, get_virtual_user( true ) will give you the VUSER string, but cleaned up so it can be used as part of a file name. Specifically, anything that isn’t a letter or number is converted to an _ understroke. For example, if your VUSER is “example.com”, this function would give you “example_com”.

Now that the function is set up, here’s how you would use it. First, of course, create the different header files for each VUSER. For our blogs at example.com/blog1 and example.com/blog2, we might use header-example_com_blog1.php and header-example_com_blog2.php.

Now in your theme, you include your header this way:

$clean_vuser = get_virtual_user( true );
include_once( 'header-' . $clean_vuser . '.php' );

That should be it. Each blog should now display its own header. Again, once the function is set up, you can use this to call any blog-specific file — you can have different footers, different CSS, or whatever you like.

Enjoy!

This entry was posted in Codecraft, Webcraft and tagged , , , , , , . Bookmark the permalink.

8 Responses to Virtual Multiblog — how to use unique headers on blogs running the same theme

  1. Den says:

    Thank you Stephen,
    a few days before your post, I found my solution, that here:

    1) in ” /wp-content/themes/mytheme/header.php, I wrote this line:
    <link rel="stylesheet" href="<?php bloginfo('template_url');?>/nav_<?php echo VUSER;?>.css" type="text/css" media="screen" />

    2) in ” /wp-content/themes/mytheme/ I’ve created the file:
    “nav_abc123.css”, where “abc123” represent in your example “example_com_blog1″ or ” example_com_blog2″, etc…

    3) It’s all, it’s functional at 100%, without bug nor error message. As soon as my 1st site with VMB is ready, I will announce my adventure! (very interesting). In a friendly way, Den

  2. Den says:

    Scusi!: my typing error:

    In my subparagraph 1), it was to read:

    <link rel="stylesheet" href="<?php bloginfo('template_url');?>/nav_.css" type="text/css" media="screen" />

  3. Den says:

    Arrrgh, it was to read:
    <link rel="stylesheet" href="/nav_.css" type="text/css" media="screen" />
    Et voila.

  4. Den says:

    Ooooh, php tag are not print here again… In a few words:
    The specific StyleSheet URL is given by the Php-command ECHO that call “VUSER” var that compose the name of the css file.

    [I think I fixed it in your first comment — ed]

  5. Stephen says:

    Den — That works just fine, except when the VUSER has slashes in it. The reason I suggest the get_virtual_user() function described above is that it takes anything not a letter or number and changes it to an understroke, thus making it filename-safe.

    So… You did it exactly right; but your code won’t work for all cases.

    (for example: nav_striderweb.com/blog.css won’t work, but nav_striderweb_com_blog.css will.)

  6. Stephen says:

    Post updated — the script was missing a line.

  7. Chris says:

    I think there is a more general issue with theme editing and VMB, for technically challenged persons like myself. There’s a risk of editing a theme for one site and forgetting it will forget other sites with the same theme, so (I suppose) the theme’s folder should be copied and the theme renamed in the CSS file (e.g. MyFavouriteTheme to MyFavouriteTheme2) as WordPress seems to use the first line of the CSS file to list its theme choices.

  8. michael says:

    I would like to create a css file for some blogs, but not all. I wrote the following code to look for the css file and use it if it exits

    $clean_vuser = get_virtual_user( true );
    if (file_exists(get_template_directory() . “/styles/” . $clean_vuser . “-style.css”)) {
    $stylesheet = get_stylesheet_directory_uri() . “/styles/” . $clean_vuser . “-style.css”;
    }
    else {
    $stylesheet = get_stylesheet_uri();
    }

    The used “echo $stylesheet;” in the right place.

    Now I would like only the users of that blog to be able to edit the stylesheet of their domain, but not others and not the default one. Anyone know how to do this?

Leave a Reply

Your email address will not be published.