Skip Navigation

[Resolved] Use wpt_field_options only in a Specific form

This support ticket is created 3 years ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 2 replies, has 2 voices.

Last updated by rafaelL-2 3 years ago.

Assisted by: Shane.

Author
Posts
#2027681

Tell us what you are trying to do?
I need translate de options of select in a Specific form and i wish know if can use wpt_field_options only in that form and dont affect others forms.

Is there any documentation that you are following?
I read the api of that command and not saw if can limit her use.

Is there a similar example that we can see?
I use that example but affect to all forms, i tried put a if saying if form X but not see that work

add_filter( 'wpt_field_options', 'tssupp_populate_user_field_a', 10, 3 );

function tssupp_populate_user_field_a( $current_options, $title_of_field,$form, $form_data ){
if ($form_data["id"]==163352)
{
if ( 'selecclabasoc' == $title_of_field ) {

$current_options = array();
//añadimos al select, usuarios que solo sean del rol laboratorios
$users = get_users(array( 'role' => 'laboratorios' ));

foreach ($users as $user) {
$current_options[] = array(
'#value' => $user->ID,
'#title' => $user->user_login,
);
}
}
}

#2027813

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Rafael,

Thank you for getting in touch. As this hook is meant to be used with Types fields there isn't any integration on it to check for a specific form ID, instead it will only modify the field that it is meant to target.

So you aren't able to apply this hook to only a specific form. If you take a look below you will see the context under which the hook is meant to be used and the parameters that it accepts.

https://toolset.com/documentation/programmer-reference/types-api-filters/#wpt_field_options

What you can do is check for the current page ID and only allow the hook to modify the form based on the current page that it is on. Example

add_filter( 'wpt_field_options', 'tssupp_populate_user_field_a', 10, 3 );

function tssupp_populate_user_field_a( $current_options, $title_of_field,$form, $form_data ){
global $post;

if ($post->ID == 163352)
{
if ( 'selecclabasoc' == $title_of_field ) {

$current_options = array();
//añadimos al select, usuarios que solo sean del rol laboratorios
$users = get_users(array( 'role' => 'laboratorios' ));

foreach ($users as $user) {
$current_options[] = array(
'#value' => $user->ID,
'#title' => $user->user_login,
);
}
}
}

Please let me know if this helps. Replace the 163352 with the ID of the current page.
Thanks,
Shane

#2029469

My issue is resolved now. Thank you! the post id dont work because only get the id of new post that going to make ,no the id of form or page where that select are working.

add_filter( 'wpt_field_options', 'tssupp_populate_user_field_a', 10, 2 );
 
function tssupp_populate_user_field_a( $current_options, $title_of_field ){
//field name
if ( 'Diagnóstico' == $title_of_field ) {
   
  //page where the form load
  if ($_SERVER['REQUEST_URI']== "/adicionar-paciente/")
{
$current_options = array();
//añadimos al select, usuarios que solo sean del rol laboratorios
$users = get_users(array( 'role' => 'laboratorios' ));
 
foreach ($users as $user) {
$current_options[] = array(
'#value' => $user->ID,
'#title' => $user->user_login,
);
}
}


 return $current_options;
}
This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.