Opened 9 years ago
Last modified 8 years ago
#6714 assigned enhancement
Filtering messages in bp_core_add_message()
Reported by: | dcavins | Owned by: | tw2113 |
---|---|---|---|
Milestone: | Awaiting Contributions | Priority: | low |
Severity: | minor | Version: | 2.3.3 |
Component: | Core | Keywords: | |
Cc: | dcavins |
Description
It's easy to change message strings for all cases by creating a language file, but there's currently no way to filter the messages added via bp_core_add_message()
in more specific cases.
It seems there are (at least) two ways to make these messages filterable:
- Add filters inline when messages occur, in a more ad hoc approach:
if ( ! empty( $errors ) ) { $message = __( 'Please make sure you fill in all required fields in this profile field group before saving.', 'buddypress' ); $profile_field_group = bp_action_variable( 1 ); $message = apply_filters( 'xprofile_screen_edit_profile_required_fields_empty_message', $message, $profile_field_group ); bp_core_add_message( $message, 'error' ); }
- Add a catch-all filter at the function level, which would require the call to
bp_core_add_message()
to include useful filter parameters, like context and any extra args (which would probably vary by situation):if ( ! empty( $errors ) ) { bp_core_add_message( __( 'Please make sure you fill in all required fields in this profile field group before saving.', 'buddypress' ), 'error', 'saving_profile_field_group', array( 'updated_profile_field_group' => bp_action_variable( 1 ), 'reason' => 'required_fields_missing' ) ); }
I've drafted an example of the second approach, but I'm not sure that it's superior. The first might actually be easier for developers to use, since they wouldn't have to chase the args and context back to the original instance. Or maybe having a grand-central-station approach is easier for developers. I'm not sure.
Would this be useful? Is there another approach that would be better? Would there be some way to standardize the $args
array so that it works for most cases with a few common keys? This function is called 175 times in BP, so there is a lot of variety in its use.
Thanks for your feedback.
Attachments (1)
Change History (6)
#2
@
9 years ago
The _x()
i18n function could be a 3rd option? For example:
$message = _x( 'Message', 'Context', 'text-domain' ); bp_core_add_message( $message...
Ref: https://codex.wordpress.org/I18n_for_WordPress_Developers#Disambiguation_by_context
#3
@
9 years ago
- Keywords has-patch removed
- Milestone changed from Under Consideration to Future Release
I think this has some merit
Example of new params in bp_core_add_message.