There are several things to be changed.
You don’t need this: global $wpdb;
Change: array_push($names, bp_group_member_name() );
To: $names[] = bp_get_group_member_name();
Change the js ajax call to:
jQuery.post(ajaxurl, function(response) {
console.log(response); // so you can examine the structure
});
Thank for the reply.
I tried it but then even the alert doesn’t show when I change the code.
This is the entire code: [ edited: see below for link to code ]
Please use pastebin or gist to share large chunks of code.
sure, sorry about that ^^’
here it is:
http://pastebin.com/XTNhRUVw
Hi
I figured out that it’s the while loop that’s stopping the php function from responding properly
While this works just fine:
function my_action_callback() {
global $wpdb;
$names = array();
$whatever = intval( $_POST['whatever'] );
//while ( bp_group_members() ) : bp_group_the_member();
//$name = bp_group_member_name() ;
//array_push($names, $name);
//endwhile;
$rep2 = array( $_POST['whatever'] . ' successful!' );
echo json_encode($rep2);
die();
}
This stops the click from alerting anything
function my_action_callback() {
global $wpdb;
$names = array();
$whatever = intval( $_POST['whatever'] );
while ( bp_group_members() ) : bp_group_the_member();
$name = bp_group_member_name() ;
array_push($names, $name);
endwhile;
$rep2 = array( $_POST['whatever'] . ' successful!' );
echo json_encode($rep2);
die();
}
Any idea why is the loop wrong there?
Any help highly appreciated
As I pointed out above, you should use bp_get_group_member_name()
not bp_group_member_name()
.
The latter will echo out the name. The former will return it as a variable.
Check your error logs.
You need to be on a group page to use while ( bp_group_members() )
Otherwise you need to precede it with if ( bp_group_has_members( 'group_id=15' ) ) :
Is this right then?
function my_action_callback() {
$names = array();
$whatever = intval( $_POST['whatever'] );
while ( bp_group_members() ) : bp_group_the_member();
array_push($names, bp_get_group_member_name());
endwhile;
//$rep2 = array( $_POST['whatever'] . ' successful!' );
echo json_encode($names);
die();
}
The plugin is run only in a group view.
Btw from the log
[06-Dec-2014 18:02:54] PHP Fatal error: Call to a member function members() on a non-object in /home/public_html/wordpress/wp-content/plugins/buddypress/bp-groups/bp-groups-template.php on line 2815