User Action

The User Action is a template to create, update or log a user on form submission.

User Settings

Setting nameDescription
Action typeCreate, update or log a user
Action nameAlias name allowing to use targeted hook
TargetThe user to update
SourceThe user to retrieve fields from
Load valuesFill inputs with values
E-mailThe user e-mail
UsernameThe username
PasswordThe user password
First nameThe user first name
Last nameThe user last name
NicknameThe user nickname
Display nameThe user display name
WebsiteThe user website
DescriptionThe user description
RoleThe user role
Save ACF fieldsChoose which ACF fields should be saved to this user
Load ACF fieldsChoose which ACF fields should have their values loaded

Validation

The Action is bundled with a validation process for the Log User Action type. You can use the acf/validate_value hook to validate each fields individually.

To validate the whole form, you can use the acfe/form/validation/user hook and throw errors with the acfe_add_validation_error() function. Usage example:

/*
 * User Validation
 * 
 * @array   $form     Form settings
 * @string  $post_id  Post ID (where the form is displayed)
 * @string  $action   Action name
 */
 
action('acfe/form/validation/user',                $form, $post_id, $action);
action('acfe/form/validation/user/form=my-form',   $form, $post_id, $action);
action('acfe/form/validation/user/action=my-user', $form, $post_id, $action);
add_action('acfe/form/validation/user/form=my-form', 'my_form_validation', 10, 3);
function my_form_validation($form, $post_id, $action){
    
    // Get field input value
    $my_field = get_field('my_field');
    
    if($my_field === 'Company'){
        
        // Add validation error
        acfe_add_validation_error('my_field', 'The value Company is not allowed');
        
    }
    
}

Prepare

The preparation phase is triggered after the form validation and before its submission. The acfe/form/prepare/user hook allows you to stop the Action early if necessary.

It is also the best place to add some logic or create new query vars that could be used within the Action UI. Returning false will stop the Action and go to the next one. Usage example:

/*
 * User Prepare
 * 
 * @bool    $prepare  True/false
 * @array   $form     Form settings
 * @string  $post_id  Post ID (where the form is displayed)
 * @string  $action   Action name
 */
 
filter('acfe/form/prepare/user',                $prepare, $form, $post_id, $action);
filter('acfe/form/prepare/user/form=my-form',   $prepare, $form, $post_id, $action);
filter('acfe/form/prepare/user/action=my-user', $prepare, $form, $post_id, $action);
add_filter('acfe/form/prepare/user/form=my-form', 'my_form_prepare', 10, 4);
function my_form_prepare($prepare, $form, $post_id, $action){
    
    // Get field input value
    $my_field = get_field('my_field');
    
    // Create custom message
    $message = 'This is a custom message. Field value: ' . $my_field;
    
    // Retrive the message using {query_var:user_message} in the User Action UI
    set_query_var('user_message', $message);
    
    // Do not create/update/log user if user isn't logged in
    if(!is_user_logged_in()){
        
        $prepare = false;
        
    }
    
    // return
    return $prepare;
    
}

Values Source

Alter the user ID where meta values are loaded from. Usage example:

/*
 * User Values Source
 * 
 * @int     $user_id  User ID Source
 * @array   $form     Form settings
 * @string  $action   Action name
 */

filter('acfe/form/load/user_id',                $user_id, $form, $action);
filter('acfe/form/load/user_id/form=my-form',   $user_id, $form, $action);
filter('acfe/form/load/user_id/action=my-user', $user_id, $form, $action);
add_filter('acfe/form/load/user_id/form=my-form', 'my_form_user_source', 10, 3);
function my_form_user_source($user_id, $form, $action){
    
    // Get field input value
    $my_field = get_field('my_field');
    
    if($my_field === 'Company'){
    
        // Force to load values from the user ID 8
        $user_id = 8;
    
    }
    
    // return
    return $user_id;
    
}

User Arguments

Alter the user arguments before database insert/update. Usage example:

/*
 * User Arguments
 * 
 * @array   $args    Generated user arguments
 * @string  $type    Action type: 'insert_user' or 'update_user'
 * @array   $form    Form settings
 * @string  $action  Action name
 */

