最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Add user custom fields and make editable on frontend

programmeradmin0浏览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?

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?

Share Improve this question asked Feb 3, 2019 at 5:47 BenjaminBenjamin 1852 silver badges14 bronze badges 1
  • Here's a nice little tutorial on the ACF way of doing that. usersinsights/acf-user-profile – mrben522 Commented Feb 6, 2019 at 19:21
Add a comment  | 

2 Answers 2

Reset to default 3

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.

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

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);
发布评论

评论列表(0)

  1. 暂无评论