admin管理员组

文章数量:1426918

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 : site_id=1
define('1_mytheme_town', 'Orlando');
define('1_mytheme_subject', 'food');
define('1_mytheme_p1', 'This is the first paragraph text');

// adifferentsite: 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 : 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.

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 : site_id=1
define('1_mytheme_town', 'Orlando');
define('1_mytheme_subject', 'food');
define('1_mytheme_p1', 'This is the first paragraph text');

// adifferentsite: 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 : 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 asked Mar 28, 2011 at 10:44 TheLoneCuberTheLoneCuber 3192 silver badges13 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

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.

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:

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

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.

本文标签: multisiteHow can I append blogid toecho functionsdefinedconstant