Solved

Run PHP once in Wordpress page

Posted on 2011-03-04
3
351 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The purpose of this video is to demonstrate how to Import and export files in WordPress. 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 : Click on Too…
The viewer will learn how to dynamically set the form action using jQuery.

758 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now