• Hi,

    I am adding custom fields to my profile page, but using add_action hooks and custom functions. I have placed them at the end of the wp-includes/functions.php

    function extra_profile_fields ( $user ) {
    global $current_user;
    $type_of_data = gettype( $user );
    error_log("the type of data for $user is");
    

    add_action(‘profile_personal_options’, ‘extra_profile_fields’);

    However, I have found that $user is coming up empty.

    If there a different place where I should be be adding my hook and code so that $user comes up with the current user who is logged in?

    Thank you.

    Peter

Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    >> wp-includes/functions.php <<

    DO NOT EDIT CORE FILES!

    If you want to add fields, either write a plugin or add code to your theme’s functions.php file.

    Moderator bcworkz

    (@bcworkz)

    You’re telling error_log() to interpret $user as a string variable and not a literal. As $user is an object, you are not getting what you expect. Try
    error_log('Type of data for $user is ' . $type_of_data );
    Note single quotes instead of double.

    You’ll see “Type of data for $user is object” in your log.

    Note that the passed user may not be the logged in user. When an admin edits another user’s profile, you get the other user, not the logged in admin user.

    It cannot be said enough “DO NOT EDIT CORE FILES!” ??

    Thread Starter billybob71a

    (@billybob71a)

    Thanks,

    I have placed the code into the theme folder.

    For some reason , it is just pulling up the $user variable as string variable.

    function extra_profile_fields( $user ) {
    global $current_user;
    $integer_value = strlen( $user );
    $type_of_data = gettype( $user );
    error_log(‘the user variable is ‘ . $type_of_data );
    error_log(‘the integer value is ‘ . $integer_value);

    add_action(‘profile_personal_options’, ‘extra_profile_fields’);

    error log shows:
    [21-Sep-2018 17:41:46 UTC] the user variable is string
    [21-Sep-2018 17:41:46 UTC] the integer value is 0
    [21-Sep-2018 17:41:46 UTC] the type of data is string

    Thread Starter billybob71a

    (@billybob71a)

    Ok, within the code , I added:

    $current_user = wp_get_current_user();
    $temp_var = var_dump($current_user);

    result on the screen is:
    object(WP_User)#338 (8) { [“data”]=> object(stdClass)#355 (10) { [“ID”]=> string(2) “50” [“user_login”]=> string(10) “testuser20”

    which is good.

    However, how do I access the “ID” and “user_login” from what the dump shows me on the screen?

    Moderator bcworkz

    (@bcworkz)

    PHP object properties are accessed with the -> operator. Example:
    echo 'The current user ' . $current_user->user_login . ' has an ID of ' . $current_user->ID;

    I like to make use of PHP’s variable replacement within double quotes, like so:

    echo "The current user {$current_user->user_login}  
    has an ID of {$current_user->ID}";

    You don’t need {curly braces} with simple variables, only array or object references.

    BTW, when you post code in these forums in the future, please demarcate with backticks or use the code button. Failure to do so means other members cannot test your code for themselves or use it as a basis for creating working examples because the forum’s parser will corrupt code not designated as such.

    Only moderators can access uncorrupted code. I tested your previous code on my WP installation and got this:

    Warning ‘strlen() expects parameter 1 to be string, object given’
    [snip]
    the user variable is object

    I’ve no idea how you are getting a string on your site, but something is not right. It probably doesn’t matter at this point if you are after the current user. wp_get_current_user() is the way to go for that.

    Hi,
    Add the following code to the function.php

    add_action( 'edit_user_profile', 'edituser' );
     
    function edituser( $user )
    {
        echo '<h3 class="heading">Custom Fields</h3>';
         
        ?>
         
        <table class="form-table">
        <tr>
                <th><label for="contact">Contact</label></th>
     
            <td><input type="text" class="input-text form-control" name="contact" id="contact" />
                    </td>
     
        </tr>
        </table>
         
        <?php
    }
    add_action( 'show_user_profile', 'wk_custom_user_profile_fields' );
    
    add_action( 'edit_user_profile_update', 'save_user' );
     
    function save_user( $user_id )
    {
             
        $custom_data = $_POST['contact'];
        update_user_meta( $user_id, 'contact', $custom_data );
     
    }

    any doubt check the link
    https://webkul.com/blog/wordpress-custom-fields-user-profile-page/
    I hope this code is work

    [signature removed]

    • This reply was modified 6 years, 2 months ago by angelch.
    • This reply was modified 6 years, 2 months ago by stephencottontail. Reason: removed signature
    • This reply was modified 6 years, 2 months ago by bcworkz. Reason: code fixed
    Thread Starter billybob71a

    (@billybob71a)

    Hi Angelch,

    Yes, I used something similar to what you had in the end.

    Thanks.

    Peter

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘creating custom fields’ is closed to new replies.