ACF fieldroup with user registration in multisite with membership 2

Hi, I changed a single site to multisite. the subsites are an exact copy of the single one. In both I have ACF 5 with a fieldgroup with location rule 'show this fieldgroup' is equal to all or to register. In single site, when using the 'register' via Membership 2 plugin, the field group appears nicely under the normal registration fields, perfect. In multisite, the fieldgroup does not show up. How to make this happen in one of the 'blog' sites with MEMBERSHIP 2?

  • Adam Czajczyk
    • Support Gorilla

    Hello Paul

    I hope you're well today and thank you for your question!

    I'd like to get to know a bit more about your Multisite.

    What I mostly mean is: are ACF and Membership 2 Pro enabled "per site" or network-activated? If Membership 2 Pro is network-enabled is it configured "by default" or is there a "define" line added to "wp-config.php" file that makes it work as a "central membership for entire network" (network-wide mode)?

    Apart from this, are there any additional plugins that do or might affect registration/user handling? Such as for example BuddyPress or some other user-management-oriented plugins?

    I'm asking because while I'm no expert in ACF the important part is that registration and user accounts' handling on Multisite works quite different to the single site and that's probably affecting it the most. So, understanding those things could help me diagnose this.

    It would also be great if you could enable support access to the site so I could actually take a closer look at this setup. To enable it, please go to the "Network Admin -> WPMU DEV -> Support" page in your site's back-end and click on "Grant support access" button there, then let me know once it's done.

    Best regards,
    Adam

  • Paul
    • New Recruit

    Hi Adam, thankd for taking this up.

    Both Membership 2 and ACF Pro are network activated on the multisite. Membership 2 is "by default" , as I need different pricing and currency per blog.

    There are some other plugins, but, as it works fine in single site, I do not expect the problem is there. No Buddy press.

    Both the single site and the staging multisite are now support acces granted !

    The difference is very visible when you try
    https://www.singlesite.tld/register/?step=register
    compared to
    https://www.multisite.tld/register/?step=register

    yes, you have the names of those both sites, I do not want everybody on fora to try my sites right now :slight_smile:
    Hope you can help?
    Thanks a lot,
    Paul.

  • Adam Czajczyk
    • Support Gorilla

    Hi Paul

    Thank you for explanation.

    I checked your sites and I can see the issue there. I was also able to replicate it fully on my end. I tried to find the culprit here but to no luck so far.

    I'll need some help from our developers then so I've asked them for consultation and I believe they'll be able to lend us a hand on this. Please keep an eye on this ticket and we'll update you here as soon as we get to know more from them.

    Kind regards,
    Adam

  • Konstantinos Xenos
    • Rubber Duck Debugger

    Hey Paul ,

    Sorry for the late reply, let me explain the 'issue' here so we can be on the same page and then I'll propose something to see if this could help.

    WordPress Core uses a different signup process between Single and Multisite. Both processes have different hooks for plugins to add extra fields there, the 'single' installation will have the "register_form" hook and the 'multisite' will have the "signup_extra_fields" hook. You can see both at:

    https://developer.wordpress.org/reference/hooks/register_form/
    https://developer.wordpress.org/reference/hooks/signup_extra_fields/

    ACF on the other hand never supported adding fields on Multisite registration, it only hooks into "register_form" that is used for single sites and that's why you don't see those fields on your Multisite as well installation. If you search on the ACF forums you'll find many users requesting that ACF should start supporting Multisite registration as well ( there are even posts as of late 2018 and newer even ).

    As you can understand this isn't something related to Membership 2 Pro, as this functionality isn't supported at all from ACF itself.

    Now to the 'solution' even though this is a little "hacky".

    We have a function in Membership that checks if the installation is a multisite or single to add the same hooks as WP core uses to extend it's forms. We could 'alter' our function to also add the "register_form" hook in our form instead of using the correct one for the multisites.. or even better support "both" so ACF will be 'tricked' and it will show the extra fields on a multisite as well.

    How to do this:

    Always make sure to keep a backup of your site before changing/adding custom code.

    1] Navigate to your /wp-content/ directory and create a new one named mu-plugins if it doesn't exist.
    2] Inside the mu-plugins folder create a file named m2p-acf-multisite.php
    3] Edit the file and copy / paste this code snippet inside.

    <?php
    
    function ms_registration_form_extra_fields() {
    	if ( is_multisite() ) {
    		// this is the original multisite hook.
    		do_action( 'signup_extra_fields', MS_Helper_Template::$ms_registration_form['empty_error'] );
    		// we are adding the single site hook as well in multisite.
    		do_action( 'register_form' );
    	} else {
    		// this is used on single site installations.
    		do_action( 'register_form' );
    	}
    }

    4] Save and close the file.
    5] The final path should look like /wp-content/mu-plugins/m2p-acf-multisite.php
    6] Everything should work after that automatically.

    I've run some tests and the fields are getting saved properly on my installation but please do test this thoroughly and again keep a backup handy as more plugins could be tapping into "register_form" resulting on unwanted behavior since that's not the intended hook to use for Multisites.

    This function has to be in a mu-plugin as mentioned on the install process, if you add it on your functions.php you will break the installation.

    Tell me if this helps or if you need any further help & information.

    Regards,
    Konstantinos

  • Paul
    • New Recruit

    Konstantinos, you DID find a sollution (well, almost) for something many people need! Great to see how you and all you folks at WPMUDEV take support serious!

    It works almost fine for registration! can you make next 2 issues work as well?
    In my fields is also a picture (in the ACF fieldgroup), that one is not saved.
    It would be a dream to also have it on the edit profile, not only in register?

    Anyhow, thanks for great help yet!
    Paul.

  • Konstantinos Xenos
    • Rubber Duck Debugger

    Hey Paul ,

    Unfortunately the "image field" is probably something handled from the ACF side since it's not updating at all. Actually the image doesn't get uploaded at all as well from what I see so since we're 'tricking' things here most likely not all fields would actually play nice with the multisite feature since that's not officially supported either. I'm sorry for that but looking at why it might require more than enough time and since it's on the ACF side of things it's a bit out of this supports scope as well.

    For the edit profile though you can easily add as many extra fields as you like using 3 extra hooks for those forms. We of course have to update 2 screens, one would be the profile view and the second the Edit profile view, of course we should hook also into the action that runs when a user profile is getting updated to save any "changed" fields.

    Let's see my setup first so you can have a better understanding:

    The acf fields group:

    The profile view page:

    The profile Edit page:

    A more "complete" mu-plugin that includes the previous code as well, I've also added comments in there so it can be more easily extended by you depending on the fields & needs.

    <?php
    
    /**
     * This adds the ACF fields into the Multisite registration form.
     */
    function ms_registration_form_extra_fields() {
    	if ( is_multisite() ) {
    		// this is the original multisite hook.
    		do_action( 'signup_extra_fields', MS_Helper_Template::$ms_registration_form['empty_error'] );
    		// we are adding the single site hook as well in multisite.
    		do_action( 'register_form' );
    	} else {
    		// this is used on single site installations.
    		do_action( 'register_form' );
    	}
    }
    
    /**
     * This adds the ACF fields into the Profile Page screen.
     * It uses a mix of jquery + php to insert the ACF fields into the existing table to show them as 'one' instance.
     * Add extra table rows in the js and alter the get_field to match the ACF fields.
     */
    add_action(
    	'ms_view_account_profile_before_card',
    	function() {
    		$uid = 'user_' . get_current_user_id();
    		?>
    		<script>
    		( function ( $ ) {
    			$( document ).ready( function() {
    				$( 'div#account-profile table' ).append(
    					'<tr><th><?php echo esc_html( 'ACF 1', 'membership2' ); ?></th><td><?php echo get_field( 'myacffield', $uid ); ?></td></tr>' +
    					'<tr><th><?php echo esc_html( 'ACF 2', 'membership2' ); ?></th><td><?php echo get_field( 'myacffield2', $uid ); ?></td></tr>'
    				);
    			} );
    		} ( jQuery ) );
    		</script>
    		<?php
    	},
    	15
    );
    
    /**
     * This ass the fields into the Profile Edit screen.
     * Add extra text fields in the array and alter the get_field to match the ACF fields.
     */
    add_filter(
    	'ms_view_profile_fields',
    	function( $fields ) {
    		$uid = 'user_' . get_current_user_id();
    
    		$array = array_slice( $fields, 0, 5, true ) +
    				array(
    					'acf1'       => array(
    						'id'          => 'acf1',
    						'title'       => esc_html__( 'ACF Field', 'membership2' ),
    						'placeholder' => '',
    						'type'        => 'text',
    						'value'       => get_field( 'myacffield', $uid ),
    					),
    					'acf2'       => array(
    						'id'          => 'acf2',
    						'title'       => esc_html__( 'ACF Field 2', 'membership2' ),
    						'placeholder' => '',
    						'type'        => 'text',
    						'value'       => get_field( 'myacffield2', $uid ),
    					),
    				) +
    				array_slice( $fields, 5, count( $fields ) - 1, true );
    
    			return $array;
    
    	},
    	15
    );
    
    /**
     * This updates the acf fields when clicking Submit on the M2P Edit screen ( connected to ms_view_profile_fields above ).
     */
    add_action(
    	'profile_update',
    	function() {
    		$uid = 'user_' . get_current_user_id();
    
    		if ( isset( $_POST['acf1'] ) && ! empty( $_POST['acf1'] ) ) {
    			update_field( 'myacffield', sanitize_text_field( $_POST['acf1'] ), $uid );
    		}
    
    		if ( isset( $_POST['acf2'] ) && ! empty( $_POST['acf2'] ) ) {
    			update_field( 'myacffield2', sanitize_text_field( $_POST['acf2'] ), $uid );
    		}
    	},
    	15
    );

    I hope this helps a little bit more and I'm sorry about the image field but as I've mentioned since this isn't something that ACF support within it's core there might be more fields that won't work out of the box.

    Tell me if you need further information or help!

    Regards,
    Konstantinos

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.