Tagged: xprofile field group
Hi brajesh,
Thanks in advance for checking my post.
I am sorry for what I am gonna ask you as the problem has nothing to do with any of your products. I have been trying to find a solution for the problem but could not find any so you are my last hope.
I need to create a xprofile field group (not a field). I can actually do that by using this function xprofile_insert_field_group(). It works fine.
But I need to check if the group exists before creating the group. For example, I need to do something like this:
if( ! is_profile_group_name( ‘About’ ) ) { //placeholder function
xprofile_insert_field_group( array( ‘name’ => ‘About’, ‘can_delete’ => true ) );
}I was unable to check this.
Any help from your end is really appreciated.
Hi Razor,
Thank you for posting. BuddyPress does not have a proper/efficient function for checking if the field group exists.You may define your own(put it in functions.php or bp-custop.php )
/** * Check if a field group exists. * * @param string $name group name. * * @return int */ function bpcustom_profile_field_group_exists( $name ) { $bp = buddypress(); global $wpdb; return $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$bp->profile->table_name_groups} WHERE name = %s", $name ) ); }
and after that you can use it to check if the groups exists or not.
Hope that helps.
Regards
BrajeshHi Brajesh,
Thank you for your help. I will give it a try and let you know.
Thanks again.
Hi Brajesh,
I tried to use the function, but it returns null regardless of what I pass. I tried to look into the profile object and find the table_name_groups, but can’t find it. I guess that’s the reason why it returns null.
Thanks
Hi Brajesh,
Is there any downside of using the following function:
public function is_profile_group( $name ) {
global $wpdb;
$group_id = $wpdb->get_var( $wpdb->prepare( ‘SELECT id FROM ‘. $wpdb->prefix .’bp_xprofile_groups WHERE name = %s’, $name ) );
if( $group_id ) {
return true;
} else {
return false;
}
}Hi Razor,
The globals(tablename) is set on bp_setup_globals action. It seems you might be calling the function too early. Try calling it on bp_template_redirect or bp_actions.The problem with second approach is it will be a problem on Multisite network active. Specially on on primary blog.
Yeah got this done. Thank you so much for your kind support.
The topic ‘ [Resolved] Conditionally create BP xprofile field group (programatically)’ is closed to new replies.