WordPress Admin Custom Field

I’m trying to add a custom textarea in the admin area of wordpress using the file user-edit.php

I’ve added the textarea in the correct place and have already tried adding a new entry in the wp_usermeta in the database and it sucessfuly echo’s into my text box.

Read More

This Code Works

<tr>
    <th><label for="user_ad">Ad Code</label></th>
    <td><textarea name="user_ad" id="user_ad" rows="5" cols="30"><?php echo esc_attr($profileuser->user_ad) ?></textarea>
    </td>   
</tr>

However…I’m having trouble updating the wp_usermeta for this new textarea when the form is submitted.

I’ve tried using,

update_user_meta( $user_id, 'user_ad', $_POST['user_ad'] );

But that doesn’t work, I know it’s close, because it overwrites the current meta_value with a blank. For some reason the $_POST variable isn’t being passed along and I can’t figure out why. I’m not that experience with wordpress, so I might be overlooking something.

Thanks

Related posts

Leave a Reply

3 comments

  1. This blog post details one way to achieve what you’re after…

    http://justintadlock.com/archives/2009/09/10/adding-and-using-custom-user-profile-fields

    I may have misinterpreted your question, but it sounds awfully like you’ve modified the user-edit.php file.

    You really do NOT want to edit that page – it’s a part of WordPress core. Any changes you make will be overwritten when you upgrade WordPress.

    WordPress provides hooks so that you don’t have to edit the files. Admittedly the hooks aren’t particularly helpful in this instance, but it’s still the way you should do it. That is how the blog post linked above handles it.

  2. I suspect that you’re getting an empty value in :

    Try this code instead:

    if($_POST['user_ad'] != '') {
      $userAd = $_POST['user_ad'];
      update_user_meta( $user_id, 'user_ad', $userAd );
    }
    

    Then check whether you’re still getting this empty field getting over-written in DB or not.

  3. This is not a direct answer to your question, but you are implying that you are trying to do this hacking the core files… If so, you definitely need a plugin.

    I recently found this one and it’s awesome for adding all kind of custom user meta:
    Cimy User Extra Fields.

    Case you really modified core files, follow this instructions to restore your WordPress files to its original state.
    http://codex.wordpress.org/Updating_WordPress#Manual_Update

    Otherwise, there are lots of answers about these matters in wordpress.stackexchange.