Solved

Run PHP once in Wordpress page

Posted on 2011-03-04
3
360 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
3 Comments
 
LVL 8

Accepted Solution

by:
rationalboss earned 500 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

In Part I (http://www.experts-exchange.com/Web_Development/Blogs/WordPress/A_8410-Getting-Started-In-WordPress-Part-I.html), I introduced you to the powerful WordPress backend, the WordPress administrative Dashboard.  In Part II, I will introduce yo…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The purpose of this video is to demonstrate how to add AdSense Ads to a WordPress Website, and how to set up WordPress to automatically place Ads in Sidebars. This will be demonstrated using a Windows 8 PC. Log into your AdSense account. : Cli…
The purpose of this video is to demonstrate how to set up the permalinks on a WordPress Website. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Go t…

820 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