WordPress.org

Ready to get started?Download WordPress

Forums

Echoing content in a post or page from a PHP-included external document. (9 posts)

  1. Sum1ElsN0tM3
    Member
    Posted 2 years ago #

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

  2. vtxyzzy
    Member
    Posted 2 years ago #

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

  3. Sum1ElsN0tM3
    Member
    Posted 2 years ago #

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

  4. vtxyzzy
    Member
    Posted 2 years ago #

    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.

  5. Sum1ElsN0tM3
    Member
    Posted 2 years ago #

    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; ?>
  6. Sum1ElsN0tM3
    Member
    Posted 2 years ago #

    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.

  7. vtxyzzy
    Member
    Posted 2 years ago #

    Declaring the variable global might help:

    [php] global $price_website_5pageBusiness; echo $price_website_5pageBusiness; [\php]
  8. Sum1ElsN0tM3
    Member
    Posted 2 years ago #

    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?

  9. wspencer
    Member
    Posted 2 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic