?
Solved

Run PHP once in Wordpress page

Posted on 2011-03-04
3
Medium Priority
?
363 Views
Last Modified: 2012-06-27
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!!
0
Comment
Question by:ambientsbs
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 8

Accepted Solution

by:
rationalboss earned 2000 total points
ID: 35035190
Are you using session_start()?
You can put it on top of wp-config.php

<?php
session_start();
/**
 * The base configurations of the WordPress.
 *
... and so on

Open in new window


In your page.php if you are not including wordpress files there, put session_start() on top too.
So here is what I imagine:

page.php:
<?php
session_start();
/* do stuff */
$_SESSION['check'] = 1;
?>

Open in new window


In contact..
<?php
session_start();
if ($_SESSION['check'] !== 1) include('page.php');
/* other stuff */
?>

Open in new window

0
 
LVL 31

Expert Comment

by:gwkg
ID: 35044824
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
0
 

Author Comment

by:ambientsbs
ID: 35044906
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!
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The purpose of this video is to demonstrate how to manually back up a WordPress Database. This will be demonstrated using a Windows 8 PC. The Host used will be IPage.com Log into your Hosting account. IPage will be used for demonstration : Locat…
The purpose of this video is to demonstrate how to set up an RSS Feed on a WordPress Website. This will be demonstrated using a Windows 8 PC. Feedburner will be used for this demonstration. Go to your WordPress login page. This will look like the…
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question