Link to home
Start Free TrialLog in
Avatar of ambientsbs
ambientsbs

asked on

Run PHP once in Wordpress page

Hello Experts,

I have a Wordpress site that I am trying to add some custom PHP to, which I would only like to run once. My Wordpress pages all use the script within "page.php" of my theme to create every page.

So if I go to my "Home" page it renders that page using "page.php". If I click on the "Contact" page it renders that page by again using the "page.php" from my theme. At least, this is my understanding of how the theme works.

I'm trying to add some script to my "page.php" file that will run the FIRST time that "page.php" is loaded and never again. Basically, someone comes to the home page and that script is run, but if they click "Contact", the script doesn't get run again when "page.php" is reloaded. I've tried using a session variable called $_SESSION['check'] but it's not working correctly.

Any ideas you can provide would be helpful and please don't hesitate to let me know if I haven't provided enough info. Thank you!!
ASKER CERTIFIED SOLUTION
Avatar of rationalboss
rationalboss

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Try

setcookie("check", 1, time()+3600*24*365,  SITECOOKIEPATH, COOKIE_DOMAIN, false);

echo $_COOKIE["check"];

setcookie: http://us.php.net/manual/en/function.setcookie.php
Avatar of ambientsbs
ambientsbs

ASKER

I got the sessions to work. Wordpress is apparently set to not use sessions out of the box so I essentially had to turn them on.

Here is my problem now. My Wordpress site is sitting in an iframe on another site so when I click a page on my Wordpress site I can see that it is starting a brand new session as the session ID changes.

I think the way around this is cookies so I'll try gwkg's suggestion and see if I can make the cookies work. Thanks for the help so far. I'll let you guys know how it goes!