Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

1_mytheme_town

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

Hope you understand what I'm trying to explain.

share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

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.

share|improve this answer
    
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? –  TheLoneCuber Mar 29 '11 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 '11 at 14:00
add comment

Hi @Thao:

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):

add_action('init','yoursite_init');
function yoursite_init() {
  if (!get_site_option('town_subject_p1_initialized')) {

    update_blog_option(1,'town','Orlando');
    update_blog_option(1,'subject','food');
    update_blog_option(1,'p1','This is the first paragraph text');

    update_blog_option(2,'town','New York');
    update_blog_option(2,'subject','wine');
    update_blog_option(2,'p1','This is the first paragraph text');

    update_blog_option(3,'town','LA');
    update_blog_option(3,'subject','donuts');
    update_blog_option(3,'p1','This is the first paragraph text');

    update_site_option('town_subject_p1_initialized',true);

  }
}

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.

UPDATE

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:

add_action('wpmu_new_blog','getsunrise_wpmu_new_blog');
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.

share|improve this answer
    
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? –  TheLoneCuber Mar 28 '11 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? –  TheLoneCuber Mar 28 '11 at 22:46
    
@Thao - Are your different sites using the same theme? –  MikeSchinkel Mar 29 '11 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. –  TheLoneCuber Mar 29 '11 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 –  TheLoneCuber Mar 29 '11 at 5:11
show 9 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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