WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I try to create an array of years, which is avaible in my template-files and in at least one other function in the functions.php.

function get_the_years() 
    global $year_arr = range(2001, date('Y'));
    return $year_arr;

Then I want to use it in a function, which creates a term for every year in the custom taxonomy 'year'.

function create_year_terms() {
foreach($year_arr as $value){   
    $term = term_exists($value, 'year');
        if ($term !== 0 && $term !== null) {
            echo $value . ' category exists!';
        else { wp_insert_term(
            $value, // the term 
            'year', // the taxonomy
                'description'=> 'The name of a year',
                'slug' => $value )
            echo'term '. $value . ' created';


And trying to attach this function to the wp_login hook

add_action('wp_login', 'create_year_terms');

I tried to make my hoeworks and I guess I understand "something" but I'm a bit scared to kill something by putting wrong stuff in my functions.php, so please excuse my question.

share|improve this question
How is this question WordPress-specific? – Chip Bennett Mar 1 '13 at 0:56
Oh sorry I'm not really familiar with php, I expected it to be WordPress-specific, cause of the hooks and custom terms created. – schwarzgrau Mar 1 '13 at 1:02
up vote 2 down vote accepted

In order to access a variable defined in the global scope you must reference it with the global keyword wherever you want to call it again.

In your case, the function create_year_terms() must call the global $year_arr within its scope.

Also, you can always get your variable in the global scope by using the $GLOBALS array with the name of your variable as the key, as such:



Regarding your hook decision: First of all, it is best practice to put this kind of code in a plug-in rather than in your theme functions. Second of all, hook it on init; or register an activation hook for your plugin and then perhaps use wp_schedule_event with a custom interval of one year.

…but then again, is this really necessary?

share|improve this answer
so I need to change the second line of the second block from foreach($year_arr as $value){ to foreach( global $year_arr as $value){ ? – schwarzgrau Mar 1 '13 at 0:59
Leave your foreach alone, but a line before declare global $year_arr – Sunyatasattva Mar 1 '13 at 1:02
Ah ok, I guess now I've understand it. thank you. – schwarzgrau Mar 1 '13 at 1:09
wp_schedule_event is a really good tip. Unfortunately I guess it's necessary. I made a website for the film department at my college. And as long as I'm still studying I'll administrate the site, but it should still work, if someone else cares about it. – schwarzgrau Mar 1 '13 at 1:25
Mmm… then maybe it would be better to check on post insertion, or something similar. But, just out of curiosity, why would you need a year taxonomy anyways? – Sunyatasattva Mar 1 '13 at 1:28

Firstly, you have to define a global variable in the scope, where you want to have access to it, meaning outside your functions (and maybe even outside the class - if you have one.

Secondly, as far as I know, you have to first 'render' a variable global, and then, in a new line, set its value:

global $myvar;
$myvar = "whatever";

Inside the functions, you have to state the variable again as global before you use it. Otherwise it'd be just a local variable with the same name as the global (but not yet known) variable from the 'outside'.

And what is it exactly that you want to do with the function? Do you know about the hook?

wp_login is deprecated - use wp_signon. But I don't get what you're willing to do...

share|improve this answer
thank you for the clarification on the use of global. The function should check if there is already a custom term with the current year, if not create one. I choosed wp_login, cause as far as I know there is no hook, which gets executed every 1th january or something else I could use. – schwarzgrau Mar 1 '13 at 1:09
Why would you do that? I updated my answer above to address this issue. – Sunyatasattva Mar 1 '13 at 1:19

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.