BuddyDev

Search

[Resolved] Conditionally create BP xprofile field group (programatically)

  • Participant
    Level: Initiated
    Posts: 6
    Razor KHan on #15574

    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.

  • Keymaster
    (BuddyDev Team)
    Posts: 24712
    Brajesh Singh on #15576

    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
    Brajesh

  • Participant
    Level: Initiated
    Posts: 6
    Razor KHan on #15585

    Hi Brajesh,

    Thank you for your help. I will give it a try and let you know.

    Thanks again.

  • Participant
    Level: Initiated
    Posts: 6
    Razor KHan on #15685

    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

  • Participant
    Level: Initiated
    Posts: 6
    Razor KHan on #15686

    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;
    }
    }

  • Keymaster
    (BuddyDev Team)
    Posts: 24712
    Brajesh Singh on #15706

    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.

  • Participant
    Level: Initiated
    Posts: 6
    Razor KHan on #15755

    Yeah got this done. Thank you so much for your kind support.

  • Keymaster
    (BuddyDev Team)
    Posts: 24712
    Brajesh Singh on #15757

    You are welcome.

The topic ‘ [Resolved] Conditionally create BP xprofile field group (programatically)’ is closed to new replies.

This topic is: resolved