Trouble Creating Age Range
-
Hello
I am trying to create an age selector profile field between 18-99 that doesn’t use DOB entry upon registration.
Is this possible and could you advise how to implement it?
Regards
-
Hello swalky,
As far I know, the only way is to create a “Drop Down Select Box” profile field and manually enter the options from 18 to 99.
When you use that field in a BP Profile Search form, you’ll get the search modes ‘is’ and ‘is one of’. In order to get the ‘range’ search mode, add this code to bp-custom.php:
add_filter ('bps_xprofile_format', 'change_format', 10, 2); function change_format ($default, $field_id) { if ($field_id == 143) return 'decimal'; return $default; }Additionally, if you wish to show the age range search field as two drop-downs instead of two input boxes, add this code to bp-custom.php:
add_action ('bps_field_before_search_form', 'change_display'); function change_display ($f) { if ($f->code == 'field_143' && $f->display == 'range') { $f->display = 'range-select'; $f->options = array ('' => '') + $f->options; } }Replace 143 with the actual field ID of your Age field.
Thank you @dontdream for providing the snippets. Please could you confirm how i can access the ‘bp-custom.php:’ file through the wp-admin dashboard, as i’m not using an FTP file manager?
I cannot see the file under BuddyPress plugin files.
Also, where do i find the field i.D?
Hi swalky,
The bp-custom.php file is in the wp-content/plugins directory, you can create it yourself if you don’t have one.
Unfortunately bp-custom.php can’t be seen from your dashboard Plugin Editor, but you can add the code snippets in your theme’s functions.php as well.
To get a field ID, go to Users -> Profile Fields in your dashboard, and Edit your field. The field ID will then be visible in your browser’s address bar, it’s the last field in the page URL.
Hi @dontdream, i have created the bp-custom.php file and added it via ftp to the plugins directory, it contains the following text:
“<?php
//
add_action (‘bps_field_before_search_form’, ‘change_display’);
function change_display ($f)
{
if ($f->code == ‘field_4235’ && $f->display == ‘range’)
{
$f->display = ‘range-select’;
$f->options = array (” => ”) + $f->options;
}
}”This unfortunately did not fix the issue and so i followed step 2 of your instructions and tried adding it to the functions.php instead, but received this error message:
“Your PHP code changes were rolled back due to an error on line 1579 of file wp-content/themes/gwangi/functions.php. Please fix and try saving again.
syntax error, unexpected end of file”
Hi swalky,
You should add *both* the code snippets to bp-custom.php, please try again and let me know if that works for you.
Hi @dontdream
My bp-custom.php file now contains the combined snippets:
“<?php
//
add_filter (‘bps_xprofile_format’, ‘change_format’, 10, 2);
function change_format ($default, $field_id)
{
if ($field_id == 4235) return ‘decimal’;
return $default;
}
add_action (‘bps_field_before_search_form’, ‘change_display’);
function change_display ($f)
{
if ($f->code == ‘field_4235’ && $f->display == ‘range’)
{
$f->display = ‘range-select’;
$f->options = array (” => ”) + $f->options;
}
}”unfortunately, this has not fixed the issue?
-
This reply was modified 5 years, 11 months ago by
swalky.
Did you select the search mode ‘range’ in your search form?
Can you please explain what you mean by range? I have implemented 18-99 as a search field in the drop down box.
When you create a search form in Users -> Profile Search, and you add your Age search field in the Form Fields box, you have to select range from the Search Mode drop-down.
There is no range option from what i can see?
View screenshot here
Should i change to date selector as that gives a range option then?
Oooh sorry you meant the ‘Profile Search’ page 🙂 i have changed to the range option and this has now resolved the issue.
Thansk!
Your screenshot is from Users -> Profile Fields, you should go to Users -> Profile Search instead and follow my latest post above.
Sorry I replied before seeing your latest post!
Great to know all is working now 🙂
-
This reply was modified 5 years, 11 months ago by
The topic ‘Trouble Creating Age Range’ is closed to new replies.