Code Share: Customize output for user attributes on public profile pages
Permalink 1 user found helpful
So you added some new user attributes, but want to customize how they display on the public profile page?
I know, yes, you could create custom attribute types, but that seemed unnecessary in this case. I added two new attributes -- a website address and a Twitter account. Instead of displaying plain text, I wanted these to link on user profiles.
1.) Copy "view.php" to /single_pages/profile
The first step is to copy "view.php" from /concrete/single_pages/profile and to place it in a top-level /single_pages/profile directory. If you just installed Concrete, the /concrete folder will be at the top-level where you installed it.
If you have upgraded since then, your /concrete folder will be located under /updates
2.) Add a switch
In my "view.php" file, here was the original code to display the attribute value:
You can expand this to switch based on the attribute handle, then render as you see fit. Here was my code to link the website address as well as the Twitter handle:
Depending on the attributes you add, you can change this code to work for your own values. This code will still display attributes that have not been handled with custom code, so you don't have to come back and update the page every time you decide to add a new attribute.
Not too hard, right?
I know, yes, you could create custom attribute types, but that seemed unnecessary in this case. I added two new attributes -- a website address and a Twitter account. Instead of displaying plain text, I wanted these to link on user profiles.
1.) Copy "view.php" to /single_pages/profile
The first step is to copy "view.php" from /concrete/single_pages/profile and to place it in a top-level /single_pages/profile directory. If you just installed Concrete, the /concrete folder will be at the top-level where you installed it.
If you have upgraded since then, your /concrete folder will be located under /updates
2.) Add a switch
In my "view.php" file, here was the original code to display the attribute value:
<?php echo $profile->getAttribute($ua, 'displaySanitized', 'display'); ?>
You can expand this to switch based on the attribute handle, then render as you see fit. Here was my code to link the website address as well as the Twitter handle:
<?php $attributeValue = $profile->getAttribute($ua, 'displaySanitized', 'display'); switch ($ua -> getKeyHandle ()) { case "twitter_handle": $handle = str_replace ('@', '', $attributeValue); echo '<a href="http://www.twitter.com/' . $handle . '">@' . $handle . '</a>'; break; case "website": echo '<a href="' . $attributeValue . '">' . $attributeValue . '</a>'; break; default: echo $attributeValue; break; } ?>
Depending on the attributes you add, you can change this code to work for your own values. This code will still display attributes that have not been handled with custom code, so you don't have to come back and update the page every time you decide to add a new attribute.
Not too hard, right?
I have been looking around on how to add links in a user profile for a week now...This worked great!
elyon,
To take this the next step by involving CSS we could then style, place, color, etc.
but where does one place the <div> or <span> to manipulate the both the label(handle) and value.
What I am looking to create is 3 boxes floating next to each other each with whatever attributes I place in them.
Basically, just knowing how to target one whole attribute would be great.
To take this the next step by involving CSS we could then style, place, color, etc.
but where does one place the <div> or <span> to manipulate the both the label(handle) and value.
What I am looking to create is 3 boxes floating next to each other each with whatever attributes I place in them.
Basically, just knowing how to target one whole attribute would be great.