Skip to:
Content

BuddyPress.org

Opened 9 years ago

Closed 9 years ago

Last modified 8 years ago

#6752 closed defect (bug) (fixed)

Provide the group object to the groups template filters

Reported by: garrett-eclipse's profile garrett-eclipse Owned by: boonebgorges's profile boonebgorges
Milestone: 2.5 Priority: normal
Severity: normal Version: 2.4.0
Component: Groups Keywords:
Cc:

Description

Hello,

This is an enhancement request to update the groups template filters such as bp_get_group_name to provide the filter with the group object.

This is beneficial in my case as I have a group meta field for short_name which I want to use to replace the group name on the front-end.

Currently the function in bp-groups/bp-groups-template.php starting line 742:

<?php
/**
 * Output the name of the current group in the loop.
 *
 * @param object|bool $group Optional. Group object.
 *                           Default: current group in loop.
 */
function bp_group_name( $group = false ) {
        echo bp_get_group_name( $group );
}
        /**
         * Get the name of the current group in the loop.
         *
         * @param object|bool $group Optional. Group object.
         *                           Default: current group in loop.
         * @return string
         */
        function bp_get_group_name( $group = false ) {
                global $groups_template;
                if ( empty( $group ) ) {
                        $group =& $groups_template->group;
                }
                /**
                 * Filters the name of the current group in the loop.
                 *
                 * @since 1.0.0
                 *
                 * @param string $name Name of the current group in the loop.
                 */
                return apply_filters( 'bp_get_group_name', $group->name );
        }

This applies the bp_get_group_name filter supplying the $group->name.

In most cases this will come to play within the groups loop which means we can use the groups_template to retrieve the group object. But in any plugin or custom code which calls bp_group_name or bp_get_group_name while supplying a group object there's no way to retrieve this object within the filter function.

I'd love to see this filter and most others within the bp-groups-template.php file supply the group object.

As an example of how I want to use the filter see below, it expects there's a group meta field called bp_group_short_name:

<?php
        // Use Short Name for Groups on Front-end
        function wplms_replace_group_name($name, $group) {

            if ( is_admin() && ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) )
                return $name;

            $short_name = groups_get_groupmeta( $group->id, 'bp_group_short_name' );
            if ( ! empty( trim( $short_name ) ) ) {
                $name = $short_name;
            }

            return $name;
        }
add_filter('bp_get_group_name', array($this, 'wplms_replace_group_name'), 10, 2);

Let me know if I can clarify or provide any information.

Thank you

Change History (5)

#1 @boonebgorges
9 years ago

  • Milestone changed from Awaiting Review to 2.5
  • Owner set to boonebgorges
  • Status changed from new to assigned

Yes, this looks correct. Anytime a function like this accepts a $group parameter, the $group should also be passed to the filter. Thanks for the ticket, @garrett-eclipse.

#2 @boonebgorges
9 years ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 10383:

Pass group objects to some group template filters.

A number of group template functions accept a $group parameter, but are
generally used within the group loop, and infer the current group when no
object is explicitly passed to the function. Filter callbacks can generally
assume which group is intended when the filters are invoked within the loop
(by examining the $groups_template global), but this technique doesn't work
when the functions are used outside of the loop, with a $group passed as
a function parameter. In each of these cases, we should be passing the $group
object - whether inferred or explicit.

Props garrett-eclipse.
Fixes #6752.

#3 @garrett-eclipse
8 years ago

Opened new ticket to catch an instance where the $group object isn't being supplied to the bp_get_group_name filter in bp-groups-admin.php;
https://buddypress.trac.wordpress.org/ticket/7069

#4 @boonebgorges
8 years ago

In 10775:

Use bp_get_group_name() instead of redefining filter in various places.

Reduces code duplication, and ensures that changes to filter parameters are
automatically applied to every instance of the filter.

Props garrett-eclipse.
See #6752. Fixes #7069.

This ticket was mentioned in Slack in #buddypress by garrett-eclipse. View the logs.


8 years ago

Note: See TracTickets for help on using tickets.