Gravity Forms is a premier forms plugin for WordPress. It is immensely popular and even more capable. This Extension allows you to use Gravity Forms form entries as a custom Source for SearchWP, making it possible to search Gravity Forms Entries with SearchWP!
Adding Gravity Forms Entries to SearchWP’s Index
Once SearchWP Source – Gravity Forms has been installed and activated, your Gravity Forms will appear in the Sources list for your SearchWP Engine.
Note: This custom Source can be used only with a Supplemental Engine.
Let’s say you were building a feature request area on your website and using Gravity Forms to collect entries.
You could utilize SearchWP to allow customers to first search existing entries before submitting their own, so they could join in up-voting the existing submission instead of creating a duplicate.
The first step would be to create a new Supplemental Engine that has our Feature Request Form as the Source:
With the form chosen (note that you can combine entries from multiple Gravity Forms into a single Engine if you’d like!) you can pick and choose which field(s) you’d like to consider for searching:
SearchWP allows you to search the entire Gravity Forms Entry as a whole, optionally consider any Notes that have been added, or choose exactly which field(s) should be considered when searching, each with its own relevancy weight!
Displaying Gravity Forms Entries as Results
The SearchWP Source – Gravity Forms Extension will make available Gravity Forms Entries as search results. This integrates as any Source would when working with a Supplemental Engine.
Unlike a native WordPress search that has The Loop which is an array of WP_Post
objects that WordPress’ API interacts with, the results set for a Supplemental Engine can be made up of any SearchWP Source object.
Gravity Forms does not have a proper object for Form Entries, each Entry is an array of key value pairs. As a convenience, SearchWP Source – Gravity Forms will return results in a wrapper object:
\SearchWP\Sources\GravityForms\Entry
This will help as you iterate over your results array:
<?php | |
$searchwp = new \SearchWP\Query( 'marketing', [ | |
'engine' => 'gravity', | |
'fields' => 'all', | |
] ); | |
foreach ( $searchwp->results as $result ) { | |
switch ( get_class( $result ) ) { | |
case 'SearchWP\Sources\GravityForms\Entry': | |
// NOTE: Gravity Forms Field values are keyed by the Field ID in the Form editor. | |
?> | |
<div class="result"> | |
Form ID: <?php echo esc_html( $result->id ); ?><br> | |
Name: <?php echo esc_html( $result->{'1'} ); ?><br> | |
Email: <?php echo esc_html( $result->{'2'} ); ?><br> | |
Type: <?php echo esc_html( $result->{'3'} ); ?><br> | |
Feature: <?php echo esc_html( $result->{'4'} ); ?> | |
</div> | |
<?php | |
break; | |
default: | |
// Another Source was added to the SearchWP Engine. | |
print_r( $result ); | |
} | |
} |
Note: You can prevent SearchWP from wrapping results in the class with the following hook:
<?php | |
add_filter( 'searchwp\source\gravity_forms\entry\raw', '__return_true' ); |
SearchWP Source – Gravity Forms allows you to make any Gravity Forms Entry searchable using SearchWP! You can make as many SearchWP Engines with as many Gravity Forms Sources as you’d like, each with their own configuration of Fields and/or Notes. Use SearchWP’s capable search algorithm to make your Gravity Forms Entries searchable!
Changelog
0.0.3
- [Fix] PHP error if form entry is omitted.
0.0.2
- [Fix] Error on activation
- [Update] Updated updater