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:
- 500 error after upgrading from 5.0 to 5.4 and adding empty functions.php
- I want to add the alt attribute to all the photos!
- How do I add schema markup to individual WordPress posts without using plugins?
- syntax error, unexpected ‘$id’ (T_VARIABLE) – where is the issue though? [closed]
- how to handle multiple forloop?
- Add product to cart from functions.php
- Parse error: syntax error, unexpected ‘endforeach’ (T_ENDFOREACH) in [closed]
- Need to use an external variable inside a function
- Owl Carousel and WordPress Integration Via WP_Enqueue
- Woocommerce – Checkout error message
- Add custom css to theme
- Trying to get this function to show below the content
- Removing Facebook contact field from user contact not working
- add variable to actions/functions across different files (woocommerce)
- How do grab the main loop, with conditions, and output via shortcodes
- Why functions metaboxes is causing White Screen in Admin [closed]
- Rename file after title , one small problem
- Help finishing script to export WP user data when form submitted
- is_preview() always return false
- How do I add functionality to images?
- Adding and updating repeating custom field meta data
- How do I make my child theme’s CSS update when I save it?
- replacing words to “…” [closed]
- Display function from functions.php in tag.php
- AJAX values converted to PHP Variables?
- How can I customize the size of the header of my theme?
- reusing code in function and running it with loop
- Sessions in word press [duplicate]
- How to hook wp_mail to add a custom email as BCC for each sent email?
- Custom Post Type Search
- Explode Array from Repeatable Custom Field
- Directing to functions.php the correct way
- Retrieving next_post_link() and previous_post_link() in functions.php
- Replace & with &
- Decimal stripped from metabox number
- Which template file to edit to edit homepage in Mystile [closed]
- Getting full code of wp_list_comments instead of replacing it
- Does functions.php apply to every page?
- Make a PHP file with a function
- Getting error on function.php
- preg_replace specific Text to small latter strtolower [closed]
- Trying to decipher this code which is encoded with HTML Special Characters [closed]
- Remove Disqus JavaScript from homepage
- Which action is triggered before final output?
- Save output of the_content_rss into variable
- Changed functions.php file and now site is blank [closed]
- get_post_class() not working well
- Missing argument 2
- Creating mixture of shortcodes to use in the visual/text editor
- PHP error in shortcode [closed]
- How can I json_encode the output of my function?
- Correct php syntax to add an image in wp functions.php
- Adding function to function.php error 505 [closed]
- How I can show short content with short tag
- Why don’t ‘wp_nav_menu’ CSS classes work until a menu is created?
- How to get postId from the argument $block when register_block_type()?
- WordPress is adding pagination for all pages not only for blog page, How to remove pagination for all pages except blog/posts page?
- image_size with respect to aspect ratios
- Removing all Category pages with one exception
- PHP Notice: Function is_page was called incorrectly. Conditional query tags do not work before the query is run. Before then, they always return false
- Add a second stock display [closed]
- How to fix enqueue_styles error for a twenty-seventeen childtheme
- “woocommerce_output_related_products” not working
- Automatically delete posts that aren’t in an array
- Ads Only at one website inside multisite network – code error
- Bulk set Post Title as Tag where Tag is Empty Function
- Different ads on every website on multisite environment
- Is mytheme_post_thumbnails() still necessary?
- Get full slug for a WordPress Post
- Problem with woocommerce recalculating tax on completed orders
- comment_post function with js not running
- WordPress Custom wp mail template return full template
- Problem with removing characters “<” – WpAllImport
- My website is generating weired url parameters of paginated pages
- Comment turned Invisible
- How to create a post without link in wordpress?
- How to resize image from import image url?
- Whats the proper way to add a new user via a front end form?
- When the WP_EDITOR button is pressed, the user is prompted to select a photo from their device
- How to crop image in WordPress with aspect ratio for any size of image
- Is storing in a variable the result of a fonction a most powerful way to code an if structures in WordPress?
- Vimeo video play button color function
- multible shortcodes (for differnt values) with one function
- User’s Comments Number: Storing it in a meta field for different uses
- Load script only on selected Pages
- Show one post per author and order by custom field
- Bad value crossorigin for attribute crossorigin on element link
- Insert image between X number of posts but on specific paginated pages?
- Animated Accordion [closed]
- Text before price on WooCom
- wp_head not injecting css
- Problem with lists and “allowed_block_types” in functions.php (since WP 6.1.1)
- How do i set global variable in a function in wordpress functions.php
- Warning: call_user_func_array() expects parameter 1 to be a valid callback
- Change Post status based on custom field date +1 day
- Get all users from role and add to dropdown (select) – wordpress, javascript
- How to add height and width dimensions automatically to archive descriptions
- Can’t find function which is called in a wordpress theme
- Call wordpress function through functions.php when pressing menu
- Working function() doesn’t execute when triggered by WP CRON