Home Forums General Issues User Name Field – Display Name Only Reply To: User Name Field – Display Name Only

  • Perhaps this is a difference with newer versions of PHP – but the syntax that @spacewindow provides no longer works – it throws a somewhat vague PHP error of “Warning: Illegal string offset”.

    After a lot of digging, I realized that the User field doesn’t apparently return an actual array, but instead returns an array in a string, thus triggering the warning.

    Therefore, the following works for retrieving the associative array element by position for nickname.

    $values = get_field('user_field_name');
    $array = array_values($values);
    echo $array[3];

    For reference on the keys depending on the User info you’re wanting to retrieve, using print_r(array_keys($values)); reveals the following:

    Array ( [0] => ID [1] => user_firstname [2] => user_lastname [3] => nickname [4] => user_nicename [5] => display_name [6] => user_email [7] => user_url [8] => user_registered [9] => user_description [10] => user_avatar )

    Just swap the number for the element you’re wanting.

    In case this helps anyone else save hours of digging.