Skip to main content
Added another explaination
Source Link
Sunyatasattva
  • 1.6k
  • 9
  • 12

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:

$GLOBALS["year_arr"]

Update

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?

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.

Update

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?

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:

$GLOBALS["year_arr"]

Update

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?

added 429 characters in body
Source Link
Sunyatasattva
  • 1.6k
  • 9
  • 12

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.

Update

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?

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.

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.

Update

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?

Source Link
Sunyatasattva
  • 1.6k
  • 9
  • 12

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.