Skip to:
Content
Pages
Categories
Search
Top
Bottom

[INFO] Useful s2member/buddypress knowledge


  • Myg0t
    Participant

    @myg0t

    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 that if(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

Viewing 15 replies - 1 through 15 (of 15 total)
  • @myg0t Thanks for sharing that with the community, S2 Member has wide use so this ought to be useful to people.

    One point though it’s best not to advise that people edit a core file /buddypress/bp-templates/bp-legacy/..etc BP has a fairly well defined template hierarchy overload allowing files to be copied to the theme or child theme, those are the copies one should edit.

    Another minor point rather than have to edit & pass the arguments in the template could bp_parse_args() perhaps work for this?
    Using bp_parse_args() to filter BuddyPress template loops

    In respect of the profile data would not setting the user visibility options on the profile field/s in the backend not work to provide the privacy for address fields, just a thought but I may have missed the point of this aspect.

    Lastly this could make a nice user example guide for the BP codex if you’d care to post it there and we’ll slot it under a section appropriate.


    Myg0t
    Participant

    @myg0t

    @hugo
    Thanks for the reply!

    Touche’, I should’ve mentioned that in my post. I’ve integrated all of it using a child theme, so it’ll be simple for me to show how to do that.

    That’s a good idea using bp_parse_args(). I’ll check it out and see if I can make it happen.

    I tried using the user visibility option to change the visibility of the s2member fields, but sadly, the options (when allowing s2member to integrate with BP) were grayed out. So it seemed that BP had no control over the options themselves. Which is why I decided to go the route that I did.

    One other reason that I went the way I did, is I know that all the information that I’m grabbing from my users is entirely gathered by the s2member application. In no way does my setup gather information using BP. So it seemed to me to be a better option.

    All in all, i’d be happy to post this up in the codex. If I do, will I be able to edit it? Cause i’d like to keep it updated with all the other things I find along the way.

    Thanks,
    \M

    > If I do, will I be able to edit it?

    Yes of course, as will anyone else though as that’s how the codex functions, but you are author, you may edit/update anytime you wish.


    shanebp
    Moderator

    @shanebp

    I wrote a function that would check a users information when they login.

    Your function runs on every page load.
    You could use this hook instead to run only on login. Note that the hook provides all the WP_User info that you need in the $user parameter.

    For the user_id gathering, bp_parse_args is the way to go.

    An s2member codex page would be very useful. It will be editable.


    Myg0t
    Participant

    @myg0t

    Cool cool, i’ll take a look at those things.

    When creating the codex page, What would you two suggest for components, types, and contexts tabs?

    I assume I should just put 2.1 for version?

    Thanks,
    \M

    Don’t worry too much about those tags we’ll update them as necessary(component is ‘Members’, type is ‘loop’, context I guess has to be ‘Developer’) and I’m looking into sections to hold user tips/tricks so may be shuffling pages around, for now it would live under ‘developers’


    Myg0t
    Participant

    @myg0t

    Okay great, much appreciated!


    mcpeanut
    Participant

    @mcpeanut

    @Myg0t Hi there, if you manage to integrate the fixes hugo and shane mentioned above would it be possible for you to post your updated code with the fixes implemented? Will save me a job further down the line as i plan on integrating the s2member plugin when ive finished on my current design layouts, will be much appreciated, cheers.

    @mcpeanut the changes mentioned weren’t that extreme you should be able to adjust the code based on what was suggested by shanebp and myself. bp_parse_args() is well documented in the codex and is relatively trivial to implement, the update function simply changing the hook it runs on to the WP login hook so it only fires once per user login/session.

    I would look though in the codex for the final example rather than request it be duplicated here for convenience.


    mcpeanut
    Participant

    @mcpeanut

    @hnla cheers for that, it will be a few weeks i think before i integrate s2members anyhows, i am currently finishing off the overall design of my theme and customizing every aspect of it and integrating alot of css changes to the way buddypress looks, so i will bookmark this page and refare back to it when i get round to s2member, i have learned so much over the past year and a half by visiting this site thx to you guys so cheers ๐Ÿ™‚


    Myg0t
    Participant

    @myg0t

    @mcpeanut I’ve posted the material in the codex now, so you may want to bookmark this page instead:

    https://codex.buddypress.org/developer/useful_func_s2_bp/

    I’ve already posted the option2 for the field import function. Once I get some more time and figure out how to use bp_parse_args() i’ll update the first section as well. Along with any other usefull s2member/buddypress tweaks that I find along the way.

    Glad you could find this useful!


    @hnla

    what’s the trick to formatting code in the codex? I’ve wrapped all the code in the tags, but it rips apart the formatting for the text (like indention’s)

    \M


    shanebp
    Moderator

    @shanebp

    Re: Filter Users Displayed in Members Directory by s2member role.
    There a couple of ways that this can be accomplished without needing to overload the members loop template and then hack it. One of them is bp_parse_args. Please try using it.

    The goal of this new codex page is to provide simple functions that adhere to best practices for WP and BP, that users can easily paste into bp-custom.php or theme/functions.php.

    Thanks for creating the page – looking forward to your updates.


    shanebp
    Moderator

    @shanebp

    Re: 2. Importing s2member custom fields to buddypress fields
    Instead of, for now, editing the codex page, please note the args parameter for add_action.

    So the ‘2’ here is the arguments parameter. add_action('wp_login','s2_profile_field_update',10,2);
    So the hook provides 2 arguments – very handy & useful.
    But your example doesn’t use them in the function s2_profile_field_update.
    You can use the $user arg instead of $current_user and thereby get rid of that global.
    Use this to see the data in $user:

    function hook_arg_display( $user_login, $user ) { 
       var_dump( $user ); 
    }
    add_action('wp_login','hook_arg_display',10,2);

    imo: There is no Option 1.


    shanebp
    Moderator

    @shanebp

    >whatโ€™s the trick to formatting code in the codex?

    Sadly, it is not intuitive.
    Take a look at how other code examples in the codex are formatted.

    @shanebp thanks for clarifying the changes.


    @myg0t
    Big thanks for adding this to the codex, please don’t be put off by us pestering on the changes ๐Ÿ™‚ bp_parse_args greatly simplifies things in terms of not having to have people locate, overload, possibly set up a child theme etc. wp_login is important to ensure you only run your function once.

    Common formatting of codex pages is something hard to achieve I created a couple of basic templates saved as drafts that can be copied ( i.e content copied) but they are not perfect but help. We’re still trying to see how to provide and enforce a format.

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘[INFO] Useful s2member/buddypress knowledge’ is closed to new replies.
Skip to toolbar