filter('acfe/form/submit/user_args',                $args, $type, $form, $action);
filter('acfe/form/submit/user_args/form=my-form',   $args, $type, $form, $action);
filter('acfe/form/submit/user_args/action=my-user', $args, $type, $form, $action);
add_filter('acfe/form/submit/user_args/form=my-form', 'my_form_user_args', 10, 4);
function my_form_user_args($args, $type, $form, $action){
    
    // Get field input value
    $my_field = get_field('my_field');
    
    if($my_field === 'Company'){
    
        // Change user first name
        $args['first_name'] = 'My name';
    
    }
    
    // Stop user creation/update
    // return false;
    
    // return
    return $args;
    
}

Submit

Trigger a custom action after the user was created/updated/logged using the acfe/form/submit/user hook. Usage example:

/*
 * User Submit
 * 
 * @int     $user_id  Created/Updated user ID
 * @string  $type     Action type: 'insert_user', 'update_user' or 'log_user'
 * @array   $args     Generated user arguments
 * @array   $form     Form settings
 * @string  $action   Action name
 */
 
action('acfe/form/submit/user',                $user_id, $type, $args, $form, $action);
action('acfe/form/submit/user/form=my-form',   $user_id, $type, $args, $form, $action);
action('acfe/form/submit/user/action=my-user', $user_id, $type, $args, $form, $action);
add_action('acfe/form/submit/user/form=my-form', 'my_form_submit', 10, 5);
function my_form_submit($user_id, $type, $args, $form, $action){
    
    // Get field input value
    $my_field = get_field('my_field');
    
    if($my_field === 'Company'){
    
        // Do something...
    
    }
    
}

Query Var

After the action completion, a query var with the user arguments is automatically genereated. This query var is useful to retrieve the action information in the UI using {query_var:my-user:ID} or in PHP using get_query_var('my-user')['ID'].

If the action is named, the query var will simply use its name: my-user-action. If there is no action name, it will be named my-form-user . If there are multiple user actions without names, the second user query var will be called my-form-user-2 and so on.

The acfe/form/query_var/user hook allows you to change the data that is returned. Usage example:

/*
 * User Query var
 * 
 * @array   $data     User information
 * @array   $user_id  Created/Updated user ID
 * @string  $type     Action type: 'insert_user', 'update_user' or 'log_user'
 * @array   $args     User arguments
 * @array   $form     Form settings
 * @string  $action   Action name
 */
 
filter('acfe/form/query_var/user',                $data, $user_id, $type, $args, $form, $action);
filter('acfe/form/query_var/user/form=my-form',   $data, $user_id, $type, $args, $form, $action);
filter('acfe/form/query_var/user/action=my-user', $data, $user_id, $type, $args, $form, $action);
add_filter('acfe/form/query_var/user/form=my-form', 'my_form_query_var', 10, 6);
function my_form_query_var($data, $user_id, $type, $args, $form, $action){
    
    /*
     * $data
     *
     * Array
     * (
     * [ID] => 4
     * [user_login] => User
     * [user_pass] => mypassword
     * [user_nicename] => user
     * [user_email] => user@email.com
     * [user_url] =>
     * [user_registered] => 2020-08-19 20:27:59
     * [user_activation_key] =>
     * [user_status] => 0
     * [display_name] => First name Last name
     * [nickname] => User
     * [first_name] => First name
     * [last_name] => Last name
     * [description] =>
     * [rich_editing] => true
     * [syntax_highlighting] => true
     * [comment_shortcuts] => false
     * [admin_color] => fresh
     * [use_ssl] => 0
     * [show_admin_bar_front] => true
     * [locale] =>
     * [wp_capabilities] => a:1:{s:10:"subscriber";b:1;}
     * [wp_user_level] => 0
     * [permalink] => /author/user
     * [admin_url] => /wp-admin/user-edit.php?user_id=4
     * )
     */
    
    // Get field input value
    $my_field = get_field('my_field');
    
    if($my_field === 'Company'){
        
        // Set a custom key
        $data['custom_key'] = 'custom_value';
        
    }
    
    // return
    return $data;
    
}