Support Forum
Support › MB User Meta › Custom User Fields not showing in WordPress back endResolved
Hi there, I am trying to add a custom field to my Users in WordPress, using Meta Box.
I have MB User Meta and MB User Profile installed via the Meta Box AIO plugin.
I have created a custom User Role called 'Customer' in WordPress (this was created using the 'Members' plugin). And I want my custom field of 'Company' to be avaiable for all Users who have the Role of Customer
In Meta Box > Custom Fields I have created a Field Group (called 'Customner Fields'), set to 'Location' = User and 'Advanced location rules' set to 'Show' when 'any' conditions match > 'User Role = Customer'.
I then clicked the 'Generate' button, where I am given this code:
<?php
add_filter( 'rwmb_meta_boxes', 'show_customer_company_name' );
function show_customer_company_name( $meta_boxes ) {
$prefix = '';
$meta_boxes[] = [
'title' => __( 'Customer Fields', 'customer-company-name' ),
'id' => 'customer',
'type' => 'user',
'include' => [
'relation' => 'OR',
'user_role' => ['customer'],
],
'fields' => [
[
'name' => __( 'Company', 'customer-company-name' ),
'id' => $prefix . 'customer_company',
'type' => 'text',
'admin_columns' => 'after title',
],
],
];
return $meta_boxes;
}
I have inserted that code into the 'functions.php' file in my child theme.
However, when I go into any of the users (User > All Users > Select a user) I have created which have the user role of 'Customer', I do not see my custom field on the screen.
Please can someone advise why this is not working?
Thanks in advance for your help,
Keith
Hi,
The setting user_role
is to used for the current user role. You do not have the role customer
to edit the fields.
If you also want to edit the field, please use the setting edited_user_role
. Read more on the documentation https://docs.metabox.io/extensions/meta-box-include-exclude/
Hi Long, thank you for coming back to me.
I'm a bit confused here - the code I am using (in my functions.php) is the code generated by Meta Box.
So are you saying that the code which Meta Box generates, is incorrect?
If so, why does it not generate the correct code? I don't understand.
Please see my attached screenshots (which are labelled from 1 to 5 in order), showing you what I am doing. I am thinking maybe you did not understand my original question, as there is no mention at all in the documentation for Meta Box, that I need to do what you have said in your response, to add a custom user field?
Thanks again for your help,
Keith
Hi,
As I explained above, the setting user_role
supports showing custom fields for the current (logging) user role. You are logging with the user vP5rT2bZ
which has the Administrator role, not the Customer role. Can you please log in admin dashboard with the user test-customer1
and check the custom fields?
Hi Long, yes you explained, but I did not understand - your explanation was not very helpful, just redirecting me to a documentation page is not very helpful. Also your documentation is not very good (maybe for experienced Meta Box users it is, but not for someone like me who is using Meta Box for the first time).
An example would be to say
"Keith, to achieve what you need, add this code to your functions.php file" followed by the line of code (that is the level of support I have had at Advanced Custom Fields or Toolset).
Anyway - so now, I think I understand what you are saying, that because when I am logged in as the Administrator role, that because that is not the 'Customer' role, that I can't see the field.
So what I need to do, is for the Administrator role to be able to edit the field, BUT the Customer role to just be able to view their 'Company' field when logged in (but NOT to be able to edit it).
Am I correct in saying that I just need to add this line of code to my code in the original thread:
'edited_user_role' => 'administrator',
Is that right? As like I say, your documentation is not very good for a beginner user, there isn't even an example syntax provided for the 'edited_user_role'.
Please consider improving your documentation for beginner users. Also, please can you add it to the 'settings' tab for a field group, so that I can get this code generated correctly by Meta Box itself (as I'm guessing that is the whole point of using Meta Box).
Many thanks for your help,
Keith
...and just to explain further from my last reply:
- the role 'Customer' does NOT have access to the back end, ONLY to the front end. I am building a dashboard which shows some information to 'Customer' when they log into their accouint on the FRONT END.
- I want for the role (and ONLY this role) of Administrator to be able tedit the custom field of 'Company' in the role of 'Customer'.
So I tried this code in my functions.php file:
add_filter( 'rwmb_meta_boxes', 'show_customer_company_name' );
function show_customer_company_name( $meta_boxes ) {
$prefix = '';
$meta_boxes[] = [
'title' => __( 'Customer', 'customer-company-name' ),
'id' => 'customer',
'type' => 'user',
'include' => [
'relation' => 'OR',
'user_role' => ['customer'],
'edited_user_role' => ['administrator'],
],
'fields' => [
[
'name' => __( 'Company', 'customer-company-name' ),
'id' => $prefix . 'customer_company',
'type' => 'text',
'admin_columns' => 'after title',
],
],
];
return $meta_boxes;
}
It did NOT work. I am logged in the WordPress back end as Administrator, I go to User > test-customer1 (which has the role of Customer) and I still cannot see my custom field 🙁
Please can you provide me with workable code to try in my functions.php file which will fix this.
Many thanks,
Keith
Hi Keith,
Thank you for your feedback.
We are trying to improve the documentation for the first-used and non-coding users. You are using the MB Builder so no need to export the code and add it to the file functions.php to create the custom fields, it will help you to create the field automatically. Please read more on the documentation https://docs.metabox.io/extensions/meta-box-builder/
Regarding the settings, if you do not want to allow the users to edit that fields, please use the setting user_role
and assign it to administrator
. It looks like the setting edited_user_role
is not working correctly. I will inform the development team to fix this issue.
And there is no option to let the user view
their user profile without editing it.
Hi Keith,
Please swap administrator
and customer
roles like this to include the user meta fields
'include' => [
'relation' => 'OR',
'user_role' => ['administrator'],
'edited_user_role' => ['customer'],
],
Let me know if it works.
Hi Long,
My apologies for the delay coming back to you - I had to work on another project for the last few weeks.
YES!! This has now worked for me, I can see my custom user field for 'Company' is now showing when I go into the individual User Account in the WordPress back end.
Thank you so much!