GravityView Merge Tag modifiers
GravityView Merge Tag Modifiers
GravityView heavily uses Gravity Forms Merge Tags, in addition to adding new ones.
To enhance Merge Tags, GravityView adds additional “modifiers” that allow you to format Merge Tags differently.
GravityView Modifiers #
String formatting
:wpautopย ย – Converts line breaks into HTML paragraphs; runswpautop()ย ย on the output:wptexturizeย ย – Applies WordPress “texturize” formatting (curly quotes, em-dashes, etc.); runswptexturize()ย ย on the output:esc_htmlย ย – Makes field output safe to use in HTML attributes; runsesc_html()ย ย on the output:sanitize_html_classย ย – Makes output safe for use as CSS class names; runssanitize_html_class()ย ย on the output:sanitize_titleย ย – Creates URL-friendly slugs; runssanitize_title()ย ย on the output:urlencodeย ย – URL-encodes the output for use in query strings; runsurlencode()ย ย on the output:rawurlencodeย – URL-encodes the output per RFC 3986, encoding spaces as%20; runsrawurlencode()ย on the output:maxwords:{number}ย ย – Limits output to{number}ย ย of words
Case transformation
:strtolowerย ย – Converts the entire output to lowercase:strtoupperย ย – Converts the entire output to uppercase:ucfirstย ย – Capitalizes the first character of the output:ucwordsย ย – Capitalizes the first character of each word
Name formatting
:initialsย ย – Converts a full name to initials (e.g., “John Doe” becomes “JD”)
Date and time formatting
:timestampย ย – Converts a date field value to a Unix timestamp:humanย ย – Displays dates and times in a human-readable relative format (e.g., “2 hours ago”, “3 days ago”):format:{format}ย ย – Formats date and time field merge tags using WordPress date format strings
Multi-value fields
:explodeย ย – Converts JSON or CSV values into a space-separated string (useful for Multiple Select fields)
Note: These modifiers will not be processed on {all_fields} ย ย Merge Tags.
Usage: :esc_html ย ย Modifier #
The :esc_html ย ย modifier runs the output of the field through the esc_html() ย ย function in WordPress.
Before, you weren’t able to use Merge Tags when generating HTML. This modifier makes field output safe for HTML, and allows you to use Merge Tags inside HTML attributes safely:
{Business Name:2}ย ย isThe "World's Best" Astronaut Ice Creamย
Before:
Without the Merge Tag modifier, you would use this code:
<a href="{Website:3}" title="Go to the {Business Name:2} website">{Business Name:2}</a>And the output would be broken HTML:
<a href="{Website:3}" title="Go to the The "World's Best" Astronaut Ice Cream website">The "World's Best" Astronaut Ice Cream</a>Notice the extra double quotes inside the HTML tag? That is broken HTML.
After:
Let’s update the example to use the :esc_html ย ย modifier:
<a href="{Website:3}" title="Go to the {Business Name:2:esc_html} website">{Business Name:2:esc_html}</a>The output is now valid HTML, with the quotes converted to HTML entities: #
<a href="{Website:3}" title="Go to the The "World's Best" Astronaut Ice Cream website">The "World's Best" Astronaut Ice Cream</a>Usage: :sanitize_html_class ย ย Modifier #
This modifier is perfect for making sure the field values are valid CSS class names. Use it inside your HTML, like so:
<div class="{Category:5:sanitize_html_class}">[...]</div>The :sanitize_html_class ย ย modifier runs the field output through the gravityview_sanitize_html_class() ย ย function, which is very similar to the sanitize_html_class() ย ย WordPress function, except the WordPress function does not allow spaces (multiple CSS classes), and GravityView does.
Before:
- Merge Tag:
{Your Profession:5}ย - Value:
Scientist, Astronaut!ย
After:
- Merge Tag:
{Your Profession:5:sanitize_html_class}ย - Value:
Scientist Astronautย
Usage: :sanitize_title ย ย Modifier #
The :sanitize_title ย ย modifier runs the field output through the sanitize_title() ย ย WordPress function.
This is convenient for adding standardized attributes to HTML tags.
Before:
- Merge Tag:
{Your Profession:5}ย - Value:
Scientist, Astronaut!ย
After:
- Merge Tag:
{Your Profession:5:sanitize_title}ย - Value:
scientist-astronautย
Usage: :urlencode ย ย Modifier #
The :urlencode ย ย modifier URL-encodes the output, making it safe to use in query strings.
Before:
- Merge Tag:
{Search Query:7}ย - Value:
rocket science & space travelย
After:
- Merge Tag:
{Search Query:7:urlencode}ย - Value:
rocket+science+%26+space+travelย
This is useful when building URLs dynamically:
https://example.com/search?q={Search Query:7:urlencode}Usage: :rawurlencode ย Modifier #
The :rawurlencode ย modifier URL-encodes the output according to RFC 3986. Unlike :urlencode , which encodes spaces as + , :rawurlencode ย encodes spaces as %20 . This makes it more suitable for encoding URL path segments and safer for use with email links.
This modifier runs the rawurlencode() function on the Merge Tag’s content.
Before:
- Merge Tag:
{Search Query:7} - Value:
rocket science & space travel
After:
- Merge Tag:
{Search Query:7:rawurlencode} - Value:
rocket%20science%20%26%20space%20travel
Use :rawurlencode ย when building URLs that need to be used in emails.
<a href="mailto:example@example.com?subject={My Topic:1:rawurlencode}">email us</a>Usage: :wpautop ย ย Modifier #
The :wpautop ย ย modifier changes double line breaks in the text into HTML paragraphs (<p>...</p> ย ) and single line-breaks are converted to HTML <br /> ย . Line breaks within the script and style sections are not affected.
This modifier will run the wpautop() function on the Merge Tag’s content. Learn more on WordPress.org.
Before:
- Merge Tag:
{Cosmonaut:4}ย - Output:
The Earth was small and light blue.ย
After:
- Merge Tag:
{Cosmonaut:4:wpautop}ย - Output:
<p>The Earth was small and light blue.</p>ย
Usage: :wptexturize ย ย Modifier #
The :wptexturize ย ย modifier applies WordPress “texturize” formatting, which converts straight quotes to curly quotes, double hyphens to em-dashes, and other typographic improvements.
This modifier will run the wptexturize() function on the Merge Tag’s content.
Before:
- Merge Tag:
{Quote:8}ย - Value:
She said "Hello" -- and then left...ย
After:
- Merge Tag:
{Quote:8:wptexturize}ย - Value:
She said "Hello" โ and then leftโฆย
Usage: :maxwords:{number} ย ย Modifier #
Limits the length of displayed field content to {number} ย ย words.
When the content length exceeds the number of words, “โฆ” will be appended to the text. Example: “The text is too long” becomes “The textโฆ”.
Example: Display the first 10 words of a testimonial
We have a form named “Testimonial”, where we gather customer feedback. It has a textarea field called “Quote”, and it has the field ID of “12”.
To display the content in GravityView, we normally use the {Quote:12} ย ย Merge Tag. The full quote is too long, though:
I really enjoyed the service that I received. Thank you for all your help! In the future, I will have no reservations about referring people to your business. Thanks again!
That quote has 30 words. We only want to show the first 10 words so the content fits nicely in our heading. We can use the :maxwords ย ย modifier to limit the number of words.
We can add the :maxwords ย ย modifier to the end of the Merge Tag, along with the max number of words we want: :maxwords:10 ย . The final Merge Tag looks like {Quote:12:maxwords:10} ย .
Here’s the output from the {Quote:12:maxwords:10} ย ย Merge Tag:
I really enjoyed the service that I received. Thank youโฆ
Notes
- Commas, periods, etc, connected to the final word of the value will be preserved.
- If you set
:maxwords:3ย ย on this value:Example with comma, to be trimmedย , the output would beExample with comma,โฆย
- If you set
- HTML tags are preserved. Whitespace between tags will make an HTML tag be considered a separate word:
<p> <strong>Example</strong></p>ย ย will be considered two words because there is a space between<p>ย ย and<strong>ย .<p><strong>Example</strong></p>ย ย will be considered one word.
- HTML entities are allowed. If they are separated by word breaks, they will be counted as words:
Foo & Barย ย will be considered three words because&ย ย is separate.Foo& Barย ย will be considered two words because&ย ย is connected toFooย .
- When the text is truncated, “โฆ” is appended to the output, using HTML entity
…ย .
Usage: Case Transformation Modifiers #
GravityView provides four modifiers for transforming the case of text:
:strtolower ย
Converts the entire output to lowercase.
Before:
- Merge Tag:
{Name:3}ย - Value:
JOHN DOEย
After:
- Merge Tag:
{Name:3:strtolower}ย - Value:
john doeย
:strtoupper ย
Converts the entire output to uppercase.
Before:
- Merge Tag:
{Name:3}ย - Value:
John Doeย
After:
- Merge Tag:
{Name:3:strtoupper}ย - Value:
JOHN DOEย
:ucfirst ย
Capitalizes only the first character of the output.
Before:
- Merge Tag:
{Description:4}ย - Value:
rocket scientistย
After:
- Merge Tag:
{Description:4:ucfirst}ย - Value:
Rocket scientistย
:ucwords ย
Capitalizes the first character of each word.
Before:
- Merge Tag:
{Job Title:5}ย - Value:
senior rocket scientistย
After:
- Merge Tag:
{Job Title:5:ucwords}ย - Value:
Senior Rocket Scientistย
Usage: :initials ย ย Modifier #
The :initials ย ย modifier converts a full name to initials. This is useful for displaying abbreviated names or creating avatar placeholders.
Before:
- Merge Tag:
{Name:3}ย - Value:
John Michael Doeย
After:
- Merge Tag:
{Name:3:initials}ย - Value:
JMDย
Usage: :timestamp ย ย Modifier #
The :timestamp ย ย modifier converts valid dates into a timestamp (the number of seconds since the Unix Epoch, January 1 1970 00:00:00 GMT). This is helpful for use in combination with the [gvlogic] ย ย shortcode.
This modifier will run the strtotime() function on the Merge Tag’s content. If not a valid format, the value will be returned as -1 ย .
Before:
- Merge Tag:
{Date:3}ย - Output:
07/07/2003ย
After:
- Merge Tag:
{Date:3:timestamp}ย - Output:
1057547880ย
It will work with any format for the Date field (mm/dd/yyyy ย , yyyy-dd-mm ย , and everything in between):
Before:
- Merge Tag:
{Text Field With Parseable Date:6}ย - Output:
July 14, 2015, 11:49 GMTย
After:
- Merge Tag:
{Text Field With Parseable Date:6:timestamp}ย - Output:
1436874540ย
Notes
- The
:timestampย ย modifier is designed to work with Date fields, but it will work for any field, as long as the value is formatted in a way that is parsed by PHP’s strtotime() function.
Usage: :human ย ย Modifier #
The :human ย ย modifier displays dates and times in a human-readable relative format. This is useful for showing how long ago something happened.
This modifier works with Date and Time fields.
Before:
- Merge Tag:
{Date Created:date_created}ย - Value:
2024-01-15 10:30:00ย
After:
- Merge Tag:
{Date Created:date_created:human}ย - Value:
2 weeks agoย
For Time fields:
Before:
- Merge Tag:
{Appointment Time:6}ย - Value:
14:30ย
After:
- Merge Tag:
{Appointment Time:6:human}ย - Value:
3 hours from nowย
Usage: :format:{format} ย ย Modifier #
The :format ย ย modifier allows you to format date and time fields using PHP date format strings, which are the same format strings supported by WordPress.
This modifier works with Date fields, Time fields, and date-related merge tags like {date_created} ย , {date_updated} ย , {now} ย , {yesterday} ย , and {tomorrow} ย .
Examples:
{Date:3:format:F j, Y}ย ย โJanuary 15, 2024ย{Date:3:format:m/d/Y}ย ย โ01/15/2024ย{Date:3:format:Y-m-d}ย ย โ2024-01-15ย{Time:4:format:g:i A}ย ย โ2:30 PMย{Time:4:format:H:i}ย ย โ14:30ย
Notes
- To include a colon (
:ย ) in your format string, escape it with a backslash:{Time:4:format:H:i:s}ย ย โ14:30:00ย - The
:formatย ย modifier preserves the original timezone of Date fields (no timezone offset is applied).
Usage: :explode ย ย Modifier #
The :explode ย ย modifier converts JSON arrays or comma-separated values into a space-separated string. This is particularly useful for Multiple Select fields or other fields that store multiple values.
Before (JSON array):
- Merge Tag:
{Categories:5}ย - Value:
["Science","Technology","Space"]ย
After:
- Merge Tag:
{Categories:5:explode}ย - Value:
Science Technology Spaceย
Before (CSV):
- Merge Tag:
{Tags:6}ย - Value:
rocket,satellite,launchย
After:
- Merge Tag:
{Tags:6:explode}ย - Value:
rocket satellite launchย
This is especially useful when you want to use field values as CSS classes:
<div class="{Categories:5:explode,sanitize_html_class}">[...]</div>Using Multiple Modifiers at a Time #
You can combine multiple modifiers by separating them with commas. Be sure to use GravityView’s modifiers first, then Gravity Formsโ.
For example, if you want to use :wpautop ย ย but you also want to URL-encode the output, you would write it like this: {Field:1:wpautop,urlencode} ย
More examples:
{Name:3:strtolower,sanitize_html_class}ย ย – Convert to lowercase, then make safe for CSS classes{Description:4:maxwords:20,wpautop}ย ย – Limit to 20 words, then add paragraph tags{Categories:5:explode,sanitize_html_class}ย ย – Convert array to space-separated string, then sanitize for CSS
- GravityView Modifiers
- Usage: :esc_html ย ย Modifier
- The output is now valid HTML, with the quotes converted to HTML entities:
- Usage: :sanitize_html_class ย ย Modifier
- Usage: :sanitize_title ย ย Modifier
- Usage: :urlencode ย ย Modifier
- Usage: :rawurlencode ย Modifier
- Usage: :wpautop ย ย Modifier
- Usage: :wptexturize ย ย Modifier
- Usage: :maxwords:{number} ย ย Modifier
- Usage: Case Transformation Modifiers
- Usage: :initials ย ย Modifier
- Usage: :timestamp ย ย Modifier
- Usage: :human ย ย Modifier
- Usage: :format:{format} ย ย Modifier
- Usage: :explode ย ย Modifier
- Using Multiple Modifiers at a Time