wp_get_current_user()
returns a user object. You can’t output objects directly.
Proceed by getting the users roles like $roles = (array)$user->roles;
. Note that this returns an array though, so you will probably want to use the first element with $roles[0]
.
Your code might look like this (untested):
function onboarding_update_fields( $fields = array() ) {
$user = wp_get_current_user();
$roles = (array)$user->roles;
$fields['customertype'] = $roles[0];
return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'onboarding_update_fields' );
Additionaly, you might want to check if the user is logged in, otherwise there might not be a user and role available.
WordPress Code Reference articles: wp_get_current_user, User object.
Related Posts:
- User registration followed by automatic login
- wp_insert_user role not working
- How to find cause of PHP notices with no stack trace?
- How to paginate a list of custom taxonomy terms?
- reason of splitting theme files to multiple files
- wordpress separate registration for different roles
- How to get user by display_name with WP_User_Query
- Changing Link Attributes for Wp_Link_Pages
- Problem with Woocommerce REST API Authentication
- How to get custom image size for image uploaded in Customizer
- Passing POST data from one WP post to another
- How do I add HTML to a PHP function [closed]
- register_activation_hook isn’t adding table to DB
- how to get the value of time left for a transient
- Call to undefined function issue
- Remove query string specific key value
- is_page “range” for if statement?
- How to Fetch values from WP database based on selected option value by onchange using PHP?
- wp_insert_post() is returning the correct post ID, no failure, but the post content does not get updated
- How to create a custom category of wordpress admin
- WordPress automatically adding html tag around PHP code
- WordPress child theme fails to override parent navigation menu in /inc/structure/header.php
- Customizer: Output default value in Customizer CSS
- Converting HTML Template to WordPress Theme
- Adding rel tag to all external links
- PHP Warning: chmod(): Operation not permitted in class-wp-filesystem-direct.php on line 173
- Saving an array of dynamic repeater data as post_meta
- Image rotation and editing stopped working in WordPress after upgrading Ubuntu from 16.04 to 18.04
- WordPress asks you for your FTP details if you want to run Automatic Updates/Upgrade Plugins
- Populate editor with some content of a page with a page template
- Show success or error messages in Ajax response to WordPress custom registration form
- How to add a Custom Mailchimp AJAX Newsletter Subscribe Form
- 404 error when I acess the second page of post-type
- Where can I create a normal php file?
- Trying do build a contact form
- How to Disable WordPress Page Revisions?
- Prev/Next Links Broken on Static Front Page Pagination
- How to integrate noimage picture in this code
- get comment data using comment id
- $_SESSION variables work perfectly on my local machine but returns NULL on development webserver
- PHP E_WARNING being shown despite php.ini [closed]
- Creating black and white copy of an image [closed]
- PHP code inside Shortcode
- preg_replace on all HTML content in WordPress
- Post class only applying to first excerpt; why?
- Add categorys to custom post type not working
- call adminbar greeting in other place
- PHP Warning: preg_match() expects & PHP Warning: strip_tags()
- Limit the number of child pages
- WP_LINK_PAGES Generating Unnecessary Tag
- Why is home page content not displaying with this rewrite rule?
- How to show upcoming events according to start date field
- Some problems in custom widget
- Call a function in functions.php from within tags (JS)
- Displaying images outside the content
- WordPress Conditional / Multiple Taxonomy Query
- Add class to ul and li in wp_na
- How can i programmatically change the SKU of a product based on product variation?
- Can I ‘order by’ date that is in a text field?
- Show related products by attribute based and certain conditions
- How to retrieve taxonomy Metabox fields in frontend
- PHP function for horizontal Woocommerce thumbnails and badges
- Creating a Function and Call It
- How to count active users on a specific page
- WooCommerce display price before add to cart [closed]
- How to route root path / to /xyz in nginx?
- Adding column to wpex_users
- Create dashboard widget for custom theme support
- The style.php file inside theme-engine folder has other content on FTP
- WordPress ajax filter returning all posts when it should be filtering by category
- WooCommerce Quantity in Dropdown menu instead of clasic [closed]
- Trying To Call a Dynamic Email Address
- How to override html codes in wordpress?
- Insert data from form to database
- My search results are only showing 5 posts?
- WP_query meta_query slow with OR and DATES
- Plugin Modification Change Functionality For Logged User Only
- How can I output the slug of a comment author?
- Matching multi user
- redirect 301 with special character like WIX site “#!”
- customizer live preview layout picker
- Display tab title as ‘blog tagline | blog title’
- Conditional statement within WP SQL query
- Modify category archive page loop on functions.php
- How to display html code on homepage
- two wordpress sites, two themes, one database, same content
- Check if values exists DB
- Trying to exclude posts from a category on the home page
- Php conditional help needed
- Adapting a php array to WordPress
- Different layout on search page depending on category post
- WordPress and magic quotes
- Dynamically pull the correct email address from a match
- How to quickly/easily make an analysis (reverse engineering) of WordPress?
- New to wordpress. Need Help with templates [closed]
- Issues trying to add a href statment with PHP into a PHP Echo Statement [closed]
- Help with if/else loop [closed]
- Hide category by tag
- How to add SKU and remove product title from WooCommerce url?
- php 8 compability admin interface