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?
- Is there a way to natively return the full filesystem path when using $wp_styles?
- 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?
- WordPress Multisite – global categories
- How to pass arguments from add_settings_field() to the callback function?
- What is a good alternative to using $content_width for image optimization?
- Why isn’t WordPress part of Framework Interop Group?
- Passing variables from header.php to template and vice verca
- When to use global $post and other global variables?
- What is $post_id? is it a global variable in WordPress?
- How to name files of namespaced classes?
- WordPress Theme variables scope
- Sandwich Coding Standards
- When to use Exceptions vs Error Objects vs just plain false/null
- Actions, functions and conditionals
- WordPress and event-driven programming – what is it about?
- Any advantage of using wp_scripts and is_IE when enqueuing scripts
- Why does WordPress reverse conditional statements?
- Reducing the use of global $post
- Does an activated plugin automatically mean its methods are available to other WP functions?
- Where to get information about array fields in $_REQUEST?
- Global Objects and Public Methods
- Custom plugin: Trying to show saved data on frontend
- How to call a function only once (global variable scope)
- How should I document function calls?
- What is the difference between using global $current_screen and get_current_screen()?
- Why declare $post globally?
- PHP Coding Standards, Widgets and Sanitization
- how to escape wp_oembed_get for phpcs
- Create a global variable for use in all templates
- Avoiding “Usage of a direct database call is discouraged”
- When and Where is `global $post` Set and Available?
- Registering AJAX callback function that is part of a class without instantiating the class in function.php
- Filtered query_vars becomes global. Why does this work?
- query_posts() in function makes global $wp_query out of sync?
- Nonce in settings API with tabbed navigation
- How to set global variable in functions.php
- Passing variables to templates (alternatives to globalizing variables)
- What to do when theme and WordPress coding standards conflict?
- Retrieve the template directory URI via global or get_template_directory_uri() every time?
- WP Coding standards – escaping the inescapable?
- Detecting Embed URLs Within post_content
- using 1 form shortcode (si or cf7) for all multisite sites [closed]
- Filter the blog title displayed in the header
- Filter the blog’s title without using global variables
- How to access global $wp_meta_boxes variable on front-end?
- Multisite Installation: how do I setup global search?
- global $wp_meta_boxes returns NULL
- Save last login date in global before change it?
- How to check if this is the nth instance of a given shortcode in a post
- Passing variables to template parts
- Should I global $wpdb outside of any of my plugin’s functions scope?
- What is WordPress file encoding?
- How should I store global information such as a phone number so that it is editable through the CMS?
- How Flexible are the WordPress Coding Standards for PHPCS?
- JavaScript, best way to use data from the loop
- Correct processing of `$_POST`, following WordPress Coding Standards
- store custom WP table names in a global variable
- How to set up phpcs with WordPress coding standard with PHP8?
- Are we allowed to use the Allman (BSD) indent style when coding WordPress plugins and themes?
- WP 4.4.1 allow empty comments via add_action ‘pre_comment_on_post’
- Multisite Global Custom Posts
- Pass variables from one widget to another widget
- Hyphens instead of Underscores in Post-type Archive Template Filenames when Post-type handle contains underscore
- Adding a section to admin menu for global settings
- WordPress Multisite – global categories
- Using query_posts inside single.php loop
- Can I use namespaces in my plugin?
- PHPCS: Strings should have translatable content
- How does the $post_type global variable work?
- How to fix a “globals” issue to avoid a rejected theme?
- after_setup_theme, Global Variable and Theme Customizer
- Global functions on WPMU
- How/where is the global variable $wp_registered_widgets filled?
- Assignments must be the first block of code on a line Validation Error on Travis