Support » Fixing WordPress » Echoing content in a post or page from a PHP-included external document.

  • Sum1ElsN0tM3

    (@sum1elsn0tm3)


    I have a price list in an external file for easy site-wide updating. I’ve included the document into my theme’s header.php and know it’s working because I can echo out a price variable just under the body tag (did it just for testing, of course).

    I also have “Allow PHP in Posts and Pages” plugin installed and know it’s working because I can echo a string into a page.

    But, for some reason, the combination of the two are not working. I cannot echo a price variable from the included price list into a WordPress-generated page.

    Does anyone have any ideas as to what I might be doing wrong?

    [ Do not bump, it’s not permitted here. ]

Viewing 8 replies - 1 through 8 (of 8 total)
  • vtxyzzy

    (@vtxyzzy)

    Just a guess, but you might need to declare the list as global in your page.

    Sum1ElsN0tM3

    (@sum1elsn0tm3)

    Thanks for the response. I’m not sure what you mean, though.

    vtxyzzy

    (@vtxyzzy)

    Since I don’t know the exact coding of the PHP in your page, I can’t say for sure how to do this. Can you post the code that you used just after the body tag?

    (Don’t try to post the code here unless it is 10 lines or less. Use a pastebin and post a link to it here if necessary)

    It would also help to know the structure of the external document that you included in the header.

    Sum1ElsN0tM3

    (@sum1elsn0tm3)

    For example, prices.php looks like this:

    <?php
    $price_website_HomeSinglePage = '187';
    $price_website_3pageIMsite = '350';
    $price_website_3pagePersonal = '350';
    $price_website_5pageBusiness = '550';
    $price_website_WPbasic = '550';
    ?>

    Then I call that into the WP theme’s header:

    <?php include('includes/prices.php'); ?>

    And then, in various posts and pages (using ‘Allow PHP’ plugin) I call individual prices like this:

    [php] echo $price_website_5pageBusiness; [\php]

    As for the tag under the body tag, that was regular PHP (below) and was strictly for testing, to make sure that prices.php was being called in, which it was.

    <?php echo $price_website_5pageBusiness; ?>
    Sum1ElsN0tM3

    (@sum1elsn0tm3)

    I should also mention that I’m working strictly in the theme and posts, not in WP core code. I’m also not breaking any theme code.

    vtxyzzy

    (@vtxyzzy)

    Declaring the variable global might help:

    [php] global $price_website_5pageBusiness; echo $price_website_5pageBusiness; [\php]
    Sum1ElsN0tM3

    (@sum1elsn0tm3)

    I thought that was insecure and that PHP had been updated to automatically make variables global. Am I confusing this with some other “global” topic?

    wspencer

    (@wspencer)

    Since everything displayed on a WordPress page is the result of a WordPress specific function, variables have to be declared global inside of the function that’s currently running to be used.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Echoing content in a post or page from a PHP-included external document.’ is closed to new replies.