php - Extra fields in Wordpress user panel not shown

483

Working on a plugin for wordpress and met the problem:

There has to be shown an extra field on a user info panel aka Wordpress Profile. I suppose that the following code cut has an logical mistake. I am new to php programming and WordPress.

  add_action( 'show_user_profile', 'show_internal_id' );
  add_action( 'edit_user_profile', 'show_internal_id' );

  function show_internal_id($user_id){ 

  $profileuser = get_user_to_edit($user_id);
  $global_uid = $profileuser->ID;

  $user_current = get_userdata($global_uid);
  $user_roles = $user_current->roles;

  foreach((array)$user_roles as $cur_role){
    if($cur_role=='internal_customer'){
      ?>

      <table >
        <tr>
          <th><label for="internal_id">Internal-ID</label></th>
          <td><input type="text" name="internal_id" id="internal_id" value="<?php echo esc_attr(get_user_meta( $user_id, 'internal_customer_id', true )); ?>" disabled /> </td>
        </tr>
      </table>

  <?php  }
    }
  }

this code cut was added to a plugin file.

511

Answer

Solution:

the functionshow_internal_id($user_id) doesn get any id as the argument. it gets the hole user object instead.

here is my problem solution:

function show_internal_id($user){ 

  $user_roles = $user->roles;
  foreach((array)$user_roles as $cur_role){
    if($cur_role=='internal_customer'){
      ?>

      <table >
        <tr>
          <th><label for="internal_id">Internal-ID</label></th>
          <td><input type="text" name="internal_id" id="internal_id" value="<?php echo esc_attr(get_user_meta( $user->id , 'internal_customer_id', true )); ?>" disabled /> </td>
        </tr>
      </table>

<?php  }
  }
}

People are also looking for solutions to the problem: php - storing users information in a session

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.