Using Gravity Forms ‘gform_polls_results_ajax_response’ PHP filter

The gform_polls_results_ajax_response filter allows you to override the Ajax response for Gravity Forms Polls.

Usage

To apply the filter to all forms:

add_filter('gform_polls_results_ajax_response', 'your_function_name', 10, 2);

To target a specific form, append the form ID to the hook name (format: gform_polls_results_ajax_response_FORMID):

add_filter('gform_polls_results_ajax_response_1', 'your_function_name', 10, 2);

Parameters

  • $response (array): An associative array containing the properties to be returned in the Ajax response. The properties available in the array are as follows:
    • canVote (bool): Indicates if the user is allowed to vote.
    • resultsUI (string): The results HTML, confirmation, or an empty string.
  • $form (Form Object): The current form.

More information

See Gravity Forms Docs: gform_polls_results_ajax_response

Examples

Change the color of the text for the first question

This example changes the color of the text for the first question to red.

add_filter('gform_polls_results_ajax_response', 'change_result', 10, 2);

function change_result($response, $form) {
    // Change the color of the text of the first question
    $response['resultsUI'] = str_replace('<div class=\'gpoll_field_label\'>', '<div class=\'gpoll_field_label\' style=\'color:red\'>', $response['resultsUI']);
    return $response;
}

Hide results for non-logged-in users

This example hides the poll results for users who are not logged in.

add_filter('gform_polls_results_ajax_response', 'hide_results_for_guests', 10, 2);

function hide_results_for_guests($response, $form) {
    if (!is_user_logged_in()) {
        $response['resultsUI'] = 'You must be logged in to view the results.';
    }
    return $response;
}

Add custom text after poll results

This example adds custom text after the poll results.

add_filter('gform_polls_results_ajax_response', 'add_custom_text', 10, 2);

function add_custom_text($response, $form) {
    $custom_text = '<p>Thank you for participating!</p>';
    $response['resultsUI'] .= $custom_text;
    return $response;
}

Display custom message when user can’t vote

This example displays a custom message when the user is not allowed to vote.

add_filter('gform_polls_results_ajax_response', 'custom_cant_vote_message', 10, 2);

function custom_cant_vote_message($response, $form) {
    if (!$response['canVote']) {
        $response['resultsUI'] = 'Sorry, you are not allowed to vote in this poll.';
    }
    return $response;
}