0

I found a plugin that allows me to display BuddyPress Xprofile fields anywhere on my website using a shortcode. It works great, except when the field has multiple values. Instead of the values, it displays the word "array".

I would like the code to display a list of all items that are part of that Xprofile field.

As an extra bonus, I would like the ability to use CSS to modify each item from the list. If each item had a class name added to it I think that would help out. Not sure if there's a better alternative to doing this. Maybe just output the items as a list and I can tap into the "li" class?

Any idea on how to edit this code to do this? I tried but my PHP coding skills are not up to the challenge :(

Here is the code:

    add_action('bp_init', 'td_bpxps_init');
function td_bpxps_init() {
    add_shortcode('xprofile', 'td_bpxps_xprofile_shortcode');
}

function td_bpxps_xprofile_shortcode($attributes) {
    if(empty($attributes['field'])) return false;
    extract($attributes);
    $user_id = (isset($user)) ? $user : false;
    global $bp;

    if (!empty($user_id) && intval($user_id)===0) {
        // shortcode-defined username
        $user_id = get_user_by( 'slug', $user_id);
    }
    if ((isset($user) && $user=='displayed') ||
        (empty($user_id) && isset($bp->displayed_user->id))) { 
        // On profile page, show the displayed user's information
        $user_id = $bp->displayed_user->id;
    }

    global $post;
    if ((isset($user) && $user=='author') ||
        (empty($user_id) && !empty($post->post_author))) {
        // On author or single post page, show the author's information
        $user_id = $post->post_author;
    }
    if ((isset($user) && $user=='current') ||
        (empty($user_id) && is_user_logged_in())) {
        // Show the currently logged in user's information
        $user_id = get_current_user_id();
    }
    if (empty($user_id)) return false;

    return xprofile_get_field_data($field, $user_id);
}

?>

Author: Tyler Digital http://tylerdigital.com/ Released under the GPL license

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.