php/wordpress include a file

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.

Read More

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.

Related posts

Leave a Reply

2 comments

  1. 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/

  2. 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.