Ready to get started?Download WordPress


How (where) to include php-file with variables? (3 posts)

  1. wpsweden
    Posted 5 years ago #


    I have a site in three languages and I use qTranslate to handle the content. I want to change static content as well and have a language file with translations ("translation.php"). An example would be:

    $_PAGE_HOME_TEASERTEXT['en'] = "teaser, English";
    $_PAGE_HOME_TEASERTEXT['se'] = "teaser, Swedish";
    $_PAGE_HOME_TEASERTEXT['de'] = "teaser, German";

    I want to include those variables in certain positions all over my site and simply include the translation file (to get access to the variables). But, and here's the problem, I don't know where to include it to get it working. Including in index.php or header.php does not give me access in for instance sidebar.php.

    So, I want it global over my site and parsed for every page. Where can I include it? (using include("/home/..../translation.php");)

    Help much appreciated!

  2. sojweb
    Posted 5 years ago #

    Is your theme using functions to include page sections (like get_sidebar(), get_header(), etc.)? If so, it's a problem of scope; those functions don't have explicit access to any variables defined outside of them.

    There are a couple ways around this, you could use manual includes instead of functions in your template (for instance, instead of get_sidebar();, use require_once TEMPLATEPATH.'/sidebar.php';), and then put all of these variables at the top of your header file, or some other file that is common to all pages.

    Probably the simplest thing to do, though, is to create a function in functions.php in your theme directory that spits out the text you need when you pass it what you want. Doing this, you're guaranteed that you'll have access to the variables at any place in your theme, and you don't need to modify any of your theme's files (except functions.php, of course).

    For instance, you might make a function like this:

    function echo_theme_text($type='', $language='')
    	 * Include the translation data here;
    	 * copy and paste from translation.php, or do an include
    	// Fail text; returned if no match found
    	$fail_text = 'text retrieval failed.';
    	// Get the right text variable
    		case 'home_teaser':
    			$text_variable = $_PAGE_HOME_TEASERTEXT;
    		case 'home_body':
    			$text_variable = $_PAGE_HOME_BODYTEXT;
    		/** Add however many blocks here as you have variables */
    	// In case no matches where found
    	if(!isset($text_variable)) echo $fail_text;
    	// Make sure language data exists before returning
    		echo $text_variable[$language];
    	// Otherwise, fail out
    		echo $fail_text;

    Then, in your theme, wherever you needed a translation, you could use <?php echo_theme_text('home_teaser','en'); ?>.

  3. wpsweden
    Posted 5 years ago #

    sojweb, thank you very much!

    I tried the first suggestion which works perfectly, but on the other hand creates some redundant code (but I can live with that).

    I will have a go at the second one as well and see if I can refine the method a bit (perhaps separate code and content).

    Really appreciate your answer, thanks again.

Topic Closed

This topic has been closed to new replies.

About this Topic