Skip to content

Instantly share code, notes, and snippets.

@rafaehlers
Created May 9, 2025 23:29
Show Gist options
  • Save rafaehlers/f82541869e4ec326d9ca071ae7f6d89c to your computer and use it in GitHub Desktop.
Save rafaehlers/f82541869e4ec326d9ca071ae7f6d89c to your computer and use it in GitHub Desktop.
Adds an entry note with information about the user who moved the entry to the trash.
<?php // DO NOT COPY THIS LINE
/**
* Add a note whenever a Gravity Forms entry is moved to the Trash.
*
* Hook: gform_update_status — fires when an entry’s status changes
* (e.g., active → trash, spam → active).
*/
add_action( 'gform_update_status', 'gk_note_on_trash', 10, 3 );
function gk_note_on_trash( $entry_id, $new_status, $old_status ) {
// Only act on transitions INTO the trash.
if ( $new_status !== 'trash' || $old_status === 'trash' ) {
return;
}
$entry = GFAPI::get_entry( $entry_id );
if ( is_wp_error( $entry ) ) {
// Stop gracefully if the entry cannot be fetched.
return;
}
$current_user = wp_get_current_user();
$user_id = ( $current_user && $current_user->exists() ) ? $current_user->ID : 0;
$user_name = ( $current_user && $current_user->exists() ) ? $current_user->display_name : __( 'System', 'gk' );
$note_text = sprintf(
/* translators: 1: user display name, 2: user ID */
__( 'Entry moved to Trash by %1$s (user ID %2$d).', 'gk' ),
$user_name,
$user_id
);
// Record the note on the entry.
GFAPI::add_note(
$entry_id, // Entry ID.
$user_id, // User ID (0 = System/anonymous).
$user_name, // User display name.
$note_text, // Note content.
'system', // note_type.
'trash' // sub_type (custom tag to keep things clear).
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment