Missing filter to allow devs to extend which screens to show on
Support › MB User Profile › Missing filter to allow devs to extend which screens to show on
- This topic has 6 replies, 3 voices, and was last updated 1 year, 10 months ago by
Anh Tran.
-
AuthorPosts
-
April 26, 2023 at 7:00 PM #41620
pluginoven
ParticipantAs 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-metaThis 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.
April 26, 2023 at 7:57 PM #41623pluginoven
ParticipantUpdate. 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.June 14, 2023 at 6:20 PM #42194pluginoven
ParticipantJust 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'sRW_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 theis_edit_screen_
June 14, 2023 at 10:02 PM #42203Peter
ModeratorHello,
Thanks for your feedback. I will let our development team know about the filter hook and get back to you later.
June 15, 2023 at 6:42 AM #42213Anh Tran
KeymasterCan you please explain why do you need other screens? I don’t see any reason here.
June 15, 2023 at 4:33 PM #42220pluginoven
ParticipantI 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 MB 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.
June 16, 2023 at 9:50 AM #42240Anh Tran
KeymasterIt’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).
-
AuthorPosts
- You must be logged in to reply to this topic.