There is no difference when you are using just echo
. What works different is unset()
:
function test_unset_1()
{
global $post;
unset( $post );
}
function test_unset_2()
{
unset( $GLOBALS['post'] );
}
test_unset_1();
echo $GLOBALS['post']->ID; // will work
test_unset_2();
echo $GLOBALS['post']->ID; // will fail
The reason is that unset()
destroys just the local reference in the first case and the real global object in the second.
For readability use always $GLOBALS['post']
. It is easier to see where the variable is coming from.
Related Posts:
- How to: inspect global variables in WordPress
- get_post() vs global $post or $GLOBAL[‘post’]
- How to deal with global information without creating a Singleton class
- Accessing $post global on a custom post type archive page
- get_query_var vs global query variables?
- What is the global $wp object used for?
- Change global values from templates
- Will $current_user no longer be global?
- How to avoid repeatedly use the new statement to instantiate a class? [closed]
- Is $page a global variable in wordpress?
- How can i use a global variable in a .css file generated with php?
- Current page id returns the incorrect value
- Echo get_the_category() outside of loop (global?)
- How does WordPress make its functions globally available?
- Recommendations on accessing current user data in WordPress
- WP user agent returns random variables
- How to get particular data from wp_list_comments outside the loop?
- How to save a viewer specific option
- What is the correct way to obtain access to the WP_oEmbed object?
- $GLOBALS[‘value1’] is not working
- Creating a (global)-mapping
- wp localize script makes variable global, how to solve that?
- using globals from wp_link_pages function
- How can one use variables in a template or template part without polluting the global scope?
- How to set and use global variables? Or why not to use them at all
- Why is WordPress code so “space-happy”?
- Should we trust the post globals?
- What is a good alternative to using $content_width for image optimization?
- Why isn’t WordPress part of Framework Interop Group?
- When to use global $post and other global variables?
- When to use Exceptions vs Error Objects vs just plain false/null
- WordPress and event-driven programming – what is it about?
- Where to get information about array fields in $_REQUEST?
- What is the difference between using global $current_screen and get_current_screen()?
- Why declare $post globally?
- Avoiding “Usage of a direct database call is discouraged”
- Nonce in settings API with tabbed navigation
- How to set global variable in functions.php
- Passing variables to templates (alternatives to globalizing variables)
- Should I global $wpdb outside of any of my plugin’s functions scope?
- How should I store global information such as a phone number so that it is editable through the CMS?
- JavaScript, best way to use data from the loop
- Are we allowed to use the Allman (BSD) indent style when coding WordPress plugins and themes?
- Multisite Global Custom Posts
- WordPress Multisite – global categories
- PHPCS: Strings should have translatable content
- Assignments must be the first block of code on a line Validation Error on Travis
- Add classname comment template from functions.php
- Defining a global array in functions.php?
- WordPress mode for emacs?
- Call global variable array() in woocommerce child/template
- Override global query results without hooks
- WordPress Loop and $post
- How to prevent XSS alter custom global javascript object & methods in WordPress
- Create a new query in function.php to filter blog posts
- Relative redirects for posts, categories, products and product categories
- Add comments for template variables
- Translate a Constant while appeasing WordPress PHPCS
- Declare Global Variable In OOP PHP
- Does WP have a global of $id?
- What is the use of $content_width?
- Use global variables or function that returns said variables for site-wide private-ish WP settings?
- Why the WP Core team does not allow filter_* functions? [closed]
- WordPress Theme Validator?
- Understanding how the class family `inner-container` works
- WordPress Image Slider Looping Title Not Working
- Declare global var from Template File and use it in Functions.php
- how to get the post id in plugin
- get_posts() and global variables
- Passing global variable from header.php to functions.php
- Where is this function’s callback getting its arguments from?
- keep a variable for all pages in memory from the url
- Translation issue with global variables
- How could I better initialize a method from my class?
- How to get submenu of admin menu?
- Detecting current page from class method
- Retrieve post in AJAX Callback
- ‘global’ not working in wordpress?
- Is there a common structure for a wordpress formular?
- SFTP define in wp-config.php
- cannot get global $menu value in form request
- Custom global variable not working in function
- Hide payment method based on user role AND minimum cart value
- Global $wp_admin_bar always returns null
- How do I get a reference to the HTML memory buffer while the page is being constructed?
- Standard technique for AJAX post endpoint: WP REST or WP API?
- global categories – Share specific categories in wordpress multisite
- PHP Code Sniffer – WordPress VIP Coding Standards
- Declare global variable that can be used on other function
- Popup panel is only displaying 1 entry ignoring all others
- Best way to access variables in template markup
- Sanitization html output itself
- Settings in functions.php used by a plugin
- Current URL path variable
- Save something to global var in add_filter
- key( $GLOBALS[‘wp_registered_sidebars’] ) is always showing the same value
- How do I access variables outside a function [closed]
- How to automatically convert “normal” conditions to yoda conditions
- Suppress errors when using global function
- $wpdb->prepare referencing a const without a coding guideline warning