Skip Navigation

[Waiting for user feedback] Parsing Radio Field Values in Custom Code

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.

This topic contains 1 reply, has 1 voice.

Last updated by Christopher Amirian 1 hour, 22 minutes ago.

Assisted by: Christopher Amirian.

Author
Posts
#2868089

Tell us what you are trying to do?

I am trying to write custom code to select a set of users to send a notification email to when a form is submitted, using a radio field selection to determine who should get the email.

Is there any documentation that you are following?

No, I've searched around but cannot find specific guidance. Here are the details:

In the form, the field is set like this:

[cred_field field="agency" force_type="field" class="form-control" output="bootstrap"]

...and outputs this HTML:

<pre>
<label for="cred_form_59725_1_1_agency_NPS" class="visuallyhidden">Agency</label>
<div class="js-wpt-field-items js-wpt-repetitive wpt-repetitive" data-initial-conditional="" data-item_name="radios-wpcf-agency">
<ul class="wpt-form-set wpt-form-set-radios wpt-form-set-radios-wpcf-agency">

<li class="wpt-form-item wpt-form-item-radio radio-nps radio"><label class="wpt-form-label wpt-form-checkbox-label" for="cred_form_59725_1_1_agency_NPS">

<input type="radio" id="cred_form_59725_1_1_agency_NPS" name="wpcf-agency" value="NPS" data-wpt-validate="{"required":{"args":{"1":true},"message":"This field is required."}}" data-wpt-field-title="Agency" class="wpt-form-radio form-radio radio js-wpt-validate" checked="checked" data-wpt-type="radio" data-wpt-id="cred_form_59725_1_1_cred_form_59725_1_1_agency_NPS" data-wpt-name="wpcf-agency">NPS</label>

<li class="wpt-form-item wpt-form-item-radio radio-usfs radio"><label class="wpt-form-label wpt-form-checkbox-label" for="cred_form_59725_1_1_agency_USFS">

<input type="radio" id="cred_form_59725_1_1_agency_USFS" name="wpcf-agency" value="USFS" data-wpt-validate="{"required":{"args":{"1":true},"message":"This field is required."}}" data-wpt-field-title="Agency" class="wpt-form-radio form-radio radio js-wpt-validate" data-wpt-type="radio" data-wpt-id="cred_form_59725_1_1_cred_form_59725_1_1_agency_USFS" data-wpt-name="wpcf-agency">USFS</label>

<li class="wpt-form-item wpt-form-item-radio radio-blm-moab radio"><label class="wpt-form-label wpt-form-checkbox-label" for="cred_form_59725_1_1_agency_BLM Moab">

<input type="radio" id="cred_form_59725_1_1_agency_BLM Moab" name="wpcf-agency" value="BLM Moab" data-wpt-validate="{"required":{"args":{"1":true},"message":"This field is required."}}" data-wpt-field-title="Agency" class="wpt-form-radio form-radio radio js-wpt-validate" data-wpt-type="radio" data-wpt-id="cred_form_59725_1_1_cred_form_59725_1_1_agency_BLM Moab" data-wpt-name="wpcf-agency">BLM Moab</label>

<li class="wpt-form-item wpt-form-item-radio radio-blm-monticello radio"><label class="wpt-form-label wpt-form-checkbox-label" for="cred_form_59725_1_1_agency_BLM Monticello">

<input type="radio" id="cred_form_59725_1_1_agency_BLM Monticello" name="wpcf-agency" value="BLM Monticello" data-wpt-validate="{"required":{"args":{"1":true},"message":"This field is required."}}" data-wpt-field-title="Agency" class="wpt-form-radio form-radio radio js-wpt-validate" data-wpt-type="radio" data-wpt-id="cred_form_59725_1_1_cred_form_59725_1_1_agency_BLM Monticello" data-wpt-name="wpcf-agency">BLM Monticello</label>

</div>
</pre>

My custom code currently looks like this:

<pre>
add_filter('cred_notification_recipients', 'add_recipients11', 10, 4);
function add_recipients11($recipients, $notification, $form_id, $post_id) {
if ( $form_id == 59725 ) {

// determine role to send email to based on request's agency (communicated from form in hidden field name: wpcf-agency)

$user_role = $_POST['wpcf-agency'];
switch ($user_role) {
case 'BLM Moab':
$role == 'blm_coordinator';
break;
case 'BLM Monticello':
$role == 'blm_coordinator';
break;
case 'NPS':
$role == 'nps_coordinator';
break;
case 'USFS':
$role == 'usfs_coordinator';
break;
default:
$role == 'administrator';
}

$blogusers = get_users( 'orderby=user_id&role='.$role);
foreach ( $blogusers as $user ) {

// modifiy the $recipients array to include the user email
$recipients[] = array (
'to' => 'to',
'address' => $user->user_email,
'name' => '',
'lastname' => ''
);
}
}
return $recipients;
}
</pre>

It's not working, and I'm thinking that the problem lies in the fact that the radio is an array, and I'm probably not getting the value I need with $_POST['wpcf-agency'];

Any thoughts?

#2868233

Christopher Amirian
Supporter

Languages: English (English )

Hi,

Welcome to Toolset support. A radio field is single-value, not an array — only checkboxes and select fields store values as arrays. So $_POST['wpcf-agency'] returns a plain string like NPS or BLM Moab, exactly as the HTML shows (name="wpcf-agency", one value per input).

The actual bug is in the switch block. Every case uses == instead of =:

$role == 'blm_coordinator';   // comparison, discards result

That's a comparison, not an assignment. $role is never set, so get_users() runs with an empty role and returns nothing to loop over — no recipients are added. Change every == to =.

There's a second thing worth fixing. Rather than reading $_POST, take the value from the post that was just saved, since agency is a Types custom field and $post_id is passed to the filter — the documentation's own examples use get_post_meta( $post_id, 'wpcf-my-date-field', true ) to read a Types field inside these notification hooks. That's more reliable than depending on the raw request array.

Something like this:

add_filter('cred_notification_recipients', 'add_recipients11', 10, 4);
function add_recipients11($recipients, $notification, $form_id, $post_id) {

    if ( $form_id == 59725 ) {

        // Read the saved Types field value for this post
        $user_role = get_post_meta( $post_id, 'wpcf-agency', true );

        switch ( $user_role ) {
            case 'BLM Moab':
            case 'BLM Monticello':
                $role = 'blm_coordinator';
                break;
            case 'NPS':
                $role = 'nps_coordinator';
                break;
            case 'USFS':
                $role = 'usfs_coordinator';
                break;
            default:
                $role = 'administrator';
        }

        $blogusers = get_users( 'orderby=user_id&role=' . $role );

        foreach ( $blogusers as $user ) {
            $recipients[] = array(
                'to'       => 'to',
                'address'  => $user->user_email,
                'name'     => '',
                'lastname' => ''
            );
        }
    }

    return $recipients;
}

Two things to verify if it still doesn't fire after this:

- Confirm the role slugs (blm_coordinator, nps_coordinator, usfs_coordinator) match the actual registered role slugs on the site, not the display names. A slug mismatch makes get_users() return an empty set, with the same silent result as the bug above.

- Confirm the notification itself is set to run on the event you expect. The filter only modifies recipients of a notification that is already being sent — it doesn't trigger one.

For further reading:

https://toolset.com/documentation/programmer-reference/cred-api/#cred_notification_recipients

https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate

Please consider that this is a question about custom code. We usually do our best to help you to find a solution, but the code given is not tested, and we do not guarantee a working solution as custom code is outside of our support scope.

Thanks.