Filter uncanny-automator

uap_wp_polls_token_WPPOLLANSWER

Filters the poll answer token for wp-polls to allow modification of the generated token.

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

Description

Fires after processing the answers for a WP-Polls poll token. Developers can modify the generated answer string to customize its format or content before it's displayed. Passes the poll ID, answers, and trigger context for fine-grained control.


Usage

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

Parameters

$value (mixed)
This parameter represents 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.
$answers (mixed)
This parameter contains the ID of the poll for which the answers are being retrieved.
$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 the poll action.
$trigger_log_id (mixed)
This parameter contains metadata related to the trigger that initiated the poll action.

Return Value

The filtered value.


Examples

add_filter( 'uap_wp_polls_token_WPPOLLANSWER', 'my_custom_wppolls_answer_filter', 10, 6 );

/**
 * Custom filter to modify the output of WP-Polls answers for UAP.
 * This example appends a " - Voted!" suffix to each answer if the poll has been voted on.
 *
 * @param string $value        The default formatted string of poll answers.
 * @param int    $poll_id      The ID of the current poll.
 * @param array  $answers      An array of answer objects for the poll.
 * @param string $trigger_id   The ID of the trigger that fired this token.
 * @param array  $trigger_meta Meta data associated with the trigger.
 * @param int    $trigger_log_id The ID of the trigger log entry.
 *
 * @return string The modified string of poll answers.
 */
function my_custom_wppolls_answer_filter( $value, $poll_id, $answers, $trigger_id, $trigger_meta, $trigger_log_id ) {

	// Check if the poll has been voted on by the current user or under specific conditions.
	// In a real scenario, you'd likely check $trigger_meta or interact with UAP's
	// user data/voting logs to determine if voting has occurred.
	// For this example, we'll simulate this by checking if $trigger_id is 'user_voted'.
	$has_voted = false;
	if ( ! empty( $trigger_id ) && $trigger_id === 'user_voted' ) {
		$has_voted = true;
	}

	if ( $has_voted && ! empty( $answers ) ) {
		$modified_answers = array();
		foreach ( $answers as $answer ) {
			// Append a suffix to each answer if the poll has been voted on.
			$modified_answers[] = $answer->polla_answers . ' - Voted!';
		}
		// Re-implode the answers with the new format.
		$value = implode( "rn", $modified_answers );
	}

	// Always return the value, whether modified or not.
	return $value;
}

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:144

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

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

				return '';
			}

Scroll to Top