Not too good with code, can somebody help please
-
Hi, all I want is to be able to format a field sow that my users only have to under their facebook name instead of the whole address. how can I do this?
https://www.ads-software.com/extend/plugins/buddypress-xprofile-custom-fields-type/
-
OMG sleepy typos, let me try again…lol
all I want is to be able to format a field so that my users only have to enter their facebook name instead of the whole address, and then it will show a link to their facebook profiles. Can someone please show me how can I do this?
Angel
Hi,
In the faq, I explain how to use the filter bxcft_show_field_value to modify the way you show your fields in profile page.
In your case, I think the best way is to create a textbox field named “Facebook”. Then in your functions.php of your theme, write this code:
add_filter( 'bxcft_show_field_value', 'my_show_field', 15, 4); function my_show_field($value_to_return, $type, $id, $value) { if ($type == 'textbox') { $value = str_replace("<p>", "", $value); $value = str_replace("</p>", "", $value); $field = new BP_XProfile_Field($id); if ($field->name == 'Facebook') { $value = trim(strip_tags($value)); $new_value = sprintf('<a href="https://www.facebook.com/%s">%s</a>', $value, $value); } else { $new_value = $value; } return '<p>'.$new_value.'</p>'; } return $value_to_return; }
This code will execute every time you need to show a profile field. It will check if the type of the field is a “textbox” and then it will check if the name of the field is “Facebook” if you create the field with a distinct name, please change this line:
if ($field->name == 'Facebook') {
Instead of “Facebook”, write the name of your field.
Then it creates the link to facebook with the name of the user in facebook.
Check this and tell me if it works. Thanks!
Thank you so much, that actually does help immensely. Except I have a new problem. When I try to use the same example you gave me for multiple fields but change the names, only the first field actually links properly. The rest just append the user information to my own websites URL. Am I doing something wrong? is there a better way to format multiple fields?
add_filter( 'bxcft_show_field_value', 'facebook_field', 15, 4); function facebook_field($value_to_return, $type, $id, $value) { if ($type == 'textbox') { $value = str_replace("<p>", "", $value); $value = str_replace("</p>", "", $value); $field = new BP_XProfile_Field($id); if ($field->name == 'Facebook') { $value = trim(strip_tags($value)); $new_value = sprintf('<a href="https://www.facebook.com/%s" target="_blank">%s</a>', $value, $value); } else { $new_value = $value; } return '<p>'.$new_value.'</p>'; } return $value_to_return; } add_filter( 'bxcft_show_field_value', 'google_plus_field', 15, 4); function google_plus_field($value_to_return, $type, $id, $value) { if ($type == 'textbox') { $value = str_replace("<p>", "", $value); $value = str_replace("</p>", "", $value); $field = new BP_XProfile_Field($id); if ($field->name == 'Google Plus ID') { $value = trim(strip_tags($value)); $new_value = sprintf('<a href="https://plus.google.com/%s" target="_blank">%s</a>', $value, $value); } else { $new_value = $value; } return '<p>'.$new_value.'</p>'; } return $value_to_return; } add_filter( 'bxcft_show_field_value', 'twitter_field', 15, 4); function twitter_field($value_to_return, $type, $id, $value) { if ($type == 'textbox') { $value = str_replace("<p>", "", $value); $value = str_replace("</p>", "", $value); $field = new BP_XProfile_Field($id); if ($field->name == 'Twitter') { $value = trim(strip_tags($value)); $new_value = sprintf('<a href="https://www.twitter.com/%s" target="_blank">%s</a>', $value, $value); } else { $new_value = $value; } return '<p>'.$new_value.'</p>'; } return $value_to_return; }
Ohh I figured it out ?? YAY ME :D…lol. I realized they all needed to be apart of the same function, it’s working properly now for all my fields ??
add_filter( 'bxcft_show_field_value', 'twitter_field', 15, 4); function twitter_field($value_to_return, $type, $id, $value) { if ($type == 'textbox') { $value = str_replace("<p>", "", $value); $value = str_replace("</p>", "", $value); $field = new BP_XProfile_Field($id); if ($field->name == 'Twitter') { $value = trim(strip_tags($value)); $new_value = sprintf('<a href="https://www.twitter.com/%s" target="_blank">%s</a>', $value, $value); } elseif ($field->name == 'Google Plus ID') { $value = trim(strip_tags($value)); $new_value = sprintf('<a href="https://plus.google.com/%s" target="_blank">%s</a>', $value, $value); } elseif ($field->name == 'Facebook') { $value = trim(strip_tags($value)); $new_value = sprintf('<a href="https://www.facebook.com/%s" target="_blank">%s</a>', $value, $value); } else { $new_value = $value; } return '<p>'.$new_value.'</p>'; } return $value_to_return; }
Great!! I’m glad to read this.
Hi Atallos,
I’m running into another issue I have added some custom code to my buddypress templates profile header
if ( $twitter_name = xprofile_get_field_data('Twitter') ) { echo '<a href="https://www.twitter.com/' . $twitter_name . '" target="_blank"><img src="'. get_bloginfo('template_url') .'/images/social_icons/twitter_icon.png">?'; }
The problem is as long as the code you gave me for the function is in my functions.php file the code above doesn’t display properly.
If this exists in functions.php
function custom_field_formats($value_to_return, $type, $id, $value) { if ($type == 'textbox') { $value = str_replace("<p>", "", $value); $value = str_replace("</p>", "", $value); $field = new BP_XProfile_Field($id); if ($field->name == 'Twitter') { $value = trim(strip_tags($value)); $new_value = sprintf('<a href="https://www.twitter.com/%s" target="_blank">%s</a>', $value, $value); } else { $new_value = $value; } return '<p>'.$new_value.'</p>'; } return $value_to_return; }
I get this in my profile header
https://i.imgur.com/07L2DvI.pngWithout it My profile header looks like this
Hi,
It’s because xprofile_get_field_data(‘Twitter’) is returning the link after the filter was applied, so your link is wrong, it’s something like this:
<a href="https://www.twitter.com/<p><a href="https://www.twitter.com/username" target="_blank">username</a></p>" target="_blank"><img src="'. get_bloginfo('template_url') .'/images/social_icons/twitter_icon.png">
I think you can solve this, using strip_tags:
if ( $twitter_name = strip_tags(xprofile_get_field_data('Twitter')) ) { echo '<a href="https://www.twitter.com/' . $twitter_name . '" target="_blank"><img src="'. get_bloginfo('template_url') .'/images/social_icons/twitter_icon.png"> '; }
If you write this, xprofile_get_field_data will return only the username without the p and a tags. I suppose you know that you need to close the a tag. The correct way should be:
if ( $twitter_name = xprofile_get_field_data('Twitter') ) { echo '<a href="https://www.twitter.com/' . $twitter_name . '" target="_blank"><img src="'. get_bloginfo('template_url') .'/images/social_icons/twitter_icon.png" /></a> '; }
Thank you so much, I’m still learning code, but this helps, and yeah I saw that I had forgotten to close the href tag after I posted this, silly me.
You have been so very helpful I truly appreciate it.
You’re welcome! ??
- The topic ‘Not too good with code, can somebody help please’ is closed to new replies.