logo
Ask your WordPress questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

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

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

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 [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.

This question has been answered.

TheLoneCuber | 03/29/11 at 7:43am Edit

Previous versions of this question: 03/29/11 at 7:55am

(3) Possible Answers Submitted...

See a chronological view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    03/29/11
    8:26am
    Daniele Raimondi says:

    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, using constant PHP function:

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

    Previous versions of this answer: 03/29/11 at 8:24am | 03/29/11 at 8:25am | 03/29/11 at 8:26am

    • 03/29/11 8:19am

      TheLoneCuber says:

      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



  • avatar
    Last edited:
    03/29/11
    9:17am
    Sébastien | French WordpressDesigner says:

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

    • 03/29/11 8:27am

      TheLoneCuber says:

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

    • 03/29/11 8:32am

      Sébastien | French WordpressDesigner says:

      no problem

    • 03/29/11 8:43am

      TheLoneCuber says:

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

    • 03/29/11 8:47am

      Sébastien | French WordpressDesigner says:

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

  • avatar
    Last edited:
    03/29/11
    8:32am
    AdamGold says:

    Try this:


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

    • 03/29/11 8:34am

      AdamGold says:

      If it doesn't work, try this one:


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

    • 03/29/11 8:35am

      AdamGold says:

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

This question has expired.





Current status of this question: Completed



Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.