Skip to:
Content
Pages
Categories
Search
Top
Bottom

Xprofile get field data checkbox returns array

  • Avatar of applegateian
    applegateian
    Participant

    @applegateian

    Hi

    I am using the following to get a members xprofile data displayed <?php echo xprofile_get_field_data( 'Specialist Sectors', get_the_author_id()) ?>

    It works fine for xprofile fields that are free text, but the fields that are checkboxes, it simply returns ‘array’ to the front end.

    Is there a way to include this so it works?

    Thanks,

    Ian

Viewing 22 replies - 1 through 22 (of 22 total)
  • Avatar of applegateian
    applegateian
    Participant

    @applegateian

    Can anyone help with this?

    Thanks

    Avatar of applegateian
    applegateian
    Participant

    @applegateian

    Really trying to get this one sorted, can’t find a fix – anybody know?

    Avatar of Paul Gibbs
    Paul Gibbs
    Keymaster

    @djpaul

    This is expected, because the function’s not intended to be used like this (for direct output to HTML).

    Are you trying to do this in a WordPress template — looks like on a author archive page template?

    Avatar of applegateian
    applegateian
    Participant

    @applegateian

    I See.

    Screenshot is here of what we are doing: screenshot

    I am trying to use it in a sidebar widget on a post page. Using the following PHP in the text widget to get back users info:

    <div class="<?php echo get_post_meta(get_the_id(), 'Account Type', true); ?>">
     <a href="/community/members/<?php echo get_the_author_meta('user_login') ?>"> <?php echo get_avatar ( get_the_author_meta('user_email'), $size = '192' ); ?>
    </a>
    <a  href="/community/members/<?php echo get_the_author_meta('user_login') ?>"><h2 class="profileHeader"> <?php echo get_the_author(); ?></h2></a>
    <h2 class="profileAccount"><?php echo get_post_meta(get_the_id(), 'Account Type', true); ?></h2>
    <h3>Specialist Sectors</h3>
    <?php echo xprofile_get_field_data( 'Specialist Sectors', get_the_author_id()) ?>
    <h3>Experience</h3>
    <?php echo xprofile_get_field_data( 'Experience', get_the_author_id()) ?>
    <h3>About</h3>
    <?php echo xprofile_get_field_data( 'About you', get_the_author_id()) ?>
    

    Thanks,

    Ian

    Avatar of shanebp
    shanebp
    Moderator

    @shanebp

    To return a csv string rather than array, try:

    xprofile_get_field_data( 'Specialist Sectors', get_the_author_id(), $multi_format = 'comma' )

    Avatar of Henry
    Henry
    Participant

    @henrywright-1

    With checkboxes you’re not storing a single item of data as the user can check many checkboxes. This is why the the data is stored in an array. If you want to display the list of values you can easily process the array with a for loop, echoing the value on each cycle.

    Avatar of Henry
    Henry
    Participant

    @henrywright-1

    Or of course use shanebpdev’s approach if you need a comma separated list

    Avatar of shanebp
    shanebp
    Moderator

    @shanebp

    @henrywright-1

    just fyi: the data is not stored in an array.
    It is stored as a serialized string.
    Depending on the multi_format value, that string can be returned as an array (and handled in a loop) or as a csv string.

    boone was kind enough to add the multi_format var awhile back so that we don’t always need to check type on the returned value in customized displays.

    Avatar of Henry
    Henry
    Participant

    @henrywright-1

    thanks @shanebp, nice one for explaining it all properly :}

    Avatar of applegateian
    applegateian
    Participant

    @applegateian

    Thanks all… @shanebp I’ve inputted the following but it isn’t showing anything, is my implementation incorrect?

    <?php echo xprofile_get_field_data( 'Specialist Sectors', get_the_author_id(), $multi_format = 'comma' ) ?>

    Thanks

    Avatar of shanebp
    shanebp
    Moderator

    @shanebp

    echo get_the_author_id() first to make sure it has a value.

    If it doesn’t, xprofile_get_field_data will return nothing.

    also – If the profile field in question is empty, nothing will be returned.

    Avatar of applegateian
    applegateian
    Participant

    @applegateian

    OK thanks…so I have switched it around as follows:

    <?php echo get_the_author_id(), xprofile_get_field_data( 'Specialist Sectors', $multi_format = 'comma' ) ?>

    I am now getting something returned, which is unique for each user but it’s returning a number, such as 52, 65 or 1 – I don’t know what these refer to.

    Any ideas?

    Avatar of Henry
    Henry
    Participant

    @henrywright-1

    @applegateian

    That doesn’t look quite right to me. I’ve never used the $multi_format approach myself so perhaps shanebpdev can advise on that. My approach would be this:

    
    <?php if ( $sectors = bp_get_profile_field_data( 'field=Specialist Sectors' ) ) { ?>
    		<?php foreach ( $sectors as $sector ) { ?>
    			<?php echo $sector; ?>
    		<?php } ?>
    <?php } ?>
    
    Avatar of applegateian
    applegateian
    Participant

    @applegateian

    Unfortunately that is returning blank too @henrywright-1

    Thanks

    Avatar of Henry
    Henry
    Participant

    @henrywright-1

    May I ask where in your theme you’re using the code?

    Also, double check the exact spelling you’ve used for the field name matches “Specialist Sectors”. Also try editing your profile making sure a few checkboxes have actually been checked. Sometimes it is the most obvious parts of the process that cause the issue.

    Avatar of applegateian
    applegateian
    Participant

    @applegateian

    Hi there

    I’m using it in a text widget sidebar, which works for all the other fields.

    The spelling is correct and there are multiple options selected.

    Thanks,

    Ian

    Avatar of Henry
    Henry
    Participant

    @henrywright-1

    Try using the code in your user profile template, then if that works you’ll know the widget is the issue.

    Avatar of applegateian
    applegateian
    Participant

    @applegateian

    Yes it works on profiles.php… but the text widget I use on a post page works for all the other xprofile fields?

    Avatar of Henry
    Henry
    Participant

    @henrywright-1

    Can you paste the exact code which you are using in the text widget on the post page that works, and can you then paste the code you are using in the post page widget that does not work?

    Avatar of applegateian
    applegateian
    Participant

    @applegateian

    Hi there

    In the page template test I used your code exactly, within the profile.php file.

    In the text widget I use the following (all of which work except Specialist Sectors):

    `<div class=”<?php echo get_post_meta(get_the_id(), ‘Account Type’, true); ?>”>
    “>
    <?php if ( $sectors = bp_get_profile_field_data( ‘field=Specialist Sectors’ ) ) { ?>
    <?php foreach ( $sectors as $sector ) { ?>
    <?php echo $sector; ?>
    <?php } ?>
    <?php } ?>

    “><h2 class=”profileHeader”> <?php echo get_the_author(); ?></h2>
    <h2 class=”profileAccount”><?php echo get_post_meta(get_the_id(), ‘Account Type’, true); ?></h2>
    <h3>Specialist Sectors</h3>

    <h3>Telephone Number</h3>
    <?php echo xprofile_get_field_data( ‘Telephone Number’, get_the_author_id()) ?>
    <h3>Email</h3>
    “><?php echo xprofile_get_field_data( ‘Contact Email Address’, get_the_author_id()) ?>

    Avatar of Henry
    Henry
    Participant

    @henrywright-1

    Thanks for pasting everything. I see now exactly what you’re trying to do. This should work:

    <?php if ( $sectors = xprofile_get_field_data( 'Specialist Sectors', get_the_author_id() ) ) { ?>
    		<?php foreach ( $sectors as $sector ) { ?>
    			<?php echo $sector; ?>
    		<?php } ?>
    <?php } ?>
    Avatar of applegateian
    applegateian
    Participant

    @applegateian

    Perfect!Screenshot

    Thanks SO much.

Viewing 22 replies - 1 through 22 (of 22 total)

You must be logged in to reply to this topic.