Skip to:
Content

BuddyPress.org

Opened 9 years ago

Last modified 8 years ago

#6714 assigned enhancement

Filtering messages in bp_core_add_message()

Reported by: dcavins's profile dcavins Owned by: tw2113's profile 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)

6714.01.patch (2.9 KB) - added by dcavins 9 years ago.
Example of new params in bp_core_add_message.

Download all attachments as: .zip

Change History (6)

@dcavins
9 years ago

Example of new params in bp_core_add_message.

#1 @DJPaul
9 years ago

  • Milestone changed from Awaiting Review to Under Consideration

#2 @henry.wright
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 @DJPaul
9 years ago

  • Keywords has-patch removed
  • Milestone changed from Under Consideration to Future Release

I think this has some merit

#4 @tw2113
8 years ago

  • Owner set to tw2113
  • Status changed from new to assigned

This ticket was mentioned in Slack in #buddypress by dcavins. View the logs.


8 years ago

Note: See TracTickets for help on using tickets.