Filter uncanny-automator

uap_wp_polls_token_WPPOLL_ANSWERS

Filters the poll answers data before it's used or saved, allowing modification of the poll's responses.

add_filter( 'uap_wp_polls_token_WPPOLL_ANSWERS', $callback, 10, 6 );

Description

Filters the poll answers for the WPPOLL_ANSWERS token, allowing developers to modify the formatted list of answers. This hook is applied after retrieving all answers for a specific poll. Developers can use this to alter how poll answers are presented within triggers or logs.


Usage

add_filter( 'uap_wp_polls_token_WPPOLL_ANSWERS', 'your_function_name', 10, 6 );

Parameters

$value (mixed)
This parameter contains the formatted string of poll answers, with each answer on a new line.
$poll_id (mixed)
This parameter contains the formatted string of poll answers, with each answer on a new line, that will be returned by the filter.
$answers (mixed)
This parameter contains the ID of the poll for which the answers are being processed.
$trigger_id (mixed)
This parameter contains an array of answer objects associated with the poll.
$trigger_meta (mixed)
This parameter contains metadata associated with the trigger that initiated this hook.
$trigger_log_id (mixed)
This parameter contains metadata associated with the trigger that initiated the poll action.

Return Value

The filtered value.


Examples

/**
 * Filters the answers for a WP-Polls token to add a prefix to each answer.
 *
 * @param string $value       The current value of the token, which is a string of answers separated by newlines.
 * @param int    $poll_id     The ID of the poll.
 * @param array  $answers     An array of answer objects from the WP-Polls plugin.
 * @param mixed  $trigger_id  The ID of the trigger.
 * @param mixed  $trigger_meta The metadata of the trigger.
 * @param mixed  $trigger_log_id The log ID of the trigger.
 *
 * @return string The modified value of the token with a prefix added to each answer.
 */
function my_uap_wp_polls_prefix_answers( $value, $poll_id, $answers, $trigger_id, $trigger_meta, $trigger_log_id ) {
    // Ensure we have answers to process and the value is not empty
    if ( ! empty( $answers ) && ! empty( $value ) ) {
        $prefix = 'Q: '; // Define the prefix to add
        $new_value_lines = array();

        // Split the existing value into lines and add the prefix to each
        $answer_lines = explode( "rn", $value );
        foreach ( $answer_lines as $line ) {
            if ( ! empty( $line ) ) {
                $new_value_lines[] = $prefix . $line;
            }
        }

        // Rejoin the lines with the prefix
        return implode( "rn", $new_value_lines );
    }

    // If no answers or value is empty, return the original value
    return $value;
}
add_filter( 'uap_wp_polls_token_WPPOLL_ANSWERS', 'my_uap_wp_polls_prefix_answers', 10, 6 );

Placement

This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.


Source Code

src/integrations/wp-polls/tokens/wpp-tokens.php:82

if ( null !== $answers ) {
							$value = '';
							foreach ( $answers as $answer ) {
								$value .= $answer->polla_answers . "rn";
							}

							return apply_filters( 'uap_wp_polls_token_WPPOLL_ANSWERS', $value, $poll_id, $answers, $trigger_id, $trigger_meta, $trigger_log_id );
						}

						break;
					case 'WPPOLL_START':
						// Get Poll start timestamp
						$start_timestamp = $wpdb->get_var( $wpdb->prepare( "SELECT pollq_timestamp FROM $wpdb->pollsq WHERE pollq_id = %d", $poll_id ) );

Scroll to Top