Edit a user profile field on front end

I have a user profile field that I would like to allow users to edit, but I have the backend dashboardprofile disabled.

I have a user profile field that I would like to allow users to edit, but I have the backend dashboard/profile disabled. If I only know the meta (dbem_paypal_account), how can I make this work? I've tried a few plugins, but have had no luck so far.

Basically, I would like to add a text field onto a page allowing the user to change this particular field. I hope that's clear. Thanks yall!

I have a user profile field that I would like to allow users to edit, but I have the backend dashboard/profile disabled. If I only know the meta (dbem_paypal_account), how can I make this work? I've tried a few plugins, but have had no luck so far.

Basically, I would like to add a text field onto a page allowing the user to change this particular field. I hope that's clear. Thanks yall!

Share Improve this question asked Aug 9, 2019 at 21:14 Beefi1123Beefi1123 32 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

ACF is great for managing user custom fields, but it's also fairly simple to do what you're asking using just update_user_meta() to save custom data and then retrieve it with $user->get( 'whatever' );

Here's an example of a form that let's users set and update their twitter handle:

<?php 

// Grab the current user (Or redirect to login page)
$user = wp_get_current_user();
if ( !$user->exists() ) {
  wp_safe_redirect(wp_login_url(home_url($_SERVER['REQUEST_URI'])));
  exit;
}


// Check if form was submitted
if ( isset($_POST['submitted']) ) {

  // Verify nonce
  if ( !isset($_POST['_wpnonce']) || !wp_verify_nonce( $_POST['_wpnonce'], 'user_form' ) ) {
    wp_die("Invalid nonce.");
  }

  update_user_meta( $user->ID, 'twitter_handle', $_POST['twitter_handle'] );

  // Do a PGP redirect to prevent submitting the same for data multiple times.
  // Read more about PGP here: https://en.wikipedia/wiki/Post/Redirect/Get
  wp_safe_redirect( add_query_arg( 'updated', '', home_url($_POST['_wp_http_referer']) ) );
  exit;
}

get_header();

?>

<?php if ( isset($_GET['updated']) ) : ?>
<p>Your twitter handle has been updated!</p>
<?php endif; ?>

<form method="POST">
  <label for="twitter_handle">Twitter Handle</label>
  <input type="text" id="twitter_handle" name="twitter_handle" value="<?php echo esc_attr($user->get("twitter_handle")); ?>">
  <?php wp_nonce_field("user_form"); ?>
  <input type="hidden" name="submitted" value="1">
  <button type="submit">Save changes</button>
</form>

<?php get_footer(); ?>

Step 1: Get the user ID.

Assuming the user is logged in, which they would have to be to save data associated with their profile, you can grab their user id with the get_current_user_id function. That will be necessary, otherwise you're not going to be able to attach something to their profile.

Step 2: Create a front end page with a form.

I've done this before, using ACF, so I'm not 100% sure how your set up will function for this, but conceptually I had added custom fields to a profile with ACF. Then I made a front end form submission with ACF, and used its save post functions to push form field inputs to the posts, or profiles, as it were. This wasn't easy - I spent a lot of time doing it and I had to get really familiar with how WordPress user profiles worked.

Regardless, I don't think you're going to find an out of the box solution, I think you're looking at custom development. If that's not your wheelhouse, you'll want to hire a WordPress dev.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745252964a4618787.html

相关推荐

  • Edit a user profile field on front end

    I have a user profile field that I would like to allow users to edit, but I have the backend dashboardprofile disabled.

    2小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信