Support

Account

Home Forums Front-end Issues Front End and custom user meta

Solved

Front End and custom user meta

  • Hi, I’m creating a user registration form in the front end. I already used acf_form to create and update posts in the frontend but I cannot find any documentation about usermeta in the frontend. I would like my users to be able to create and edit their profile in the frontend.
    Is it possible at the moment?
    Thanks !

  • Hi @dominic

    Currently, this is not an easy task with ACF, but it is possible.

    I would first use another plugin to create the user edit form, then find an action which allows you to add extra HTML inside the form.

    In this action, you can ask ACF to render the custom fields like so:

    
    
    acf_form(array(
    	'field_groups' => array(123) // the ID of the field group
    	'form' => false
    ));
    
    

    This will produce the ACF HTML without the form tags.

    Once you have the fields appearing, let me know.

    Cheers
    Elliot

  • Hi Sorry to jump in on Dominic’s thread but I have used the code above in a similar scenario and the fields are appearing, I just need to get them to save to the new users profile

  • Hi @richb

    If the fields show correctly, then all that needs to be done is to run the acf/save_post action during the save of the form.

    You can do this by:
    1. Find a hook that is run during the form save. This will not be an ACF action or filter, but specific to the form plugin or WP

    2. Create a function and hook it into the above. In this function you need to run the acf/save_post action like this:

    
    // $post_id to save against
    $post_id = 'user_' . $user_id;
    
    // update the post
    do_action('acf/save_post', $post_id);
    
    

    Note. The ACF plugin already hooks into the user_register action and runs a save function similar to above…

    I would think that this would already do the work for you but perhaps it is not firing in your setup?

    Thanks
    E

  • Hi Elliot

    Thank you so much, adding the code you suggested into the form save worked a charm.

    The only remaining issue I am having is that when a user returns to edit the same form once registered the ACF fields are not automatically populated with the saved user meta from the original registration.

    I have checked in the admin and all the user meta is being saved correctly.

    Thanks again
    Rich

  • Elliot, thanks for your reply ! @richb, did you try passing something like 'post_id' => 'user_'.$current_user->ID as an arg to acf_form in order to populate your fields ? It worked for me.

    Elliot, I used a couple of hacks to get it working but it’s not as clean as your solution. I did that just before I left the office for holidays and I don’t have my code with me and I can’t remember exactly what I did but I’ll get back here with some code snippets in a couple of days…

    Cheers !
    Dominic

  • @dominic thanks that worked a treat!! The users meta is saving correctly and can be edited in the same form, perfect.

    Cheeky side question, the theme im using has a similar form for creating / editing posts, if I use the same code as above the post does save however it also saves to the users meta, how would I use the code above but for post creation / editing?

    @elliot thanks again for your help!

  • Hi Elliot,

    I have the same situation as dominic. However, i am not sure how to display the field group in the frontend.

    
    
    acf_form(array(
    	'field_groups' => array(123) // the ID of the field group
    	'form' => false
    ));
    

    Can you provide some example or step for me?

    Cheers,
    Karl

  • @richb, @karlkinkin, maybe you guys should read that great article posted by @elliot : http://www.advancedcustomfields.com/resources/tutorials/using-acf_form-to-create-a-new-post/ and this one also of not done yet : http://www.advancedcustomfields.com/resources/tutorials/creating-a-front-end-form/

    Here is a quick overview of what I understand from these articles, hope it helps :

    According to the atticle called “Using acf_form to create a new post”, by hooking to acf/pre_save_post, the function you assign (my_pre_save_post) receives an ID. This ID comes from the post_id passed to acf_form function. If you pass it “new” instead of a post ID, you can do whatever you want, such as creating a post, plus, your custom fields will be saved automatically if you return a post ID after your custom code is executed. You can also see that the function my_pre_save_post returns the post ID and does nothing else if the ID is different than “new”. This is because the function acf_form does the updating job automatically, so there is no need to hook to acf/pre_save_post here.

    Also, by adding the prefix “user_” to your ID, acf_form saves user metas instead of post metas. Ain’t that great !

    That said, @richb, you might need to split your user management and post creation/updating in two separate pages. I don’t know exactly what your use case is but it looks you are calling acf_form with wrong parameters.

    Cheers !
    Dominic

  • @dominic Thank you so much for the detailed reply, it was a huge help.
    pre_save_post was the way to go and the problem is solved 🙂

  • Hello, I am trying to save user metadata from a form. I use the plugin WP User Frontend to store normal user data at front. The problem is that i can’t save my extended user metadatas.To display the extended fields all goes well, I have a template with the code indicated ..

    I wonder what hook to use to call the function with the “do_action (‘acf / save_post’)”.

    I guess the only function has to make the call to the hook.
    Will this new role I can invoke it from the “functions.php”?

    I tried the wordpress hooks but I can’t find adequate, on the other hand, does the hook has to be necessarily during the process? Can be just following standard data saving a user?

    I have tried many things already and nothing works for me, thank you very much.

  • Hi @dominic

    I doubt the action do_action (‘acf / save_post’) will be of much help to you as the names of your fields (from the WP User Frontend plugin) will not be in the ACF format.

    What I would do is research the WP User Frontend plugin docs for an action which is triggered during the saving of the form. Hook into this action and then manually save the ACF data using the update_field function.

    You will find documentation about this function over on the docs page.

    Thanks
    E

  • I am using acf5 beta. The form is displaying on the front end fine, submits and then calls my php script with the acf save function in it.

    The php script called by the form action has the correct array within $_POST so the values are available, just not sure why acf is not saving them.

    1. The form

    <?php get_template_part('templates/wrapper-header'); ?>
    
    	<?php
    	/*
    	When the form is submitted we call this file to deal with it's submission
    	Below we have a function and then a condition to call the function. The conditional is $_POST['submit'] so the function is only called if the form is submitted
    	*/
    	
    	// Get the current user 1
    	$userID = get_current_user_id();
    	?>
    
    	<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
                            
            <h1 class="editor editor-title"><?php the_title(); ?></h1>
            
            <div class="editor editor-content"><?php the_content(); ?></div><!-- end editor -->
            
            <form method="post" action="<?php echo get_template_directory_uri(); ?>/frontend-form-acf-siteaddresses.php">
            
            	<?php
                acf_form(array(
                    'field_groups' => array('1632'), // the ID of the field group (group not field name or key, the entire field group)
    				'post_id' => 'user_'.$userID, // To populate the front end form fields with saved data
                    'form' => false // Don't load the full acf form, just load the fields into my html form
                ));
    			?>
                
                <input type="hidden" name="function" value="ttm_frontend_form_acf_siteaddresses">
    
            	<input type="submit" value="Submit">
    
            </form>
        
        <?php endwhile; ?>
    
    <?php get_template_part('templates/wrapper-footer');

    2. The php script called by the form (this is called correctly and the $_POST variable contains all of the submitted fields)

    <?php require_once($_SERVER['DOCUMENT_ROOT'].'/wp-load.php'); ?>
    
    <?php
    $userID = get_current_user_id();
    
    // $post_id to save against
    $postID = 'user_' . $userID;
    
    //echo '<pre>';
    //var_dump($fields);
    //echo '</pre>';
    
    // update the post
    do_action('acf/save_post', $postID);
    ?>

    Any ideas?

    Thanks

  • i want to create front end user edit profile with this plugin,
    i already able to call and save the field from ACF with the code i got from here. but, how i can display the rest of user field (default from WordPress) in my front end page?

    thank you

  • Hi adechriz
    you can use get_the_author_meta( $field, $userID )
    See here for more details:
    http://codex.wordpress.org/Function_Reference/get_the_author_meta

  • Thank you for your answer RichB, but that’s not what i mean.

    i want to create front end EDIT profile. not echo-ing the value of the field.

    see an attachment

  • Hmm it should be possible by tapping into the existing fields, name, display name etc etc.
    If not you might have to use a plugin like: https://wordpress.org/plugins/profile-builder/

  • aahh.. i already figure it out

    to call the default user field in ACF, just give it the exact field as long as its on _usermeta.

    now i’m trying to call the field from _users table

  • Can anybody post a postebin of the final code?
    Thanks.

  • @adechriz I was wondering if you found a way to get the options from the _users table? Working on something very similar to your project right now.

  • In case anyone finds this (like me), things seem to have gotten a lot simpler. The process I used to get get user-specific data saved for my WordPress site is:

    1. Make a new form with all the fields you want. Note the number in the url post=, this is the field group #.
    2. Make a Custom Page Template using the code below.
    3. Change the form you previously made so that its location is: “Post Type is equal to <Your Custom Page Template>”,
    4. Make a new page and use the Template you just made.
    5. done

    The code I used in the custom form:

    
    acf_form(array(
      'field_groups' => array(123), //use the field group for your form
      'post_id' => 'user_'.get_current_user_id(),
    ));
    
Viewing 21 posts - 1 through 21 (of 21 total)

The topic ‘Front End and custom user meta’ is closed to new replies.