[INFO] Useful s2member/buddypress knowledge
-
Hey there guys, i’m working on a website where i’m integrating s2member functionality with buddypress. I thought some of the things I’ve come across may be useful to other people.
1. Filter Users Displayed in Members Directory by s2member role.
In your plugins/buddypress/bp-templates/bp-legacy/buddypress/members/ folder edit the file “members-loop.php”. Just under
<?php do_action( 'bp_before_members_loop' ); ?>
at the top 20 lines of the file we need to write some PHP code to build a comma separated list of ID’s based on the s2member role or roles that we want. In my case I wanted only members of “s2member_level2”.
<?php $args = array ( 'role' => 's2member_level2', 'fields' => array( 'id' ) ); // The User Query $user_query = new WP_User_Query( $args ); $custom_ids = ''; for($i = 0;$i < sizeof($user_query->results); $i++){ $a = $user_query->results[$i]->id; $custom_ids .= $a.","; } ?>
Now that we’ve made a comma separated string of ids ($custom_ids), we need to pass it to the members loop.
Change
<?php if ( bp_has_members( bp_ajax_querystring( 'members' ) ) ) : ?>
to
<?php if ( bp_has_members('per_page=30&type=alphabetical&'.'include='.$custom_ids ) ) : ?>
In my case I wanted to make sure atleast 30 members showed up, and that they were in alphabetical order.Done.
2. Importing s2member custom fields to buddypress fields
You may be asking; why??? Well, it turns out that when you have the s2member option to integrate with buddypress it doesn’t actually import it’d data to the buddypress tables. It just binds it’s self to the ‘Base’ group, Which will show up by default in buddypress profiles. When it doesn’t import to the buddypress tables, it was very difficult for me to manipulate how the information showed up. Particularly the fact that I have the users give me their address information, and I don’t want that to even be an option to show in user profiles.
So instead of using s2member to integrate automatically, I wrote a function that would check a users information when they login. If they have certain information in s2member that is not in their BP Profile, it will add it automatically.
in functions.php add the code:
<?php add_action('wp_head','s2_profile_field_update',10,2); function s2_profile_field_update() { global $current_user; get_currentuserinfo(); if( get_user_field("s2member_login_counter") < 1000 ) { if( current_user_is("s2member_level2") ) { if(xprofile_get_field_data('Nationality',$current_user->id) == '' && get_user_field('nationality') != '') { xprofile_set_field_data('Nationality', $current_user->id, get_user_field('nationality')); } } } } ?>
You can change Nationality to what ever the name is of the extended BP field (in users>profile fields).
And of course you can duplicate the if statements for how every many fields you want to import from s2memeber -> BP.
Also note thatif(get_user_field("s2member_login_counter") < 1000)
mean’s that this process will run for any user that logs in with membership role s2member_level2 and has logged in less than 1000 times. I used 1000 times because I want any member at this moment who logs in to have their profiles populated. In the future I will drop it down to 1 or 2.I’ll post more as I gather it. But in the mean time I hope this helps some people.
\M
- The topic ‘[INFO] Useful s2member/buddypress knowledge’ is closed to new replies.