Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

How to append blog_id to echo [functions-defined-constant]? WordPress

  • SOLVED

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 ; ?>

But I need these to be blog-specific for multisite. So I thought it might be possible to append the current blog_id to the theme call and declare MultiSite-specific constants 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 [blog_id]_mytheme_town ; ?> .....

<strong>For example</strong>

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

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

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

<em>// anothersite.net : site_id=3</em>
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 [blog_id]_mytheme_town ; ?>
<?php echo [blog_id]_mytheme_subject ; ?>
<?php echo [blog_id]_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 a plain text version of the code

1_mytheme_town

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

Answers (3)

2011-03-29

Sébastien | French WordpressDesigner answers:

in your file functions.php :

global $blog_id;
if($blog_id==1) {
define('mytheme_town', 'Orlando');
define('mytheme_subject', 'food');
define('mytheme_p1', 'This is the first paragraph text');
}
elseif($blog_id==2) {
define('mytheme_town', 'New York');
define('mytheme_subject', 'wine');
define('mytheme_p1', 'This is the first paragraph text');
}
elseif($blog_id==3) {
define('mytheme_town', 'LA');
define('mytheme_subject', 'Donuts');
define('mytheme_p1', 'This is the first paragraph text');
}

and in your theme
<?php global $blog_id;
echo mytheme_town ;
echo mytheme_subject ;
echo mytheme_p1 ; ?>


TheLoneCuber comments:

Would there be some performance issues with this method if there were 50+ blogs and therefore 50+ elseif's ?



TheLoneCuber comments:

Is that how the if/elseif's work? If the blog id = 50 and I defined all 50 blog constants in ascending order in functions.php, then all the 49 elseif's get processed before it gets to elseif($blog_id==50)?


Sébastien | French WordpressDesigner comments:

no, not really.
There is one query processed, not 49

2011-03-29

Daniele Raimondi answers:

You can get the blog ID from the $wbpd object, like so:

<?php
global $wpdb;
$current_blog = $wpdb->blogid;
?>

or could simple use

global $blog_id;

-------------------------------------------------------------------
In your functions.php you can define your constants as you have already done, and then in your theme, make a call like this, <strong>using constant PHP function</strong>:

<?php
global $blog_id;
echo constant($blog_id.'_mytheme_p1');
?>


TheLoneCuber comments:

I have tried this approach unsuccessfully. It does not append the blog_id to the constant. It echos them separately and does not process them as a single php call. This might be due to the way I am calling it?

This is what I've tried ....
<?php global $blog_id; echo $blog_id; echo '_mytheme_town'?>

But it literally echos the blog_id and echos the constant in text form (not processed php). This is what appears on the frontend ...
1_mytheme_town


2011-03-29

AdamGold answers:

Try this:

$my_blog_id = get_current_blog_id();
define( $my_blog_id . '_constant', 'content' );


AdamGold comments:

If it doesn't work, try this one:

global $current_site;
define( $current_site->id . '_constant', 'content' );


AdamGold comments:

By the way, if you want to use $blog_id (which is also an option..):
<?php
global $blog_id;
define( $blog_id . '_mytheme_town', 'Orlando' );
?>