0

I am saving extra profile fields to my users' profiles - not via User admin but via another method (a plugin, but that's not important - all the user data is there properly).

Now I do also want to show the extra info on User profiles in the admin backend, and make them editable there, too. I have followed the Justin Tadlock code at http://justintadlock.com/archives/2009/09/10/adding-and-using-custom-user-profile-fields - that means I have added some actions and two functions to functions.php, to support displaying and updating these fields on the backend.

The problem is, fields whose names include underscores are not returning results properly.

ie. Whilst I can properly return the value of "company", the value of "company_url" is also returned as though it were "company" (eg. "Microsoft" instead of "http://www.microsoft.com").

My two functions are below. I haven't yet updated the save function as this would just corrupt the User data with a duplicate of the "company" value. I have many field names containing "_", so this will be important.

 /*
    * Extra profile fields: Show on user admin backend
  * http://justintadlock.com/archives/2009/09/10/adding-and-using-custom-user-profile-fields
    */

 add_action( 'show_user_profile', 'mysite_show_user_meta' );
 add_action( 'edit_user_profile', 'mysite_show_extra_profile_fields' );

 function mysite_show_extra_profile_fields( $user ) { ?>

    <h3>Extra profile information</h3>

    <table class="form-table">

        <tr>
            <th><label for="twitter">Twitter</label></th>
            <td>
                <input type="text" name="twitter" id="twitter" value="<?php echo esc_attr( get_the_author_meta( 'twitter', $user->ID ) ); ?>" class="regular-text" /><br />
                <span class="description">Please enter your Twitter username.</span>
            </td>
        </tr>

    <tr>
            <th><label for="company">Company</label></th>
            <td>
                <input type="text" name="company" id="company" value="<?php echo esc_attr( get_the_author_meta( 'company', $user->ID ) ); ?>" class="regular-text" /><br />
                <span class="description">Please enter your company.</span>
            </td>
        </tr>
    <tr>
            <th><label for="company_url">Company website</label></th>
            <td>
                <input type="text" name="company_url" id="company_url" value="<?php echo esc_attr( get_the_author_meta( 'company_url', $user->ID ) ); ?>" class="regular-text" /><br />
                <span class="description">Please enter your company website URL.</span>
            </td>
        </tr>
    <tr>
            <th><label for="photo_url">Photo URL</label></th>
            <td>
                <input type="text" name="photo_url" id="photo_url" value="<?php echo esc_attr( get_the_author_meta( 'photo_url', $user->ID ) ); ?>" class="regular-text" /><br />
                <span class="description">Please enter your photo URL.   </span>
            </td>
        </tr>
    </table>
 <?php }


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

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

 function mysite_user_meta( $user_id ) {

    if ( !current_user_can( 'edit_user', $user_id ) )
        return false;

    /* Copy and paste this line for additional fields. Make sure to change 'twitter' to the field ID. */
    update_usermeta( $user_id, 'twitter', $_POST['twitter'] );
 }

2 Answers 2

0

You really should rename your field names with a hyphen (-) instead of an underscore. There is no solution for that other than replacing the underscores (_) for the hyphens (-) I read somewhere that the problem is with the new php naming conventions and when used in functions, it separates the words when an underscore is used. And since Wordpress uses php in its core functions, it is still a bug. I am sorry, that your problem couldn't be solved, but if the form is the only place where your fields

1
  • Even mid-field name? Not just when something starts with an underscore? I wonder how I can rename all affected custom user meta names... ? Feb 27, 2016 at 23:43
0

Follow-up - this was not about an underscore problem. The problem was mine. The value of company_url matched the value of company. That was my fault for incorrectly duplicating the data by import. This was fixed by reimporting properly.

Your Answer

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

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