Skip to:
Content
Pages
Categories
Search
Top
Bottom

Probleme using BP core functions

  • @mika89

    Participant

    Hello everyone !

    I’m currently developping a plugin on BP(2.0). The plugin has to remove inactive members from users friends list. When I’m trying to get the friends list of each user, I use friends_get_friend_count_for_user() function and I get this error:

    Fatal error: Call to undefined function friends_get_friend_count_for_user() in /***/***/***/***/wp-content/plugins/myPlugin/myPlugin.php on line 51

    What do I have to do to use BP core functions ?

    Thanks in advance

Viewing 25 replies - 1 through 25 (of 26 total)
  • @henrywright

    Moderator

    Hi @mika89

    You should be able to use friends_get_friend_count_for_user() in your plugin. Are you sure you have BP activated? Are you sure you have the Friends component enabled?

    @mika89

    Participant

    Hi ! Thanks for your answer.

    Well, I’m a begginer in BP dev so I must have forgot something.
    BP is activated, but what do you mean by “Friends component enabled” ? If it means that users can have friends, yes it’s enabled.

    When you develop a plugin isn’t there issues of dependencies ? Like, shouldn’t I include some files ? I tried :

    require_once(ABSPATH . '/wp-content/plugins/buddypress/bp-friends/bp-friends-functions.php');
    require_once(ABSPATH . '/wp-content/plugins/buddypress/bp-friends/bp-friends-classes.php');

    But everytime it tells me that an other function is undefined, so I don’t think it’s the right way to do it…

    @mika89

    Participant

    Here is the code I’m trying to make work. Maybe it’ll help:<?php

    /*
    Plugin Name: My Plugin
    Plugin URI: https://*******.com/
    Description: Remove inactive users from friends
    Version: 0.1
    Authors: mika89
    */
    
    /* Definition des constantes */
    define('BP_AI_PLUGIN_NAME', 'myPlugin');
    define('BP_AI_PLUGIN_URL', plugins_url('',__FILE__));
    define('BP_AI_PLUGIN_URL_JS', plugins_url('js',__FILE__));
    define('BP_AI_PLUGIN_URL_CSS', plugins_url('css',__FILE__));
    define('BP_AI_PLUGIN_URL_IMG', plugins_url('images',__FILE__));
    define('BP_AI_PLUGIN_DIR', WP_PLUGIN_DIR.'/'.BP_AI_PLUGIN_NAME);
    define('BP_AI_PLUGIN_VERSION', '0.1');
    
    /* Return the current day of the week */
    function getToday(){
    	$jd=cal_to_jd(CAL_GREGORIAN,date("m"),date("d"),date("Y"));
    	return jddayofweek($jd,1);
    }
    
    /* Update users to inactive role */
    function updateToInactive(){
    	if(getToday() == "Monday"){
               /* Not done yet*/
    	}
    }
    
    /* remove inactive members from friends */
    function removeInactiveFromFriends(){
    	$args = array('count_total' => true);
    	$user_query = new WP_User_Query($args);
     	?>
            <pre>
            <?php
    	// User Loop
    	if ( ! empty( $user_query->results ) ) {
    		foreach ( $user_query->results as $user ) {
    			$user_friends = friends_get_friend_count_for_user($user->id);
    			echo '<p>' . $user->display_name . '</p>';
    			foreach ( $user_friends->results as $friend ) {
    				echo '------------<p>' . $friend->display_name . '</p>';
    			}
    		}
    	} 
    	else {
    		echo 'No users found.';
    	}
    
    	?></pre><?php
    }
    removeInactiveFromFriends();
    ?>

    @henrywright

    Moderator

    As far as I’m aware you don’t need to include any files in order to use BP core functions. I’ve never had to. Perhaps someone else here can confirm?

    @henrywright

    Moderator

    Just looked at your plugin code and think you may be using the function wrong. I could be wrong but you seem to be treating what is returned as an object where as it will be either an array of user IDs, or false if none are found.

    @mika89

    Participant

    Actually you’re right, it’s not really the function I need coz it returns an int, whereas I need an array of users (friends). But I have been running some tests and whatever the core function I use, I get a call to undefined function error 🙁
    What am I missing ??

    @shanebp

    Moderator

    @mika89

    You’re probably calling the BP function before BP has loaded.
    Read this: https://codex.buddypress.org/plugindev/checking-buddypress-is-active/

    There may be additional problems with your code including the issue raised by @henrywright

    @henrywright

    Moderator

    I the function is the one you need to use, you’ll just have to do a little more work to get info about each friend. Loop through the array that’s returned by friends_get_friend_count_for_user() and then use get_userdata() like this:

    $friends = friends_get_friend_count_for_user( $user_id );
    if ( $friends != false ) {
        foreach ( $friends as $friend ) {
            $friend = get_userdata( $friend->ID );
            echo $friend->display_name . '<br />';
    } else {
        // this user is friendless
    }

    @mika89

    Participant

    Thank you to both of you @henrywright and @shanebp !
    I did have to wait for BP to be loaded. Now I can use the core functions.

    Besides, I tried your code @henrywright but all my users seem to be friendless lol. Are you sure it’s the good function to use ? Coz in bp-friends-functions.php, friends_get_friend_count_for_user() is declared like this:

    /**
     * Get a total friend count for a given user.
     *
     * @param int $user_id Optional. ID of the user whose friendships you are
     *        counting. Default: displayed user (if any), otherwise logged-in user.
     * @return int Friend count for the user.
     */
    function friends_get_friend_count_for_user( $user_id ) {
    	return BP_Friends_Friendship::total_friend_count( $user_id );
    }

    So it does return only an int and not an array. Unless I don’t understand ^^

    @henrywright

    Moderator

    Sorry, you’re right!

    friends_get_friend_user_ids( $user_id ) should be what you need. So change my first line to:

    $friends = friends_get_friend_user_ids( $user_id );

    @mika89

    Participant

    It doesn’t seem to work :(.
    Maybe I’m not doing it in the good way…

    @mika89

    Participant

    It doesn’t seem to work :(.
    Maybe I’m not doing it in the good way…

    I also tried the function

    /**
     * Search the friends of a user by a search string.
     *
     * @param string $filter The search string, matched against xprofile fields (if
     *        available), or usermeta 'nickname' field.
     * @param int $user_id ID of the user whose friends are being searched.
     * @param int $limit Optional. Max number of friends to return.
     * @param int $page Optional. The page of results to return. Default: null (no
     *        pagination - return all results).
     * @return array|bool On success, an array: {
     *     @type array $friends IDs of friends returned by the query.
     *     @type int $count Total number of friends (disregarding
     *           pagination) who match the search.
     * }. Returns false on failure.
     */
    function friends_search_friends( $search_terms, $user_id, $pag_num = 10, $pag_page = 1 ) {
    	return BP_Friends_Friendship::search_friends( $search_terms, $user_id, $pag_num, $pag_page );
    }

    I called it this way : friends_search_friends('', $user_id, 0, 0);
    But doesn’t work

    @henrywright

    Moderator

    Can you paste how you’re using friends_get_friend_user_ids()?

    @mika89

    Participant
    function removeInactiveFromFriends(){
    	global $bp;
    	$args = array('count_total' => true);
    	// The Query
    	$user_query = new WP_User_Query($args);
    	// User Loop
    	if ( ! empty( $user_query->results ) ) {
    		foreach ( $user_query->results as $user ) {
    			echo '<p>' . $user->display_name . '</p>';
    			$friends = friends_get_friend_user_ids( $user->ID );
    			if ( $friends != false ) {
    			    foreach ( $friends as $friend ) {
    			        $friend = get_userdata( $friend->ID );
    			        echo "------------".$friend->display_name . '<br />';
    			    }
    			} else {
    			    echo "----------friendless !";
    			}
    		}
    	}
    	else {
    		echo 'No users found.';
    	}
    
    	?></pre><?php
    }

    @henrywright

    Moderator

    Try changing:

    $friend = get_userdata( $friend->ID );

    to:

    $friend = get_userdata( $friend );

    @mika89

    Participant

    Same result…
    All users are friendless

    @henrywright

    Moderator

    All I can think of is friends_get_friend_user_ids( $user->ID ); doesn’t return an array as we expect. I just looked at the documentation and it doesn’t specify the return type. Maybe try echo’ing out friends_get_friend_user_ids( $user->ID ); to see what exactly is returned?

    @henrywright

    Moderator

    something like

    $var = friends_get_friend_user_ids( $user->ID );
    var_dump( $var );

    @mika89

    Participant

    When I echo it, I just get “Array”…

    @mika89

    Participant

    array(0) {
    }
    array(0) {
    }
    array(0) {
    }
    array(0) {
    }
    array(0) {
    }
    array(0) {
    }
    array(0) {
    }
    array(0) {
    }…………..

    With var_dump…

    @henrywright

    Moderator

    This might be a silly question but check these members actually do have friends. The var_dump seems to indicate they don’t as an empty array seems to be returned.

    @mika89

    Participant

    Aha, I already checked, all the registered members have at least 1 friend…
    It’s crazy that something as simple as getting the list of friends has to be that complicated !

    @henrywright

    Moderator

    There might be a problem with $user->ID then? Can you echo that out to see if that is what you expect it to be?

    @mika89

    Participant

    Hello,

    Sorry for the late reply @henrywright,

    I tried to echo $user->ID for each each occurence of $user_query->results and I get all the Ids of users. So the problem is not there…

    @mika89

    Participant

    I just figured out that whatever the BP function I use it always return a boolean false !! What’s wrong with my code ??

    if ( !function_exists( 'bp_core_install' ) ) {
    	require_once( ABSPATH . '/wp-admin/includes/plugin.php' );
    	if ( is_plugin_active( 'buddypress/bp-loader.php' ) )
    		require_once ( WP_PLUGIN_DIR . '/buddypress/bp-loader.php' );
    	else
    		return;
    }
    
    /* remove inactive members from friends */
    function removeInactiveFromFriends(){	
    	global $wpdb;
    	$args = array('count_total' => true);
    	$user_query = new WP_User_Query($args);
    	if ( ! empty( $user_query->results ) ) {
    		foreach ( $user_query->results as $user ) {
    			echo '<p>' . $user->display_name . '</p>';
       			$sqlGetFriends = $wpdb->prepare( "SELECT friend_user_id FROM wp_bp_friends WHERE initiator_user_id = %s",$user->ID) ;
       			$friends_id = $wpdb->get_results($sqlGetFriends);
    			foreach ($friends_id as $friend_id) {
    					$friend = get_user_by('id',$friend_id);
    					var_dump($friend);
    					echo '-----------<p>' . $friend->display_name . '</p>';
    			}
    		}
    		
    	}
    	else {
    		echo 'No users found.';
    	}	?></pre><?php
    }
    
    add_action( 'bp_include', 'removeInactiveFromFriends' );
    ?>

    I var_dumped the user object I get with the function get_user_by() and it just prints

    boolean false
    boolean false
    boolean false
    boolean false
Viewing 25 replies - 1 through 25 (of 26 total)
  • The topic ‘Probleme using BP core functions’ is closed to new replies.
Skip to toolbar