BuddyDev

Search

[Resolved] Handling Acceptance Checkbox field from php? – BuddyPress Xprofile Custom Field

  • Participant
    Level: Initiated
    Posts: 5
    Richard G4WKW on #46313

    Hi, I am not sure if anyone can help or point me in the right direction? I am developing a website with both public and members-only areas, using BuddyPress, and lots of plugins including Members from MemberPress and BuddyPress Xprofile Custom Field Types from Buddy Dev. It is for a radio club, where there will be no website registration, all user accounts with limited mandatory info will be set up for them.

    I have a working checkbox that for accepting Terms & Condition on both the WP and BuddyPress login dialog, but the user has to select it on EVERY login.

    I would like to alter the code to remove my current checkbox from the login page(s), and add one to the “welcome” page that they are taken to on login, to accept the “Terms” Acceptance Checkbox that I have created under the Base (Primary) field group, with that checkbox set according to the field state of the current logged in user. The idea is when they cannot proceed any further into the members-only part by clicking the “Continue” button, unless the checkbox has been selected.

    I know that people have done systems to take people to the BuddyPress Profile page (ideally in Edit mode) to do that, but I would far rather have the checkbox field on the “welcome” page (which contains current important user notices).

    Can anyone help me with a bit of PHP code with a shortcode that I could add to the button, to either direct the the members-only landing page if checkbox is on, or back to the welcome page if it is off, suitable to add to the functions.php of my child theme?

  • Keymaster
    (BuddyDev Team)
    Posts: 21852
    Brajesh Singh on #46326

    Hi Richard,
    Welcome to BuddyDev support forums.

    I am not sure how you add the checkbox on the login page as our plugin does not add that.
    Still, our plugin stores the value in the xprofile data. You will need to add a form and then on form submission, you will need to update the field using xprofile_set_field_data($field_id, $user_id, $value ); to set it.

    The purpose of the xprofile terms field to to simply store the preference and does not do anything else.

    I hope it helps.

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 5
    Richard G4WKW on #46336

    Thanks Brajesh ๐Ÿ™‚

    I could have written my question better; sorry about that! Forget about checkbox on Welcome page. After thinking things though a bit more clearly, I think this is a better description of what I am trying to achieve.

    I would like to have a “checktandc” page, which everyone is taken to (very briefly) on logon, with the page load calling a function to read the value of the “T&C Accepted” field (as I have called it), and then depending on the state of the field:

    1) If the value of the checkbox is 0 or false, the user is immediately redirected to the proper “Terms & Conditions” page, which has “Accept” and “Reject” buttons at the bottom. Then:

    a) If “Accept” button clicked, user immediately redirected to the “Welcome” page (and a function using xprofile_set_field_data is used to update the “T&C Accepted” field), so the “Terms & Conditions” page is bypassed on next login and user taken to “Welcome” page almost immediately.

    b) If “Reject” accepted, user is taken to the “Home” page.

    2) If the value of the checkbox is 1 or true, then the user is redirected to the Welcome page.

    Calling the function: xprofile_set_field_data($field_id, $user_id, $value );
    looks great to set the value of the checkbox to 1 or true when someone clicks the “Accept” button on the “Terms and Conditions” page.

    —-

    What function do I need to use to READ the value of the field, from the “checktandc” page? I was expecting a something like “xprofile_read_field_data()” but that does not appear to exist?

    Also, I am not sure on the easiest way to set up the automatic redirect, depending on the state of the field, from the “checktandc” page?

  • Keymaster
    (BuddyDev Team)
    Posts: 21852
    Brajesh Singh on #46342

    Hi,
    Thank you for the details.

    Here is some sample code(you will need to change page id and field id) to redirect user to the term page

    
    
    add_action( 'bp_template_redirect', function () {
    	// no need to do anything if the user is not logged
    	// or the user is super admin.
    	// (probably we would like to restrict access to update terms page in this case)
    	if ( ! is_user_logged_in() || is_super_admin()) {
    		return;
    	}
    
    	$term_page_id  = 32;// please use your page
    	$term_field_id = 2;// please update
    
    	if ( xprofile_get_field_data( $term_field_id, get_current_user_id() ) ) {
    		// already updated, no need to do anything
    		return;
    	}
    
    	// if we are here and it is not terms page, redirect to terms page
    	if ( ! is_page( $term_page_id ) ) {
    		bp_core_redirect( get_permalink( $term_page_id ) );
    	}
    
    } );
    

    The function to get the data is xprofile_get_field_data( $field_id_or_name, $user_id)

    Do you need assistance with creating a form and updating the field data or can you handle that part?

    Please let me know.

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 5
    Richard G4WKW on #46344

    Hi Brajesh,

    Thanks so much for your code! I really very much appreciate it and your quick response ๐Ÿ™‚

    I think that I can handle the form (and I enjoy a challenge). I will try and get it all working, and let you know if I have, or have had a problem with something.

    Kind regards,

    Richard.

  • Keymaster
    (BuddyDev Team)
    Posts: 21852
    Brajesh Singh on #46347

    Hi Richard,
    You are welcome.

    All the best with the implementation ๐Ÿ™‚
    Please feel free to reach out if you need any assistance.

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 5
    Richard G4WKW on #46476

    Hi Brajesh,
    I am happy to say I have got it all working the way I want, without using forms, but shortcodes for functions I have written.

    After login, the user is taken to my โ€œchecktandcโ€ page which calls [g4wkw_check_tanc]:

    // [g4wkw_check_tanc]
    // function to try and handle conditional T&C Display 
    // called from checktandc page /login-logout/checktandc/
    function g4wkw_check_tanc_field () {
        $user = wp_get_current_user();
    	$user_login = $user->user_login;
    	$my_str = $user_login;
    	$user_lc = strtolower($my_str);
    	
    	$term_page_id  = 3583;   // "T&C" page /login-logout/tc/ - post=3583
    	$welcome_page_id = 2661; // "Welcome!" page /login-logout/welcome/ - post=2661
    	$term_field_id = 15;     // field_id=15 from mouseover Edit on "T&C Accepted?" field in bp-profile-setup Base.
    	$tandc = 0;              // initialise variable
    	
    	$tandc = xprofile_get_field_data( $term_field_id, get_current_user_id() ); // value of either "Yes" or "No"
    	
    	// if T&C checkbox already set (ticked)
    	if ( $tandc == 'Yes' ) {
    		bp_core_redirect( get_permalink( $welcome_page_id ) );  // Redirect to "Welcome!" page
    		return;
    	}	else {
    		bp_core_redirect( get_permalink( $term_page_id ) );  // Redirect to "T&C" page
    		return;
    	}	
        return;
    }
    add_shortcode('g4wkw_check_tanc', 'g4wkw_check_tanc_field');

    That redirects to either the T&C page or Welcome page, depending on the field. The T&C page goes to a “T&C Rejected” page if they click “Reject T&C” button, or “Welcome” page if they click “Accept T&C”.

    The “Welcome” page calls another shortcode [g4wkw_process_accepttandc] to set the field and add the user to a ‘termsok’ usergroup (for use in content control).

    // [g4wkw_process_accepttandc]
    // shortcode to try and get value of the "T&C Accepted?" field
    function g4wkw_process_accepttandc_data () {
    	$welcome_page_id = 2661;          // "Welcome!" page /login-logout/welcome/ - post=2661
    	$term_field_id = 15;              // field_id=15 from mouseover Edit on "T&C Accepted?" (tandc) field in bp-profile-setup Base.
    	$user_id = get_current_user_id(); // get current user_id
    	$tandc = 1;                       // initialise variable for tandc field to 1 ('Yes')
    	
    	xprofile_set_field_data($term_field_id, $user_id, $tandc ); // sets tanc field to 1 ('Yes')
    	
    	// code to add logged-in user to 'termsok' usergroup (for content control enabling)
    	$user = get_user_by( 'ID', $user_id );
    	$user->add_role( 'termsok' );
    	
        return;
    }
    add_shortcode('g4wkw_process_accepttandc', 'g4wkw_process_accepttandc_data');

    Hope that makes sense.
    Regards,
    Richard.

  • Participant
    Level: Initiated
    Posts: 5
    Richard G4WKW on #46477

    I just realized that I had left some redundant code in my first function, producing $user_lc which I then don’t use! I had duplicated a shortcode function I had written before which just returns that value to display a nice username like “G4WKW Richard” on various pages to personalise them for the logged in user.

  • Keymaster
    (BuddyDev Team)
    Posts: 21852
    Brajesh Singh on #46496

    Hi Richard,
    Thank you for sharing.

    It looks good based on your use case. Earlier, I thought that you wanted users to accept the terms on a new page but I now see you wanted it differently.

    I think the approach in second step is erroneous as it will add anyone visiting that page as someone who has accepted the terms even if they did not.

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 5
    Richard G4WKW on #46508

    Hi Brajesh,

    I understand your point regarding my second step, but the page is not linked to anywhere publicly and can only be loaded by finding the correct URL to go to, by a logged-in member (so safe enough, I think).

    The problem with getting T&C Acceptance AFTER login, is that without careful content control, once logged in a user would be able to bypass T&C Acceptance if they are knowledgeable or determined enough to do so.

    Although common BuddyPress stuff like /members/ and /groups/ are locked out on my site to anyone not logged in (using the free BuddyPress Private Community plugin from WPCOM), and pages and menu items locked out (using Content Control and User Menus plugins by Code Atlantic) nothing is perfect and a logged in user could load a BuddyPress page directly after login by entering the correct URL.

    Yes, I could prevent that, by only adding them to the correct BuddyPress member category (I have three), AFTER they have accepted the T&C, but that in itself produces a host more problems, and I do not consider it worth the effort!

    One of the key things is that although a lot of membership sites are happy to use paid Pro plugins, and specific Pro Membership themes, for content behind a “pay wall”, small clubs often want members-only content for free! A lot of membership sites earn money via their websites too, which offsets the costs of setting up and maintenance.

    Anyway, thanks for your help in pointing me in the right direction with your code.

    Kind regards
    Richard.

You must be logged in to reply to this topic.

This topic is: resolved