WordPress template files are included via functions, which places the scope of those included files within those functions. That’s why you have to declare it global twice. Here is an example you can try with 3 simple php files to illustrate this without using WordPress:
main.php
function include_file_1(){
include '1.php';
}
function include_file_2(){
include '2.php';
}
include_file_1();
include_file_2();
1.php
function tester(){
global $pol;
$pol="ok all fine and working";
}
2.php
tester();
echo $pol;
If you test this out, you will see you get nothing from echo $pol
, it’s not within scope. Now edit 2.php
to declare global $pol
first:
global $pol
tester();
echo $pol;
And now you get the expected results.
Related Posts:
- What’s the difference between WordPress random_int() and PHP built-in function random_int()?
- Is it ok to use a function to output the text domain name in a wordpress theme
- Issue with wp_get_attachment_image() and SVG file type
- deregister scripts on certain page
- contactform7 remove tags with “wpcf7_autop false” from functions.php
- How to get an array of menu items from the toolbar?
- Changing a function in function.php to a shortcode – for listing categories of only a certain post type
- Using get_terms for custom taxonomy in functions.php
- How to remove the embed_footer?
- Hide some items from Screen options in dashboard for products
- Why isn’t is_page_template() adding a body class?
- Query children and parent title
- How to make unique add_filter to the_content of specific page template files – so each template gets its own addition
- Creating loop within functions.php
- Limit filter upgrader_post_install to a single plugin
- How to test for Super Admin
- How to use max and min values of custom fields
- creating shortcode to pull json array
- Still relevant method of embedding images in WP Theme in 2018
- Pass Shortcode Attribute to footer Script
- How to use Internationalized human_time_diff() function in Chinese?
- post to subcategory and parent in wp_insert_post
- Replace menu links with # and add name to its li
- Variable if post is sticky in functions.php
- Change template on the fly based on post parent selection
- Create a new query in function.php to filter blog posts
- Use third party Library in wordpress
- Remove the product category module block from the Woocommerce product page
- Setting youtube size in functions.php
- Creating new blog on multisite programatically without hacks?
- Allowing users to view private posts (pending approval)
- How to get correct URLs in network wide menu (Multisite)?
- Remove category from url
- ‘Attempt to modify property of non-object’ warning
- Conditional functions.php on page template
- Optimizing a WordPress site
- different style sheet for just one page template
- Problem with comments_number()
- Functions when woocommerce isn’t installed
- How to add custom JavaScript in functions?
- How to target post and pages and not all post types in admin?
- srcset and custom field get_post_meta and wp_get_attachment_url
- hide shortcode using hooks
- Dequeue script to prevent javascript event conflict on wordpress child theme
- While loop with an exception after a count is reached
- Adding code using functions.php in child theme
- Passing global variable from header.php to functions.php
- Page get parameter doesn’t work with is_single() function
- How is $current_page passed in woocommerce_account_orders function?
- Upgraded server now class My_Walker_Nav_Menu extends Walker_Nav_Menu not working
- Filter to wp_list_authors
- Contact Form 7 If Condition
- How to include any template using Shortcode fuction?
- display a div on ervery site but not frontpage?
- How can I change the size of the text in Word Press
- Using depth=n in wp_list_pages
- shorthand syntax for custom fields
- How do I pull avatar from post using BuddyPress? [closed]
- Replace header image on all other pages but home – URL issue
- Child theme functions.php
- How to echo Widget Title in Custom Frontend-Template Box
- Redirect to post after publish or update a published post
- Show admin page, only if function is being used
- Get users with different roles and call function on each of them (user_meta)
- Getting images from media library and get_the_date() not working
- WordPress show bad the php hour and date
- Show errormessages on wrong username/password on custom loginform?
- Using add_image_size in functions.php to get original uploaded file
- show all the posts thumbnails
- the_excerpt function not showing image
- How do I change where “You must be logged in to comment” directs users?
- Login cookies blocked after customizing hashing method
- Order column custom date using pre_get_posts
- Class parameter in get_avatar args doesn’t get added to output
- Remove text after a dot and a colon in Woocommerce product title
- Enable CORS Request for hostname using IP address
- Extend WordPress Audio Widget to accept HLS (.m3u8) URLs
- locking content with overlay/pop up ads
- How to use `foreach()` in ajax call
- Change default color scheme in twentyfifteen child theme?
- Restrict access if logged out except for homepage
- Add meta data in head from theme
- DISABLE wordpress upgrade page
- How to echo the translated custom field?
- How to list out all values from the get_body_class array
- Moving CDN jQuery to footer does not work?
- Mail function not working with user accounts
- Function not recognizing page id?
- Register a widget area when a theme option has been saved?
- Blank child theme – functions.php problem
- Conditionally write open graph meta property in header
- Pull random comment from specific post, display on homepage with shortcode
- Give a function a unique ID
- Hiding Google Analytics code based on theme options
- How do I make my child theme’s CSS update when I save it?
- Vimeo video play button color function
- User’s Comments Number: Storing it in a meta field for different uses
- Show one post per author and order by custom field
- Text before price on WooCom
- How to store page-{id}.php templates in custom folder