0

We are looking for a way to add Advanced Custom Fields data to a user / subscriber after they have purchased a product (Woocommerce). The subscriber should be able to edit these fields on the frontend and save to their own account. We have been using Buddypress's XProfile fields to handle this but isn't ideal for what we need to do.

We know you can use ACF to add user fields but how to then bring those fields to the frontend?

0

You can use ACF on the frontend in similar way

$user_id = 'user_' . $user->ID . '';
$field = get_field( 'some_field', $user_id );

Please note that some settings must be set

enter image description here

Than you can use AJAX to update necessary fields from the frontend

$post_id = $_POST['post_id'];
$some_post_data = $_POST['some_post_data'];
$field_ref = get_field_reference('some_field', $post_id);
$field = get_field('some_field', $post_id);

$value = $field;
var_dump($value);
//$value modification
update_field($field_ref, $value, $post_id);
  • 1
    This is missing info on how to update the field value on front end – mrben522 Feb 6 at 19:18
0

ACF has a built in way to do this. Its acf_form(). Essentially it lets you build a form on the frontend that will update your custom fields.

Here is a basic example from their site. Note the use of acf_form_head(), which is needed to actually save the data.

<?php acf_form_head(); ?>
<?php get_header(); ?>

    <div id="primary" class="content-area">
        <div id="content" class="site-content" role="main">

            <?php /* The loop */ ?>
            <?php while ( have_posts() ) : the_post(); ?>

                <?php acf_form(array(
                    'post_id'   => 123,
                    'post_title'    => false,
                    'submit_value'  => 'Update the post!'
                )); ?>

            <?php endwhile; ?>

        </div><!-- #content -->
    </div><!-- #primary -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>

As stated in one of the comments, this tutorial walks you through basically what you are trying to do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.