Skip to:
Content
Pages
Categories
Search
Top
Bottom

How to list content relevant to a logged-in member’s interests (WAS: 1.2.6 upgrade – xprofile_get_fi


  • Mark
    Participant

    @tak5haka

    Having just updated to 1.2.6 I have found that the following function is no longer working and simply returns “Array”:

    `$profileTagFull = xprofile_get_field_data( ’25’ , bp_loggedin_user_id() );//Fetch the text for the tags
    $profileTagClean = xprofile_format_profile_field(‘checkbox’, $profileTagFull);//Clean up the data
    echo $profileTagClean;`

    Before the update, echoing $profileTagClean would return as a comma separated list.

    I noticed that 1.2.6 has “sanitised” xprofile field data – what does that mean exactly and has this affected how we now call checkbox-based field data?

    Thanks

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

  • Mark
    Participant

    @tak5haka

    Just wanted to bump this as it’s vitally important I get it fixed

    Thanks


    r-a-y
    Keymaster

    @r-a-y

    @tak5haka – If $profileTagClean returns an array, then you need to print out the variable as an array to debug further.

    What happens when you use the following instead of echo?
    `print_r($profileTagClean);`


    Mark
    Participant

    @tak5haka

    The same thing happens – it simply returns the word “Array” – the same if I use `print_r($profileTagFull);`

    This was working fine until the 1.2.6 upgrade which is why my suspicions are that the process of how to read xprofile field data if it was created via checkboxes has changed.

    Note that the code works fine if the xprofile field data contains text rather than the following (for example): `a:2:{i:0;s:10:”Governance”;i:1;s:8:”Training”;}`


    Mark
    Participant

    @tak5haka

    Also, this data returns fine in the member’s profile (“) but of course this doesn’t allow me to specify a particular field id to use.


    Mark
    Participant

    @tak5haka

    This has to be something to do with the serialization of the data in the database field, surely? I’ve tried `maybe_unserialize` and `unserialize` but with no luck.


    Boone Gorges
    Keymaster

    @boonebgorges

    The problem is being caused by the default filters that BP applies to ‘xprofile_get_field_data’. If you put the following lines in your bp-custom.php or functions.php, it will allow you to print_r the array properly:

    `remove_filter( ‘xprofile_get_field_data’, ‘wp_filter_kses’, 1 );
    remove_filter( ‘xprofile_get_field_data’, ‘force_balance_tags’ );
    remove_filter( ‘xprofile_get_field_data’, ‘stripslashes’ );`

    I’m not sure what the root issue is, though. I assume the problem is that wp_filter_kses etc don’t know what to do with an array. If that’s true, we’re going to have to figure out a way to sanitize profile data selectively (or at least smartly). I’ll look more deeply at the problem to see if I can come up with a better solution for BP core.


    Mark
    Participant

    @tak5haka

    Brilliant, now `printr($profileTagFull)` returns `ARRAY ( [0] => COMMUNICATIONS [1] => TECHNOLOGY )` so I now need to clean this up into a comma separated list.

    Thanks for your help (I put it in the theme’s functions.php by the way)


    Mark
    Participant

    @tak5haka

    So, anyone got any ideas on how to clean up this array and get it to show as a comma-separated list?

    Probably need to look up your php functions, what about implode() ?


    Mark
    Participant

    @tak5haka

    Doh! You know, I was using explode() later on to extract the individual elements – don’t know why I didn’t think of using implode() to clean up the array.

    Thanks @hnla – that works now. For reference, here is the full code – basically this uses the data derived from checkboxes in the member’s profile (in this case it’s called “Areas of Interest” with the field ID of 25 – change this to reflect your name/field ID) to hunt down related content tags and display a random list of content relevant to that member (remember to put the code mentioned by @boonebgorges (above) in your theme’s functions.php):

    `

    <?php //for use in the loop, list 5 post titles related to user profile interest areas – checkboxes in extended profile
    $backup = $post; // backup the current object
    //Fetch profile tags from custom field ‘Areas of Interest’ and clean up data
    $profileTagFull = xprofile_get_field_data( ’25’ , bp_loggedin_user_id() );//Fetch the text for the tags
    $profileTagClean = implode( ‘, ‘, $profileTagFull);//Clean up the data

    $profileargs=array(
    ‘tag_slug__in’ => explode( ‘, ‘, $profileTagClean ),
    ‘post__not_in’ => array($post->ID),
    ‘showposts’=>5,
    ‘orderby’ => rand,
    ‘order’ => DESC,
    ‘caller_get_posts’=>1
    );
    ?>

    Content of Interest
    ()

      <?php
      $profile_query = new WP_Query($profileargs);
      if( $profile_query->have_posts() ) {
      while ($profile_query->have_posts()) : $profile_query->the_post(); ?>

    • cat_name . ‘: ‘; ?><a href="” rel=”bookmark” title=””>
    • <?php endwhile;
      } else { ?>

    • No related content
    • <?php }

      $post = $orig_post;
      wp_reset_query();
      ?>

    `
    Thanks to @hnla @r-a-y and @boonebgorges for their help.

    Sounds rather interesting although I’m struggling slightly to mentally envision this in a page but that’s my slow brain :) need to study what you’ve written above in more detail.


    Mark
    Participant

    @tak5haka

    It’s to help deliver content that is of personal interest to the registered member based on their profile information, rather than just the normal ‘related content’ – personalisation of content is key, makes your site more relevant to the individual, increasing loyalty and stickiness.


    Roger Coathup
    Participant

    @rogercoathup

    @hnla – Might work well on the home page – you log in, and the sidebar immediately shows you a set of posts that are targeted to your interests.

    Personalised content if you like.

    It’s a useful snippet of code to bring posts relevant to some metadata (in this case an interests list). You could see it in other scenarios, e.g. you create a group about “Liquorice Allsorts” and a routine like this could pull in all relevant blog posts.

    @tak5haka – impressed you stuck with it through the upgrade issues.

    This may seem to be nit picking but I noted the print_r on the array to actually render browser output and within a heading tag.

    The heading tag use is debatable but as a heading probably ought not have list items in that manner I would have probably thought to output a floated ul li construct either below the heading or run inline to the heading and as such you wouldn’t need to implode the array but simply do a while loop on it?

    something like:
    `
    while (list(, $value) = each($profileTagFull)): ?>

    `
    that might also cut down the need for both explode/implode ?

    @rogercoathup hmm yes like the sound of personalised content, in fact like the sound of that a great deal. think I was struggling to fit the profile checkboxes in to the equation despite having played around with using profile fields to, in some manner, confer extra meta data on members but penny finally dropped, and I’m wondering whether this can be taken further.

    @tak5haka It’s a really nice idea, I had played with profile fields to see how effective it was to use radio button choices and/or checkboxes to use in profile checks to control display actions. Completely agree on personalisation of data, stickiness etc.


    Mark
    Participant

    @tak5haka

    Bear in mind the original code is simply a modification of the ‘related posts’ code from WPRecipes (http://www.wprecipes.com/how-to-show-related-posts-without-a-plugin) with additional modifications from elsewhere on this support forum (forgive me, but I can’t recall where I picked it up from).
    My coding isn’t the best in the world, but I know what I needed to achieve so feel free to tidy up, etc.

    In theory, you could use the extended profile to gather all sorts of information (location, language, favourite food) depending on your member base and site content and then modify this code to deliver highly personalised content based on this meta data (as long as your tagging is in place).

    Feel free to use and abuse, like I said it a combo from various sources so I can’t claim any rights to it! Go forth and personalise!


    Mark
    Participant

    @tak5haka

    Have cleaned up the code and plugged in the code suggested by @hnla – gets rid of the need for implode/explode and also allows for the list of tags from the member profile to be live:

    `

    <?php //for use in the loop, list 5 post titles related to user profile interest areas
    $backup = $post; // backup the current object
    //Fetch profile tags from custom field ‘Areas of Interest’
    $profileTagFull = xprofile_get_field_data( ’25’ , bp_loggedin_user_id() );//Fetch the text for the tags

    $profileargs=array(
    ‘tag_slug__in’ => $profileTagFull,
    ‘post__not_in’ => array($post->ID),
    ‘showposts’=>5,
    ‘orderby’ => rand,
    ‘order’ => DESC,
    ‘caller_get_posts’=>1
    );
    ?>

    Content of Interest

    • <a href="tag/”>
    •   

      <?php
      $profile_query = new WP_Query($profileargs);
      if( $profile_query->have_posts() ) {
      while ($profile_query->have_posts()) : $profile_query->the_post(); ?>

    • cat_name . ‘: ‘; ?><a href="” rel=”bookmark” title=””>
    • <?php endwhile;
      } else { ?>

    • No related content
    • <?php }
      $post = $orig_post;
      wp_reset_query();
      ?>

    `

    This is my CSS for the run-in list:

    `
    .comma-list ul, .comma-list li {
    color: #ffffff;
    font-size: 10px;
    text-transform: uppercase;
    display: inline;
    line-height: 12px;
    }

    .comma-list li {
    text-decoration: underline;
    }
    `

    Looks a lot neater now – thanks!

    Looks good, going to give that play with later :)

    Wonder how well browsers work now with display:run-in; on that heading as the property was made for something like this.

    Playing around a little and added couple of little things:

    `
    if (is_user_logged_in() && xprofile_get_field_data(’13’ , bp_loggedin_user_id()) )`

    Added the get_field in as a check to see if users had made any selections

    If not added a final ‘else’ to provide convenient link to the edit screen (hard coded values to be adjusted)

    `

    Content of Interest

    You have set no content of interest, if you would like to please visit your
    <a href= "/profile/edit/group/4″>Content Interest settingsin your profile

    `


    Mark
    Participant

    @tak5haka

    Cool – adds that extra level of usability, nice one!


    John James Jacoby
    Keymaster

    @johnjamesjacoby

    Fixed in branch. 1.2.7 is eminent to resolve this issue.

    So I asume – without testing so to speak – that the:
    `
    remove_filter( ‘xprofile_get_field_data’, ‘wp_filter_kses’, 1 );
    remove_filter( ‘xprofile_get_field_data’, ‘force_balance_tags’ );
    remove_filter( ‘xprofile_get_field_data’, ‘stripslashes’ );
    `
    Are now redundant and can be removed, but that the rest of the thread and code examples will work as shown?

Viewing 22 replies - 1 through 22 (of 22 total)
  • The topic ‘How to list content relevant to a logged-in member’s interests (WAS: 1.2.6 upgrade – xprofile_get_fi’ is closed to new replies.
Skip to toolbar