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
- Ajax call always returns 0
- Where and how to put inline js in pages
- Overriding methods in a child theme
- WP disobeys disabling of the posts revision feature
- Unable to login using username
- How to change the order of Jquery in the footer of my theme?
- adding custom script to functions file
- add_action template_redirect not working for home page
- Way to store featured image width and height in two separate variables?
- Insert template-part inside the content
- Add unique id to Preview URL
- Numbering lines of code with the tag?
- Latest Foursquare Checkin function
- 404 when enqueue_script using plugin_url
- WP Customizer API loaded into functions.php
- How Can I add a menu to the theme from withen the function.php file
- How to hide content on mobile in the functions.php file – woocommerce
- Load templates, pass arguments, and render output from functions.php
- WordPress Lite/Light
- Custom global variable not working in function
- Is_Page doesnt detect my page
- Problems adding a new field to product in cart
- Create html from function
- Admin Panel errors because of functions.php
- Using Output from one Function and calling it into another
- How to add a class and title attribute to the link generated by next/previous post
- “Headers already sent” while trying to add a CSS file to my login page?
- Notice: Use of undefined constant REQUEST_URI – assumed ‘REQUEST_URI’ in ….functions.php on line 73
- Is storing in a variable the result of a fonction a most powerful way to code an if structures in WordPress?