The Gravity Forms User Registration Add-On plugin allows you to Create (or Update) a WordPress user and their meta fields from a Gravity Form’s fields. It’s a great plugin.
By default, it only allows one feed type per form: a Create OR an Update feed.
I build a lot of Membership Application forms with Gravity Forms. I used to build two forms per User Registration feed: one for logged-out users and another for logged-in users. It was annoying for me and our clients to maintain two forms.
I spent some time seeing if I could use both a Create and Update feed on one form, triggering one or the other feed based on a user’s log-in state.
I found a way to do it. I’m sharing the code in case it’s useful to anyone.
To use this code, copy the class into a plugin (or your theme). Then call gravity_forms_user_registration::init() from an action such as ‘plugins_loaded’.
<?php | |
/** | |
* This class gets around a limitation of the Gravity Forms User Registration Add-On that limits you to | |
* one type of User Registration feed per form: Create OR Update. | |
* https://www.gravityforms.com/add-ons/user-registration/. | |
* | |
* We use this plugin in membership application forms. | |
* Membership application forms are used by both logged-in and logged-out users. | |
* Being able to use a Create AND Update feed allows us share one membership form. | |
* | |
* Usage: | |
* Include this file inside functions.php and call gravity_forms_user_registration::init() inside an hook such as | |
* plugins_loaded: | |
* | |
* include 'gravity-forms-user-registration.php'; | |
* add_action( 'plugins_loaded', array( 'gravity_forms_user_registration', 'init' ) ); | |
*/ | |
class gravity_forms_user_registration { | |
/** | |
* Enable/disable both create and update feeds. | |
* We use an Advanced Custom Field setting for this. | |
* | |
* @return bool | |
*/ | |
public static function is_create_and_update_feeds_enabled() { | |
return true; | |
} | |
/** | |
* Entry into this class. | |
* Include this class then use gravity_forms_user_registration::init() in your functions.php file. | |
*/ | |
public static function init() { | |
if ( self::is_create_and_update_feeds_enabled() ) { | |
self::setup_hooks(); | |
} | |
} | |
/** | |
* @return bool | |
*/ | |
public static function is_user_registration_plugin_installed() { | |
return class_exists( '\GF_User_Registration' ); | |
} | |
/** | |
* Setup the hooks that enable both Create and Update feeds. | |
*/ | |
public static function setup_hooks() { | |
add_filter( 'gform_userregistration_feed_settings_fields', [ | |
self::class, | |
'enable_create_and_update_feeds' | |
], 10, 2 ); | |
add_filter( 'gform_form_args', [ self::class, 'fix_error_on_forms_with_create_and_udpate_feeds' ], 10, 1 ); | |
add_filter( 'gform_gravityformsuserregistration_pre_process_feeds', [ | |
self::class, | |
'use_update_feed_if_logged_in' | |
], 10, 3 ); | |
} | |
/** | |
* @param $fields | |
* @param $form | |
* | |
* @return mixed | |
*/ | |
public static function enable_create_and_update_feeds( $fields, $form ) { | |
unset( $fields['feed_settings']['fields'][1]['choices']['update']['disabled'] ); | |
return $fields; | |
} | |
/** | |
* Using a filter that fires early to disable an action which causes an error when both | |
* a create and update feed exist and a user not logged in. | |
* | |
* @param $form_args | |
* | |
* @return mixed | |
*/ | |
public static function fix_error_on_forms_with_create_and_udpate_feeds( $form_args ) { | |
// User registration only returns an error when an update feed exists and a user is NOT logged in. | |
if ( is_user_logged_in() || ! class_exists( '\GF_User_Registration' ) ) { | |
return $form_args; | |
} | |
$gf_user_reg_feed_types = []; | |
$gf_user_reg = \GF_User_Registration::get_instance(); | |
$feeds = $gf_user_reg->get_feeds( $form_args['form_id'] ); | |
foreach ( $feeds as $feed ) { | |
if ( $feed['is_active'] && | |
$feed['addon_slug'] === 'gravityformsuserregistration' | |
) { | |
$gf_user_reg_feed_types[] = rgars( $feed, 'meta/feedType' ); | |
} | |
} | |
// A create AND update feed exists: | |
// Disable the maybe_prepopulate_form method which prepopulates a form from a logged in | |
// user (not relevant if not logged in), and returns an error if an update feed exists but a user is NOT logged in. | |
if ( $gf_user_reg_feed_types === [ 'create', 'update' ] ) { | |
remove_action( 'gform_pre_render', [ 'GF_User_Registration', 'maybe_prepopulate_form' ] ); | |
} | |
return $form_args; | |
} | |
/** | |
* Detects a logged in user and returns the Update feed for these users if created. | |
* | |
* @param $feeds | |
* @param $entry | |
* @param $form | |
* | |
* @return array | |
*/ | |
public static function use_update_feed_if_logged_in( $feeds, $entry, $form ) { | |
if ( ! is_user_logged_in() || ! class_exists( '\GF_User_Registration' ) ) { | |
return $feeds; | |
} | |
$gf_user_reg = \GF_User_Registration::get_instance(); | |
$feeds = $gf_user_reg->get_feeds( $form['id'] ); | |
foreach ( $feeds as $feed ) { | |
if ( $feed['is_active'] && | |
'gravityformsuserregistration' === $feed['addon_slug'] && | |
'update' === $feed['meta']['feedType'] && | |
$gf_user_reg->is_feed_condition_met( $feed, $form, $entry ) | |
) { | |
return [ $feed ]; | |
} | |
} | |
return $feeds; | |
} | |
} |