skip to Main Content

I created an ACF post type named "Profile"

I want logged in users to be able to fill out their own profile and edit it via Fluent Form.
A user only has one "Profile" post content, so when he goes to the Fluent Form linked to the ACF "Profile" content, he either creates a new post if he does not have one , or it updates its existing information.

So I use "Post/CPT Creation" with a Post Feed.

If I use Submission Type "New Post", one user can create multiple profile (it’s not what I want)

If I use Submission Type "Update Post" the content is not created. Maybe I need to create 2 Post Feed on "New Post" and another "Update Post". I don’t understand well…

I’ve also tried the "Post Update" field but as each user only has one post, I find that it makes no sense to ask them to select their profile in this select. (even if I limit the field with its userid)

Is there a way to have a form that if a user don’t have any Profile he can create it. And if he already have a profile the form will load its information to allow hime to update it.

I have de PHP / Javscript Dev so I can use snippets (function.php) if this is a solution.

Thanks

Samuel

2

Answers


  1. Chosen as BEST ANSWER

    Thanks a lot Ibrahim Mustafa !

    Your solution was good but the fluentfilter have changed

    Here is my solution :

    // return the ACF Post Id of current user
    function user_has_family_post() {
        $user_id = get_current_user_id();
        $args = array(
            'post_type' => 'family', // Adjust post type as needed
            'author'    => $user_id,
            'posts_per_page' => 1,
        );    
        $query = new WP_Query($args);    
        $query->have_posts();
        $query->the_post();
        return get_the_ID();
    }
    // modify FluentForm to set :
    // - user_has_family_post to true or false (use on conditionnal in fluentform submition type "create" or "update")
    // - fluentform "post_update" field to ACF Post Id of user content
    function set_user_has_family_field_value($form) {
        $profile_id = user_has_family_post();
        
        foreach($form->fields["fields"] as $key => $field):       
            if($field["attributes"]["name"] == 'user_has_family_post') {
                $form->fields["fields"][$key]["attributes"]["value"] = $profile_id ? 'true' : 'false';
            }
            if($field["attributes"]["name"] == 'post_update') {
                $form->fields["fields"][$key]["attributes"]["value"] = $profile_id;
            }        
        endforeach;
        return $form;
    } 
    add_filter('fluentform/rendering_form', 'set_user_has_family_field_value', 10, 2);
    

  2. Step 1: Determine if the User Has a Profile Post

    function user_has_profile_post() {
    $user_id = get_current_user_id();
    $args = array(
        'post_type' => 'profile', // Adjust post type as needed
        'author'    => $user_id,
        'posts_per_page' => 1,
    );
    
    $query = new WP_Query($args);
    
    return $query->have_posts();
    

    }

    Step 2: Create/Update Post Based on User Status

    Now, you’ll need to use Fluent Forms to determine if the user should create a new post or update an existing one. You can use the "Conditional Logic" settings within the form to achieve this.

    Create a Hidden Field for User Profile Check:

    Add a hidden field to your form and set its default value using the shortcode [user_has_profile] (adjust the shortcode based on your implementation).
    Set Conditional Logic for Creating/Updating:

    For the fields related to the profile information, set conditional logic to show them if the hidden field value is equal to "false" (indicating the user does not have a profile).
    For the same fields, set another set of conditional logic to show them if the hidden field value is equal to "true" (indicating the user has a profile).
    Create/Update Post Actions:

    For the "Submit" or "Update" button, use Fluent Forms’ Post Feed to create or update the post based on the conditional logic.
    For the "Post Feed" associated with creating a new post, set the condition to run when the hidden field value is equal to "false".
    For the "Post Feed" associated with updating a post, set the condition to run when the hidden field value is equal to "true".

    Step 3: Implementing the Hidden Field Logic
    Now, you’ll need to implement the logic for the hidden field value in your theme’s functions.php file.

    function set_user_profile_check_field_value($form, $args) {
    // Check if the user has a profile post
    $has_profile = user_has_profile_post();
    
    // Set the value of the hidden field based on the user's profile status
    $form['fields']['hidden_field']['defaultValue'] = $has_profile ? 'true' : 'false';
    
    return $form;
    

    }
    add_filter(‘fluentform_rendering_form_args’, ‘set_user_profile_check_field_value’, 10, 2);

    Replace ‘hidden_field’ with the actual name or ID of your hidden field.

    This way, the hidden field value will dynamically change based on whether the user has a profile post or not.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search