Forum Replies Created
-
By the way it’s
add_action('bp_has_activities','my_denied_activity_new_friendship', 10, 2 );
Hi thanks for your answer, but I know that already, my point is that I need one activity loop with friends activity, groups I belong to, and my own ones. I m trying to use the filters but I m getting confused…
Thank u very much ! it’s exactly what I needed 🙂
Your code is working like a charm thank you 🙂
But you must be right about the get_user_meta cause your code also remove the button from members who are not supposed to be “inactive”. How can I retrieve the role of a member ? Like if he’s an administrator, i want to get the string “administrator”.Thanks !
Please anyone has an advice to give? It should be a little configuration I didn’t know..
Thank you
Please people ^^ I need help 🙂
Nobody ? 🙂
Hello @henrywright,
I managed to get the friends by using SQL, and it’s working. Now I really think that the problem is that I can’t use BP functions whatever the one I use it returns empty results. And I need several of them to develop my plugin… Any idea of why I can’t get to use them ?
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 printsboolean false boolean false boolean false boolean false
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…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 !array(0) {
}
array(0) {
}
array(0) {
}
array(0) {
}
array(0) {
}
array(0) {
}
array(0) {
}
array(0) {
}…………..With var_dump…
When I echo it, I just get “Array”…
Same result…
All users are friendlessfunction 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 }
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 workIt doesn’t seem to work :(.
Maybe I’m not doing it in the good way…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 ^^
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 ??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(); ?>
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…
Thank you for your answers, I’ll try this !
FYI I use last version of both BP (2.0) and WP(3.9)