slaFFik
3/30/2016 - 8:24 AM

BuddyPress prevent duplicate Group Names

BuddyPress prevent duplicate Group Names

<?php
function pp_check_group_name( $group_new ) {

	if ( 'group-details' == bp_get_groups_current_create_step() ) {
	
		$args = array( 
			'per_page'          => null, 
			'populate_extras'   => false, 
			'update_meta_cache' => false 	
		);
		
		$groups = groups_get_groups( $args );
		
		foreach ( $groups['groups'] as $group ) {
		
			if( $group->name == $group_new->name ) {
			
				$group_new->name = '';
				
				pp_group_name_error_message();
				
				break;
				
			}
		}
	}
}

add_action( 'groups_group_before_save', 'pp_check_group_name' );

function pp_group_name_error_message() {
	global $l10n;

	$mo = new MO();

	$mo->add_entry( 
		array( 
			'singular' => 'There was an error saving group details. Please try again.', 
			'translations' => array( __ ('That name is already being used. Please use a different name.', 'your-domain' ) ) 
		) 
	);
	
	if ( isset( $l10n['buddypress'] ) )
		$mo->merge_with( $l10n['buddypress'] );

	$l10n['buddypress'] = &$mo;
	unset( $mo );
}