Approve or decline guests’ Guest portal changes from code, and change the approval emails
Every change a guest sends through their form (see Restyle the guest’s own Guest portal form, or act when a guest sends changes) waits on the Guests screen’s Waiting for Approval list; the Guests list shows N waiting for approval with Review. All three routes are admin only (manage_options through benecaster_rest_permission_admin(), with the X-WP-Nonce header), with no show: a guest is the site’s.
GET /wp-json/benecaster-guest-manager/v1/approvalsanswers{ items, total }, one item per guest with changes waiting (a deleted or trashed guest is left out):{ guest_id, name, submitted, submitted_display, changes: [{ field, label, old, new }], photo }.submittedis ISO 8601 UTC.oldandneware plain text (social links one per line,Platform: address); never draw them as HTML.photoisnull, or{ action: "replace" | "remove", old_url, new_url }, wherenew_urlis the waiting photo as adata:address (the file itself is private and never served).POST /approvals/{id}/approveapplies the guest’s changes and answers{ approved, guest, total }(guestasGET /guests/{id}gives it;totalstill waiting). It is an edit on the Guests screen: the waiting fields are cleaned again exactly asPUT, PATCH /guests/{id}cleans them (a value that no longer passes is that route’s400, naming the field), then written betweenbenecaster_guest_manager_before_save_guestandbenecaster_guest_manager_after_save_guest( $guest_id, $fields, false )(see Read, create, change or delete guests from code, as the Guests screen does), so the feed’s cache clears. Only the fields the guest changed are written: a field the podcaster edited since keeps the podcaster’s value. A new photo joins the media library now, titled Photo of {Name} and attached to no post, and becomes the guest’s photo; Remove my photo sets none (the old image stays in the library). A photo that cannot be imported is500 benecaster_guest_manager_photo_import_failed, and nothing is applied. The guest is emailed.POST /approvals/{id}/declinethrows the changes and any waiting photo away and answers{ declined, total }. The guest is emailed.- Both answer
404 benecaster_guest_manager_not_foundfor no such guest and404 benecaster_guest_manager_nothing_pendingwhen nothing is waiting.
The hooks:
benecaster_guest_manager_before_approve_guest_changes( int $guest_id, array $pending )andbenecaster_guest_manager_after_approve_guest_changes( int $guest_id, array $fields ), the fields written (photo_idincluded when the photo changed).benecaster_guest_manager_before_decline_guest_changes( int $guest_id, array $pending )andbenecaster_guest_manager_after_decline_guest_changes( int $guest_id ).$pendingis{ fields, photo, submitted }.
The emails are plain text, from the site’s title, and never quote what the guest sent.
- To the admin on each submission:
benecaster_guest_manager_approval_email_recipient( string $to, int $guest_id ),admin_emailby default,''sends nothing;benecaster_guest_manager_approval_email_subject( string $subject, int $guest_id );benecaster_guest_manager_approval_email_message( string $message, int $guest_id, string $queue_url ). - To the guest on approve or decline, only when they have an email:
benecaster_guest_manager_outcome_email_subject( string $subject, int $guest_id, bool $approved )andbenecaster_guest_manager_outcome_email_message( string $message, int $guest_id, bool $approved ).
Code
<?php
// Send submissions to the producer instead of the site's admin.
add_filter( 'benecaster_guest_manager_approval_email_recipient', static fn(): string => 'producer@example.com' );
// Approve every waiting change from code, e.g. a trusted guest list in a cron job.
$waiting = rest_do_request( new WP_REST_Request( 'GET', '/benecaster-guest-manager/v1/approvals' ) )->get_data();
foreach ( $waiting['items'] as $item ) {
rest_do_request( new WP_REST_Request( 'POST', '/benecaster-guest-manager/v1/approvals/' . $item['guest_id'] . '/approve' ) );
}
Hooks Used
-
benecaster_guest_manager_before_approve_guest_changes -
benecaster_guest_manager_after_approve_guest_changes -
benecaster_guest_manager_before_decline_guest_changes -
benecaster_guest_manager_after_decline_guest_changes -
benecaster_guest_manager_approval_email_recipient -
benecaster_guest_manager_approval_email_subject -
benecaster_guest_manager_approval_email_message -
benecaster_guest_manager_outcome_email_subject -
benecaster_guest_manager_outcome_email_message -
benecaster_guest_manager_before_save_guest -
benecaster_guest_manager_after_save_guest
Need this built rather than just documented? See our services →