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?
Related Posts:
- Passing variables to templates (alternatives to globalizing variables)
- How to get an array of menu items from the toolbar?
- If function exists, and array is met, echo function?
- Global functions on WPMU
- How to manage arrays from custom functions stored in functions.php?
- get_post_type() in in_array doesn’t work for some reason
- Simple custom theme option not being saved
- Sorting table function default
- Create a new query in function.php to filter blog posts
- Get array of metakey in all posts
- Turn get_posts as string into an array for use in theme admin options
- Passing global variable from header.php to functions.php
- How to echo the value of an array element using a function via a shortcode
- How to Use Global Variables Inside Header and Footer
- issue with if/elseif in_array inside foreach loop display only one post
- Getting the teaser text without overriding global variables
- Output comment_author in array
- Override a Post’s URL with Advanced Custom Fields Function
- External api call and make global variable for any page visitor enters , page-home, page, single etc
- How to create an array if ’empty’?
- Pass an argument into a function to extract from array
- How to access custom class methods from any include without using global
- Combine multiple menus using the filter wp_nav_menu_items
- Featured image on archive page based on post type
- Code executes outside of Loop while same code gives ‘Uninitialized string offset’ notice inside a while loop
- How to assign results to variables?
- Get title from IDs in a string
- Dynamically generated Navigation Diagram using Custom Fields
- Setting a cookie in functions.php and it gets deleted when working on an other function
- Settings in functions.php used by a plugin
- Using array page name together with page id to deregister script
- Echo custom field value in shortcode function
- get_footer can’t find any variables set in functions.php
- How do I access variables outside a function [closed]
- trouble with passing class method data to outside function
- Explode Array from Repeatable Custom Field
- Automatically delete posts that aren’t in an array
- What’s the difference between esc_html, esc_attr, esc_html_e, and so on?
- Is it possible to rename a post format?
- Is there a hook or function I can use to display all theme files being used on a current page?
- How to paginate the get_users function?
- How can I remove the title “leave a reply” in the comment box in twentyeleven?
- Child theme – overriding add_image_size by a child theme [duplicate]
- Check if has any sidebar active on current page
- Login using the password from protected pages
- Remove tag on my RSS Feed
- Automated mark posts as featured every day
- Child Theme Not Overriding Parent Theme
- Custom image size in Media Dropdown
- Trouble using antispambot()
- Polylang: pll_e() & pll__() on functions.php, doesn’t work
- Post Pagination Showing Same Posts Every Page
- Load .txt file for login_message in wp-login.php
- How do I redirect all 404 error url to Subcategory url
- Php function 301 redirect
- Function only on a specific page
- Logout Shortcode URL
- Add Custom Background Properties to Customizer
- What is the correct way of updating wordpress options
- WordPress functions.php – Execute plugin specific user online
- Trying to register script in footer
- register_sidebar_widget is deprecated since version 2.8! Use wp_register_sidebar_widget() instead
- Shotcode argument issues
- Remove Custuomize Sections
- Adding an option to a shortcode
- How can I show immediate and/or parents in a title?
- Add Login/Logout Menu Item to Primary Nav “My Account” Submenu [Woocommerce] [closed]
- Need help with customalert that it would read on a normal page, but not in WordPress
- Child theme function.php causes fatal error
- Best way to disable sidebars on posts (only)?
- How to override this theme function in child theme
- Linking Javascript in functions.php file
- Enqueueing scripts selectively & activation where needed
- Overwriting TwentyTwelve template file with child theme template, but lower in the hierarchy
- Using One Function To Create Two Different Shortcodes
- Change Page Title
- How to Not Show posts on Category page?
- Put images in the media from url functions
- Paragraph spaces in email template function
- Is it possible to load recaptcha script only in url with fragment identifier (#)?
- page shows short code not output
- Including Font Awesome in a Custom Theme
- Prevent function from triggering on current page
- How would I correctly implement a new if statement in a child functions file?
- Multiple Notifications SetInterval
- Child Theme not loading multiple stylesheets
- How To Show Shortcodes In WordPress Custom Fields?
- How to debug function file causing white screen
- Post date on Custom field
- Fatal error: Call to undefined function add_action() – an untouched problem
- Author info does not show up when author has no posts
- Load CSS conditional on custom field is present
- Modify sub_menu function to show pages below a specifically set page
- Add custom class to all URLs on entire site
- Unregistering custom tinymce plugin?
- Combining wp_current_user() and a variable
- Init hook for header send
- site_url is not honoring scheme
- Add functionality to block comment authors in the Comment edit pag
- WordPress enque the same script causes the setInterval not to work