Created
November 26, 2021 05:56
-
-
Save GLWalker/164d2904137a14fed97d71a37c5ca459 to your computer and use it in GitHub Desktop.
Prevent duplicate BuddyPress group names
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Check if the given BuddyPress group name is duplicate. A group with same name already exists. | |
* | |
* @abstract https://buddydev.com/prevent-duplicate-buddypress-group-names/ | |
* @since 1.0.0 | |
* @param string $name name to be checked. | |
* @param int $group_id group id(exclude this group from test). | |
* | |
* @return bool | |
*/ | |
function systmp_bp_duplicate_group_name($name, $group_id = 0) | |
{ | |
global $wpdb; | |
$bp = buddypress(); | |
$table = $bp->groups->table_name; | |
$sql = $wpdb->prepare("SELECT id FROM {$table} WHERE name = %s", $name); | |
// except this group, used for updating. | |
if ($group_id) { | |
$sql .= $wpdb->prepare(" AND id != %d", $group_id); | |
} | |
return $wpdb->get_var($sql); | |
} | |
/** | |
* On BuddyPress Group Creation, Prevent duplicate group name. | |
* | |
* @abstract https://buddydev.com/prevent-duplicate-buddypress-group-names/ | |
*/ | |
function systmp_bp_check_duplicate_group_name_create() | |
{ | |
// If we're not at domain.org/groups/create/ then return false. | |
if (!bp_is_groups_component() || !bp_is_current_action('create')) { | |
return; | |
} | |
// If no current step is set, reset everything so we can start a fresh group creation. | |
buddypress()->groups->current_create_step = bp_action_variable(1); | |
if ('group-details' != bp_get_groups_current_create_step()) { | |
return; | |
} | |
if (empty($_POST['group-name'])) { | |
return; | |
} | |
// duplicate. | |
if (systmp_bp_duplicate_group_name($_POST['group-name'])) { | |
bp_core_add_message(__('Please use a different name.'), 'error'); | |
bp_core_redirect(trailingslashit(bp_get_groups_directory_permalink() . 'create/step/' . bp_get_groups_current_create_step())); | |
} | |
} | |
add_action('bp_actions', 'systmp_bp_check_duplicate_group_name_create', 9); | |
/** | |
* On BuddyPress Group Creation, Prevent duplicate group name. | |
* | |
* @abstract https://buddydev.com/prevent-duplicate-buddypress-group-names/ | |
*/ | |
function systmp_bp_check_duplicate_group_name_update() | |
{ | |
if (!function_exists('bp_get_group_current_admin_tab') || 'edit-details' != bp_get_group_current_admin_tab()) { | |
return; | |
} | |
if (empty($_POST['group-name'])) { | |
return; | |
} | |
// duplicate. | |
if (systmp_bp_duplicate_group_name($_POST['group-name'], $_POST['group-id'])) { | |
bp_core_add_message(__('Please use a different name.'), 'error'); | |
bp_core_redirect(bp_get_group_permalink(groups_get_current_group()) . 'admin/edit-details/'); | |
} | |
} | |
add_action('bp_screens', 'systmp_bp_check_duplicate_group_name_update', 9); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment