By rob.m


2019-07-11 01:19:33 8 Comments

I use the following to add and display new user meta fields on the user profile editor screen but when I click the update button, the values don't change:

function extra_user_profile_fields( $user ) { ?>
    <table class="form-table">
      <tr>
          <th><label for="save_post_external"><?php _e("Save posts external"); ?></label></th>
          <td>
              <input type="text" name="save_post_external" id="save_post_external" value="<?php echo esc_attr( get_the_author_meta( 'save_post_external', $user->ID ) ); ?>" class="regular-text" /><br />
          </td>
      </tr>
      <tr>
          <th><label for="save_post_internal"><?php _e("Save posts internal"); ?></label></th>
          <td>
              <input type="text" name="save_post_internal" id="save_post_internal" value="<?php echo esc_attr( get_the_author_meta( 'save_post_internal', $user->ID ) ); ?>" class="regular-text" /><br />
          </td>
      </tr>
    </table>
  <?php }
  add_action( 'show_user_profile', 'extra_user_profile_fields' );
  add_action( 'edit_user_profile', 'extra_user_profile_fields' );


  add_action( 'personal_options_update', 'save_extra_user_profile_fields' );
  add_action( 'edit_user_profile_update', 'save_extra_user_profile_fields' );

  function save_extra_user_profile_fields( $user_id ) {
    if ( !current_user_can( 'edit_user', $user_id ) ) { 
      return false; 
    }
    update_user_meta( $user_id, 'Save Posts', $_POST['save_post_external'] );
    update_user_meta( $user_id, 'Save Posts', $_POST['save_post_internal'] );
  }

1 comments

@Sally CJ 2019-07-11 01:43:33

You are using the wrong meta key:

update_user_meta( $user_id, 'Save Posts', $_POST['save_post_external'] );
update_user_meta( $user_id, 'Save Posts', $_POST['save_post_internal'] );

And that should be:

update_user_meta( $user_id, 'save_post_external', $_POST['save_post_external'] );
update_user_meta( $user_id, 'save_post_internal', $_POST['save_post_internal'] );

@rob.m 2019-07-11 01:48:01

might we go in chat as I have one more quick question?

@Sally CJ 2019-07-11 01:56:53

What question? I can probably answer here.

@rob.m 2019-07-11 02:31:09

nevermind, thanks

Related Questions

Sponsored Content

1 Answered Questions

How to add custom detail page for a Subscriber at Admin Panel

  • 2016-03-14 23:32:16
  • mohsinrafique
  • 689 View
  • 0 Score
  • 1 Answer
  • Tags:   users user-meta

1 Answered Questions

Cannot Update user meta in custom field

  • 2018-08-30 10:01:47
  • Diana Rider
  • 145 View
  • 0 Score
  • 1 Answer
  • Tags:   users user-meta

1 Answered Questions

[SOLVED] update multiple user meta field based on another meta field

1 Answered Questions

[SOLVED] How to Duplicate (multiple meta box)?

1 Answered Questions

[SOLVED] WP Cron: Save third party data as user meta

1 Answered Questions

[SOLVED] How to delete user meta by key

  • 2014-03-02 14:09:57
  • Neovea
  • 2593 View
  • 0 Score
  • 1 Answer
  • Tags:   plugins user-meta

1 Answered Questions

[SOLVED] Display and Update User Custom Meta via user-edit.php

  • 2014-03-01 00:23:00
  • user48333
  • 599 View
  • 1 Score
  • 1 Answer
  • Tags:   user-meta

Sponsored Content