admin管理员组

文章数量:1305501

I have custom fields on the user profile on the back end that allows the admin user to update a user's profile. Here's the back end.

When admin saves the profile these fields show up on the front end, like this:

That is working perfectly. Part of the form allows the user to choose a selection which when submitted should update the users profile on the back end. But this is not working.

Problem 1: The form seems to submit but the variable($sent) doesn't update the Wordpress user profile. Problem 2: The page redirects when the form is submitted.

Here's the code and a visual. I've created a variable and set up the form in custom-fields.php:

<?php $sent = get_user_meta($user_id, 'payment-sent', true); ?>

    <form action="custom-fields.php" method="get">
        <div class="field">
            <label for="payment-sent">Have you sent the payment?</label>
            <select id="payment-sent" name="payment-sent" size="">
                <option value="no" <?php selected($sent, 'not_sent')?>>
                No, I have not sent payment yet</option>
                <option value="yes" <?php selected($sent, 'sent')?>>
                Yes, payment is sent</option>
            </select>
        </div>
        <span class="button"><input type="submit"></span>
    </form>

In functions.php I've added the following:


<?php

  //create custom fields
  add_action('show_user_profile', 'wk_custom_user_profile_fields');
  add_action('edit_user_profile', 'wk_custom_user_profile_fields');

  function wk_custom_user_profile_fields($user)
  {
    $sent = get_user_meta($user->ID, 'payment-sent', true);
?>

  //custom field html
  <tr>
    <th><label for="payment-sent">Payment Sent</label></th>

    <td>
        <input type="text" value="<?php echo $sent ?>" readonly="readonly">
    </td>
  </tr>

Here I'm saving the updated info.

  <?php
  }

  //Save custom user profile meta data fields
  add_action('personal_options_update', 'wk_save_custom_user_profile_fields');
  add_action('edit_user_profile_update', 'wk_save_custom_user_profile_fields');

  function wk_save_custom_user_profile_fields($user_id)
{
    if (!current_user_can('edit_user', $user_id)) {
        return false;
    }
    update_user_meta($user_id, 'payment-sent', $_POST['payment-sent']);
}

?>

But as you can see its not updating in the dashboard.

What am I doing wrong? Any help would be much appreciated.

本文标签: phpwordpress allow user to edit user profile with custom fields