Support Forum
Support › MB User Profile › Missing filter to allow devs to extend which screens to show on
As this plugin includes the functionality of MB Meta User, this is the exact same issue as this one:
https://support.metabox.io/topic/expanding-user-meta-to-work-with-other-post-types/
from 2019. Since there was no result from that I posted the solution in the thread as well as this public repo on Github: https://github.com/baden03/mb-user-meta
This issue for the MB User Profile plugin stems from the following function in src/UserFields.php:
public function hide_in_admin( $html, $field ) {
if ( ! is_admin() ) {
return $html;
}
$screen = get_current_screen();
if ( ! is_object( $screen ) || ! in_array( $screen->id, [ 'profile', 'user-edit', 'profile-network', 'user-edit-network' ], true ) ) {
return $html;
}
return in_array( $field['id'], $this->fields, true ) ? '' : $html;
}
As you can see the array of accepted screens only contains:
'profile', 'user-edit', 'profile-network', 'user-edit-network'
This makes it not possible to expand to more advanced user profiles. Therefor a filter would be appreciated.
$screen = get_current_screen();
$edit_screens = apply_filters( 'rwmb_user_edit_screens', array( 'profile', 'user-edit', 'profile-network', 'user-edit-network' ) );
if ( ! is_object( $screen ) || ! in_array( $screen->id, $edit_screens, true ) ) {
return $html;
}
return in_array( $field['id'], $this->fields, true ) ? '' : $html;
I would (as always) be happy to submit a pull request.
Update. Since this plugin actually does include 'mb-user-meta', this is actually exactly the same issue as posted back in 2019. In this case, the file is found under:
vendor > meta-box > mb-user-meta > src > MetaBox.php
The function:
public function is_edit_screen( $screen = null ) {
if ( ! is_admin() ) {
return false;
}
$screen = get_current_screen();
return in_array( $screen->id, ['profile', 'user-edit', 'profile-network', 'user-edit-network'], true );
}
should be updated to:
public function is_edit_screen( $screen = null ) {
if ( ! is_admin() ) {
return false;
}
$screen = get_current_screen();
// Allow edit screens to be expanded
$edit_screens = apply_filters( 'rwmb_user_edit_screens', [ 'profile', 'user-edit', 'profile-network', 'user-edit-network' ] );
return in_array( $screen->id, $edit_screens, true );
}
I see that the array was changed from array()
to []
... yet the filter was not added.
Just a quick update.
While this technically works, keep in mind that the is_edit_screen
function will loop for each defined metabox. To make this more efficient, the core plugin's RW_Meta_Box
class would need to be modified to allow the $edit_screens to be defined and filtered once then passing this array over, and over and over again to the is_edit_screen_
Hello,
Thanks for your feedback. I will let our development team know about the filter hook and get back to you later.
Can you please explain why do you need other screens? I don’t see any reason here.
I would be delighted to explain, Tran!
The short version is because developers build things beyond the default WordPress setup and want to use this great set of meta box plugins to do so.
We are using WordPress along Meta Box and MB User Profiles to create rapid prototypes that include a large, and very complex set of Forms in the user profile area.So many fields, in fact, they needed to be grouped into Tabs. So Complex, in fact, we quickly outgrew using the Meta Box Tabs extension and started using a very clever plugin called WP-User-Profiles to separate the field groups into Tabs. These tabs and sub-tabs have their own screen id's such as: "admin_page_services" for when the user is viewing a tab called "services" under their own profile and "users_page_services" when an admin is viewing a users's service tab. So far so good? good.
Now, it's not a problem to have the meta-boxes appear in these new tabs in the user profile. we simply assign the post_types attribute when we create the metabox like so:
'type' => 'user',
'post_types' => ['admin_page_service','users_page_service','toplevel_page_service'],
The problem, and the reason we have modified the mb-user-profiles plugin--well, actually the mb-user-meta plugin that is included in this plugin--as described above for the last four years is that all scripts that make metabox so nice for users profiles such as the validation, special advanced fields and so on are hard-coded to only be loaded on "user profiles" that have screen ids of:
array( 'profile', 'user-edit', 'profile-network', 'user-edit-network' )
Feel free to reach out to me on Github or directly via email if you would like to have a closer look at how we are using your plugins. It's probably one of the more advanced use cases given the number of times pull requests have been submitted when we run into a limitation.
It’s like a custom profile page in the admin. I get it, and it’s a valid point.
I’m not sure if adding a filter will work, because the whole process might need other things. I’m happy to merge PRs on this if you have any. (I’ve invited you to join Meta Box org on Github).