1

This question is regarding wordpress.

I have two variables $random_n and $adcount declared in each of index.php , archieve.php, search.php and single.php files at the very first line.

The variables are declared as follows:

<?php $random_n = randomNumber();?> 
<?php $adcount = 1; ?>

randomNumber() is a function inside function.php.

If I move those variables to header.php and remove them from all these files, it stops working. :/ The variables can't be found in index.php, archieve.php, search.php, single.php if they are placed in header.php

I've also tried putting this code inside a new file and including that file to index.php, archieve.php, search.php, and single.php, but it hadn't helped as well.

1
  • It's hard to tell from the description of your question what you're doing wrong. Commented Jan 19, 2012 at 8:14

2 Answers 2

2

If the variable is somehow related to your blog configuration, you should use the wordpress option API to build this (see here , search for option).

If your variable is random (your function name hints that) a way to work around this would be using session variables to store those values.

A good tutorial on how to use session variables in wordpress can be found here: http://www.myguysolutions.com/2010/04/14/how-to-enable-the-use-of-sessions-on-your-wordpress-blog/

Sign up to request clarification or add additional context in comments.

Comments

1

Type in var_dump($random_n) into the beginning of index.php and post the output, please.

One possible reason is that your index, search and so on are included inside the function that does not share the scope with header.php, so your vars are just outside of the scope. It might help if you declare them global both in header.php and your working files. But that's not a good idea.

Better solution would be to introduce some Registry object (Registry pattern) and use it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.