Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Run PHP once in Wordpress page

Posted on 2011-03-04
3
Medium Priority
?
365 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

618 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