admin管理员组

文章数量:1417099

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.

本文标签: Edit a user profile field on front end