(Advanced) How to update entry approval status using PHP
To change an entry’s approval status from your own code, call GravityView_Entry_Approval::update_approved(). For a new entry, call it on gform_after_submission_{form_id}. For an edited entry, call it at priority 20 on gravityview/edit_entry/after_update. A call that runs earlier can be overwritten, even though it reported success.
On a new entry, that happens when the form has no approval field, or when the call left out the form ID. On an edited entry, it happens when Unapprove Entries After Edit resets the status, which needs that setting on and someone saving who can’t moderate entries.
Set the status on a new entry #
This example approves every new entry on one form:
/**
* Approve new entries on one form once GravityView has set their starting status.
*
* Replace 123 with your form ID.
*
* @param array $entry The new entry.
* @param array $form The form.
*/
add_action( 'gform_after_submission_123', function ( $entry, $form ) {
if ( ! class_exists( 'GravityView_Entry_Approval' ) ) {
return;
}
GravityView_Entry_Approval::update_approved( $entry['id'], GravityView_Entry_Approval_Status::APPROVED, $form['id'] );
}, 10, 2 );Note: Not sure where to add this code? See Adding custom PHP code snippets to your website.
The method takes the entry ID, the status, and the form ID. The status is one of these constants on GravityView_Entry_Approval_Status:
| Constant | Value | Status |
|---|---|---|
APPROVED | 1 | Approved |
DISAPPROVED | 2 | Disapproved |
UNAPPROVED | 3 | Unapproved |
Always pass the form ID. Without it, an Approve/Reject field on the form keeps its old value, and the next edit on the Gravity Forms Entries screen changes the status to match the field.
A call that changes the status also does the following:
- Adds a note to the entry, such as “Approved the Entry for GravityView”.
- Runs the
gravityview/approve_entries/updatedaction, and the action for the new status, such asgravityview/approve_entries/approved. - Sends any notification set to those events. How to send email notifications when an entry is approved covers the setup.
To stop adding the note, use the gravityview/approve_entries/add-note filter:
add_filter( 'gravityview/approve_entries/add-note', '__return_false' );Note: Not sure where to add this code? See Adding custom PHP code snippets to your website.
Set the status after an edit #
Use priority 20 on gravityview/edit_entry/after_update, which runs after the Unapprove Entries After Edit setting has reset the status.
Warning: Because this code runs after the reset, one edit can send two sets of emails: any notification set to GravityView - Entry approval is reset, and then any set to GravityView - Entry is approved. To keep the status without those emails, stop the reset with the gravityview/approve_entries/autounapprove/status filter by returning false.
/**
* Approve an entry after it's edited through a View, once GravityView has finished with its status.
*
* Replace 123 with your form ID.
*
* @param array $form The form.
* @param string $entry_id The ID of the entry that was edited.
*/
add_action( 'gravityview/edit_entry/after_update', function ( $form, $entry_id ) {
if ( 123 === (int) $form['id'] ) {
GravityView_Entry_Approval::update_approved( $entry_id, GravityView_Entry_Approval_Status::APPROVED, $form['id'] );
}
}, 20, 2 );Note: Not sure where to add this code? See Adding custom PHP code snippets to your website.
Check that the status was saved #
The method returns false when the status isn’t valid, the entry doesn’t exist, the form ID belongs to another form, or the Approve/Reject field couldn’t be saved. Any other call returns true. That includes a call whose status failed to save, so read the status back to confirm the result:
$entry_id = 456; // The entry to update.
$form_id = 123; // The form it belongs to.
GravityView_Entry_Approval::update_approved( $entry_id, GravityView_Entry_Approval_Status::APPROVED, $form_id );
$status = GravityView_Entry_Approval::get_entry_status( $entry_id, 'value' );
if ( ! GravityView_Entry_Approval_Status::is_approved( $status ) ) {
error_log( "Entry {$entry_id} was not approved." );
}Note: Not sure where to add this code? See Adding custom PHP code snippets to your website.
Why the status doesn’t stick #
A call can return true and still leave you looking at an entry that isn’t approved. When that happens, these are the causes to check. The note “Approved the Entry for GravityView” on an entry that shows as Unapproved means the call ran and something changed the status afterward.
The call runs too early in a submission
On a form with no Approve/Reject or User Opt-In field, GravityView sets each new entry to Unapproved on gform_after_submission at priority 10. That replaces any status set before it:
| Where your code runs | Result |
|---|---|
gform_entry_created | Overwritten |
gform_entry_post_save | Overwritten |
gform_user_updated, on a User Registration update feed | Overwritten |
gform_after_submission, priority below 10 | Overwritten |
gform_after_submission, priority 10 | Depends on the load order: overwritten from a must-use plugin, kept from a theme’s functions.php |
gform_after_submission, priority 20 | Kept |
gform_after_submission_{form_id} | Kept |
A form with one of those fields takes the status from the field at that same point. An early call that passes the form ID updates the field, so its status stays. An early call without the form ID gets replaced by what the field holds.
The View unapproves the entry after an edit
With Unapprove Entries After Edit on, an Edit Entry save by someone who can’t moderate entries resets the status on gravityview/edit_entry/after_update at priority 10. That overwrites a call on gravityview/edit_entry/before_update or gform_after_update_entry, or at a lower priority on the same action. Priority 10 depends on the load order here too. Priority 20 stays.
The entry is saved again afterward
GFAPI::update_entry() saves the approval status along with the rest of the entry, so an older copy changes it:
- A copy fetched before the call writes the old status back.
- The
$entrythatgform_after_submissionpasses you has no status yet, so saving it removes the status, and an entry with no status reads as Unapproved. - A copy fetched while the entry had no status at all, such as one made with
GFAPI::add_entry(), saves a value that reads as Disapproved.
Save first and set the status afterward, or fetch the entry again with GFAPI::get_entry() after the call. GFAPI::update_entry_field() leaves the status alone.
The status is read from an older copy
An $entry array fetched before the call still holds the old status while the database holds the new one. Read it again with GFAPI::get_entry( $entry_id ), or use GravityView_Entry_Approval::get_entry_status() as shown in Retrieve Entry Approval Status in GravityView.
Writing the value directly #
GFAPI::update_entry_property() can’t set the status. It only updates columns of the entry table and the status is entry meta, so it returns false and changes nothing.
gform_update_meta() does write the value:
$entry_id = 456; // Replace 456 with your entry ID.
$form_id = 123; // Replace 123 with your form ID.
gform_update_meta( $entry_id, 'is_approved', GravityView_Entry_Approval_Status::APPROVED, $form_id );Note: Not sure where to add this code? See Adding custom PHP code snippets to your website.
This call saves the status and nothing else. It adds no note, runs none of the approval actions, and sends no notification. It also leaves any Approve/Reject field as it was, so the next edit on the Entries screen changes the status to match that field.