Yes, as a best practice, you should call wp_current_user()
because you can’t assume that $current_user
is set (or if it is set correctly with the user you’re after).
Of course, there’s a third option that’s only just sprung to mind…
test whether $current_user is already set/defined and if it isn’t,
call wp_get_current_user().
Actually, that’s kind of what happens already. wp_get_current_user()
is just a wrapper for the private method _wp_get_current_user()
, so you can look at that function to see what it does. The first thing it does is check if $current_user
exists and is populated, and if so, it returns $current_user
.
To address your question of efficiency/inefficiency, since the method is already checking to see if $current_user
is loaded, it would actually be more inefficient if you were doing it, then running wp_get_current_user()
since that would be an unnecessary step.
Related Posts:
- Installing wp3.2.1 on IIS; getting empty sessions
- external wordpress pages using wp-blog-header
- Why WordPress uses 4 tables to manage terms
- How do I enable HTML5 prefetching on this page?
- Output a WooCommerce product custom field in WooCommerce using get_post_meta()
- Append a term to WooCommerce product existing product category terms
- user_id error: Only variables should be passed by reference
- How to find the output of contact form 7 shortcode? [closed]
- WordPress blockquote removes “ tag
- Is it necessary to upgrade to php7? [closed]
- How can I add pagination and how can I change thumbnail size?
- APC 500 Internal server error [closed]
- Call External Object in Class Function During Callback
- WordPress to end support for PHP 4?
- Why does WordPress remove my variables in the URL?
- get current custom post ID by WP_Query method
- Why do WordPress rewriites seem to work differently for posts vs pages?
- Performance impact of rewriting WordPress URLs
- WordPress: Access a plugin from within a theme
- Mute Debug Messages from Plugins
- Reprinting tags with all attributes
- wp_ob_end_flush_all and wp_suspend_cache_addition()
- edit_comment_link is not showing for comment author
- Css loads very slow [closed]
- Why is image hard cropping for one image using add_image_size not working?
- database sent to a JSON file
- How do I get around “Sorry, this file type is not permitted for security reasons”?
- How to change theme programmatically from a external application?
- can i fetch a custom metabox data in another page or post type?
- Hide Featured Image Meta Box on Editor Screen
- how to test for all children (including multilevel grand-childern) of page
- Dilemma of Populating all the categories in a drop down list
- Add item to top of menu using a filter in functions.php
- How to include seperate PHP file to class in callback function
- ‘Post-thumbnails’ feature does not seem to register
- JSON REST API WordPress only showing first 10 categories
- Password minimum length in personal subscription [closed]
- echo something inside a shortcode
- PHP include statement not working?
- How to pass a variable into an add_filter() function?
- Custom post type category not displaying category ID
- Admin-ajax php not working on new wordpress version
- Fatal error: Cannot redeclare function
- Issue Enqueueing WordPress style.css
- Add valid XHTML closing img tags to WordPress galleries
- Convert canvas to image and upload image to server
- How to get the filename from file system and create a download link?
- Only load certain artists on this page
- Using flickr api in custom wordpress plugin
- Toolbar Hidden in a Virtual Page
- Conditional for Post Format
- Prevent Buddypress Rewrite Rules on Non-Buddypress pages/posts
- Woocommerce order status payment
- How to Insert Ads After Every X Paragraph PHP
- How to define a rule in functions.php that ONLY disables the default wp functionality that undesirably changes ‘&’ to ‘#038;’?
- Dynamically added custom fields not displayed on WooCommerce email notifications
- End of blog roll or start of blog roll, _post_link = false / no link
- How to set a parrent page to single-post
- How to Remove Theme Style CSS inside Custom Plugin?
- Reading URL Parameters
- Issue in displaying the ongoing event and current events in the post
- If is_page include/else not working all of a sudden
- Run PHP 24hrs After Event Trigger
- Ajax result show in console if is called outside function and not showing in array
- Can I split a huge wp_postmeta table across different databases or servers?
- Attach and retrive multiple pdf files to post or page
- Link to index.php from home.php?
- ACF – Custom image for Product Tags not displaying
- Does WordPress disable system functions?
- WordPress/NGINX not respecting the category URL for new posts
- Display current user metadata on WordPress page
- Need help transforming echo to return for use with shortcode
- WordPress AJAX PHP(html) file that is within WordPress Loop
- Add Shortcode to ::before class
- Filtering markers on google map by WordPress category[SOLVED]
- How to display custom seo title before the loop?
- Parsing webhook from Shopify in WordPress
- scroll scrpit to particular button position to next id when click button
- Check if a user is logged into my WordPress site which is on a different server
- How to add text before posts
- WordPress / PhpStorm / XDebug and plugin_dir_path issues
- Add div after every 4 posts then every 2 posts for a responsive loop
- Issue adding featured image option to functions.php
- What to create new post using wp_insert_post, but it repeat the post infinitely
- Non-static method error when trying to use class in widgets
- Allow the execution of an external PHP script for logged users
- Get terms of the posts with one query
- Changes in header.php not reflecting
- Modify php code to pass a page id as a parameter in order to create a breadcrumb
- Newly published content gives 404 on wordpress
- WordPress 2014 Theme – Featured Content with multiple tags?
- Get unique post in parent category
- How to display the date under the post title?
- What is proper way to use the php if statement? [closed]
- Parse error: syntax error, unexpected ‘}’ in C:\xampp\htdocs\admin\products.php on line 148 [closed]
- Advanced Custom Fields Show PHP in Text Areas
- Redirect loop upon installation of my plugin
- Find owner of coupon
- WordPress wp_remote_request() – How to do async await in wordpress php?
- How Create Automatically Image jpg.webp from image .jpg without delete or change original images upload