gpppw_word_count

  1. Description
  2. Usage
  3. Examples
    1. Pay Per Character
    2. Split Words on Specific Characters
  4. Since

Description

Filter the calculated word count for the current field.

This filter must be used with its JS counterpart to handle filtering the word count on submission.

Usage

add_filter( 'gpppw_word_count', 'my_custom_function' );

## Parameters – **word_count** [int]() The calculated word count. – **words** [string]() The string of words to be counted. – **price_field** [\GF_Field_SingleProduct]() The Single Product field that has been configured to charge per word. – **word_field** [\GF_Field]() The field for which the number of words will be counted. – **form** [array]() The current form object.

Examples

Pay Per Character

Change Pay Per Word’s behavior to charge per character instead of per word. This snippet must be paired with its JS counterpart.

<?php
/**
 * Gravity Perks // Pay Per Word // Surprise, Pay Per Character! (PHP)
 * https://gravitywiz.com/documentation/gravity-forms-pay-per-word/
 *
 * This snippet requires the JS counterpart gpppw-pay-per-character.js
 */
add_filter( 'gpppw_word_count', function( $word_count, $words ) {
	// Pay per character instead of words.
	return mb_strlen( trim( $words ) );
}, 10, 2 );

Split Words on Specific Characters

In this example, Pay Per Word splits words on periods, underscores, and asterisks. This snippet must be paired with its JS counterpart.

<?php
/**
 * Gravity Perks // Pay Per Word // Split Words on Specific Characters (PHP)
 * https://gravitywiz.com/documentation/gravity-forms-pay-per-word/
 *
 * This snippet requires the JS counterpart gpppw-split-words-on-specific-characters.js
 */
add_filter( 'gpppw_word_count', function( $word_count, $words ) {
	// Splits words on periods, underscores and asterisks.
	$words = preg_replace( '/[\.\_\*]/', ' ', $words );
	return count( array_filter( preg_split( '/[ \n\r]+/', trim( $words ) ) ) );
}, 10, 2 );

Since

This filter is available since Gravity Forms Pay Per Word 1.1.4.