1

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 Answer 1

3

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'] );
3
  • might we go in chat as I have one more quick question?
    – rob.m
    Jul 11, 2019 at 1:48
  • What question? I can probably answer here.
    – Sally CJ
    Jul 11, 2019 at 1:56
  • nevermind, thanks
    – rob.m
    Jul 11, 2019 at 2:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.