I use 'constants' a lot. I set them in functions.php like define('mytheme_town', 'Orlando'); and then call them into the theme <?php echo mytheme_town ; ?>.

It's simple and easy. But I need them to be site-specific (for multisite) so I thought it might be possible to append the current blog_id to the theme call and declare constants for each MultiSite in functions.php? I don't know how to code that, but what I'm trying to achieve in the theme file this ... <?php echo [current_blog]_mytheme_town ; ?> .....

For example

In functions.php // set constants for all multisites that use this theme

// awebsite.org : site_id=1
define('1_mytheme_town', 'Orlando');
define('1_mytheme_subject', 'food');
define('1_mytheme_p1', 'This is the first paragraph text');

// adifferentsite.com: site_id=2
define('2_mytheme_town', 'New York');
define('2_mytheme_subject', 'wine');
define('2_mytheme_p1', 'This is the first paragraph text');

// anothersite.net : site_id=3 define('3_mytheme_town', 'LA');
define('3_mytheme_subject', 'Donuts');
define('3_mytheme_p1', 'This is the first paragraph text');

In theme files Call the constants using the current blog_id like so ...

<?php echo [current_blog]_mytheme_town ; ?>
<?php echo [current_blog]_mytheme_subject ; ?>
<?php echo [current_blog]_mytheme_p1 ; ?>

I have been trying all day, but I'm just guessing and I don't really know what I am doing. Everything I have tried works, but it simply echos out text of the code


and not the constant that is defined in functions.php.

Hope you understand what I'm trying to explain.

2 Answers 2


You can access the blog ID you are on by global $blog_id; and then you can use that how you want it to work.

  • I've not been able to append global $blog_id; to the defined function and therefore echo out [blog_id]_[functions_defined_text] @Ashfame. I can only get it to echo out the actual blog_id # followed by the template function call. I tried this global $blog_id; echo $blog_id; echo _mytheme_town; but it simple echos out - 1_mytheme_town - 2_mytheme_town - 3_mytheme_town Can you give a code demo of your technique? Mar 29, 2011 at 5:35
  • First construct the name into a variable and then use variable of a variable. $name=$blog_id.'_my_theme_town; echo $$name;
    – Ashfame
    Mar 29, 2011 at 14:00

Using constants in the way you are doing is very much not the way a good WordPress programmer would code. Constants are very inflexible and just not how things are typically done in WordPress.

Assuming that when you refer to "site" you really mean "blog" in WordPress' Multisite vernacular (yes, I think the terminology is strange but it is what it is) then why not use get_blog_option() and update_blog_option() along with get_site_option() and update_site_option()? (this code goes in your main blog's theme's functions.php file, or in a .php file of a "must use" plugin you might be developing):

function yoursite_init() {
  if (!get_site_option('town_subject_p1_initialized')) {
    update_blog_option(1,'p1','This is the first paragraph text');
    update_blog_option(2,'town','New York');
    update_blog_option(2,'p1','This is the first paragraph text');
    update_blog_option(3,'p1','This is the first paragraph text');

Then to access within your theme use the following code:

<?php echo get_blog_option($blog_id,'town'); ?>
<?php echo get_blog_option($blog_id,'subject'); ?>
<?php echo get_blog_option($blog_id,'p1'); ?>

This is a lot more "WordPress-like" and you'll find it's both a lot more flexible and someone else who knows WordPress is a lot more likely to actually understand the code you leave behind.


Based on comments I'm going to suggest a different approach for initialization. Rather than have the programmer constantly editing the .PHP file every time he wants to add a new site the site meta should be added using the 'wpmu_new_blog' hook which you can see here:

function getsunrise_wpmu_new_blog( $blog_id ) {
  update_blog_option( $blog_id,'town','Pending...');
  update_blog_option( $blog_id,'subject','Pending...');
  update_blog_option( $blog_id,'p1','Pending...');

Then the site admin can edit the site meta at this URL /wp-admin/network/site-settings.php?id={$site_ID} (replacing {$site_ID} with the new site #, of course.) For example:

Screenshot of editing blog-specific settings in WordPress Multisite

Scroll down to the bottom of that page and then you see the three (3) new site meta values:

Screenshot of editing blog-specific additional site meta in WordPress Multisite

This is a much better way that using constants and editing the .php file. Your clients won't know, but subconsciously they will thank you for not causing them future headaches.

  • Is there a limit to how many sites I could add in this manner @MikeSchinkel? Also, I added the update/gets for one site to test your code, and it worked perfectly. I then proceeded to add more, but the additional sites are not working. No cache plugins are activate; do I need to "reset"/flush something? Mar 28, 2011 at 13:01
  • I added @MikeSchinkle's code for update_blog_option to functions for blog 1 to test it, and it worked perfectly. So I then I added update_blog_option to the same add_action call in functions.php for the additional blogs (2 and 3) but they don't work? I've tried re-saving the permalinks (which I read helps force-refresh something to do with functions.php). Once an add_action init has been added, can it be edited? Mar 28, 2011 at 22:46
  • @Thao - Are your different sites using the same theme? Mar 29, 2011 at 4:18
  • They are @MikeSchinkel. Which is why I'm trying to use this sort of method (to streamline my workload). Not every blog in the Network is using the same theme, but every blog referenced by update_blog_option is. It's been suggested to me that once the get_site_option has been initialized the first time, that the if statement will no longer be true if (!get_site_option('title_desc_p1_city_url_initialized')) therefore any extra info I add will not be updated. So I tried commenting out the if statement, but have had no joy with that either. Mar 29, 2011 at 5:06
  • That's assuming when you refer to "site" @MikeSchinkel you really mean "blog" in WordPress' Multisite vernacular (yes, I think the terminology is strange but it is what it is). #laughing Mar 29, 2011 at 5:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.