In functions.php
you can create a constant with the user id, like this.
// if somehow this constant exists this will prevent a php error
if (!defined('CURRENT_USER_ID')) {
// if user is not logged it this constant will contain 0
define('CURRENT_USER_ID', get_current_user_id());
}
Now you have a constant variable available anywhere in your theme.
To access it you can simply call it, for example
echo CURRENT_USER_ID;
Related Posts:
- Adding fields to the “Add New User” screen in the dashboard
- How to check if a user exists by a given id
- Use author author display name in permalink structure for pages and posts
- Get a list of all available fields in the User Profile
- Logout redirect to current page – function
- How to paginate the get_users function?
- Create a global variable for use in all templates
- How to display random users with avatars
- How to manually activate user in dashboard only by admin
- Add new user : make the fields First Name and Last name required
- Passing variables to templates (alternatives to globalizing variables)
- wp_delete_user with username
- Change labels on ‘Nickname’ and ‘Biographical Info’ in user-edit.php
- Order get_users() by last login date. Is it possible?
- How can I tell when a user is logged in?
- how to get and display logged in user’s recently read posts
- Displaying a random user with a shortcode
- How to update BuddyPress xprofile fields programmatically? [closed]
- How to detect first visit of a user?
- Can I pass two roles to the function get_users?
- How can I add an image field to BuddyPress Extended Profile Fields? [closed]
- Create dropdown of users in admin dashboard
- password protected post policy
- Global functions on WPMU
- Remove the deleted users avatar from list
- Defining a global array in functions.php?
- Get Current User info using wp_localize_script, in functions.php
- How to add custom fields to my custom registration form
- create shortcode to list users with specific meta key value
- Don’t delete a page if it holds users
- How to make custom column Admin>Users sortable?
- Execute a ultimate member action when user role is updated
- Create a new query in function.php to filter blog posts
- Filter out other users comments. Visualize only own comments and editor role users’ comments
- Force to use STRONG users password and implement rule to prevent REUSE [closed]
- Change label for registration Fields
- Condition OR for current user ID
- Passing global variable from header.php to functions.php
- Secondary Menu and Logged In Users
- Why get_user_by() doesn’t work in my code?
- Filter to wp_list_authors
- How to add array [closed]
- Getting the teaser text without overriding global variables
- Add Link to Users List (Backend) to open each users front-end profile
- Suggest Users basing on User taxonomy
- Showing user profile data on front-end
- Modify function to only return values for the user that’s logged in
- loginout function customization
- User meta and public function security
- call wp_insert_user in custom class doesnt work
- Get users with different roles and call function on each of them (user_meta)
- Show errormessages on wrong username/password on custom loginform?
- How can I show different content for different user-levels?
- Custom user role still showing up after deletion, ideas?
- referencing the current user in a page i created in wordpress
- Show metadata in users.php – wordpress
- Deleting guests profile users after 1.5 hours
- wp_get_current_user return ID = 0 when used outside of wordpress, such as webhook / fulfillment dialogflow
- External api call and make global variable for any page visitor enters , page-home, page, single etc
- Allow user only create specific tags
- Add Change role button for list of user in an event
- get_current_user_id() always return 0 in if else statement
- How to access custom class methods from any include without using global
- Show users last read posts for each user?
- How to add number to wordpress count function?
- Setting a cookie in functions.php and it gets deleted when working on an other function
- Settings in functions.php used by a plugin
- get USER ID in functions.php using user_register action
- How do I access variables outside a function [closed]
- Whats the proper way to add a new user via a front end form?
- sort by date in users
- Add custom css to specific user
- Missing feature image link function
- What’s the difference between home_url() and site_url()
- Remove “Category:”, “Tag:”, “Author:” from the_archive_title
- get_template_directory_uri pointing to parent theme not child theme
- How to customize the_archive_title()?
- How to set and use global variables? Or why not to use them at all
- remove empty paragraphs from the_content?
- What is the “with_front” rewrite key?
- Why use if function_exists?
- How to override parent functions in child themes?
- wp_enqueue_script was called incorrectly
- Add multiple custom fields to the general settings page
- Ajax call always returns 0
- 400 bad request on admin-ajax.php only using wp_enqueue_scripts action hook
- How long does a deprecated function live in core?
- Solution to render Shortcodes in Admin Editor
- How to add a data attribute to a WordPress menu item
- What’s the difference between esc_html, esc_attr, esc_html_e, and so on?
- remove_action on after_setup_theme not working from child theme
- plugins_url vs plugin_dir_url
- Remove type attribute from script and style tags added by WordPress
- How to run a function every 5 minutes?
- Best way of passing PHP variable between partials?
- Upload Multiple Files With media_handle_upload
- How to display custom field in woocommerce orders in admin panel?
- Issues with title-tag and document_title_parts
- How do I get the current edit page ID in the admin?
- Why isn’t is_page working when I put it in the functions.php file?