PHP in WordPress - Setting & Accessing Variables

by 5 replies
7
I'm using Exec-PHP to execute code, and it's working.

I want to be able to set a variable once upon & access that variable in individual posts.

I tried putting the code in header.php, then about everywhere else.

But when I put

<?php echo $var_name; ?> in the post, it's blank.

Help?

thanks,
PCRoger.
#programming #accessing #php #setting #variables #wordpress
  • Did you try using a session variable? That should persist through pages.

    Just a thought.

    Bill
  • That worked, thanks.

    Do you happen to know how to reference a file that is in

    http://mydomain.com/../includes/filename') with all the appropriate number of "../" but it never works.

    Had to duplicate the file in the directory of that theme.

    Ideas on that?

    thanks.
    • [1] reply
    • Not sure if WP is in the root of your domain or not. If it is then you could try the ABSPATH like require_once( ABSPATH . 'includes/file.php' );
  • WP is in /blog.

    thanks,
    PCRoger.
  • You will need to find the path yourself. It isn't too hard. Just create a path.php file in your includes directory.

    PHP Code:
    <?php
    echo dirname(__FILE__);
    ?>
    Now load it up in your browser and it will output the path. Something like /home/user/public_html/includes.

    Then you can do require_once('/home/user/public_html/includes/includefilename.php')

    Later,
    Dan

Next Topics on Trending Feed