Frontend Unsubscribe Form

Create a form that can manage unsubscribing (or resubscribing) an email address from Notification Scheduler.

Code

Filename: gpns-frontend-unsubscribe-form.php

<?php
/**
 * Gravity Perks // Notification Scheduler // Frontend Unsubscribe Form
 * https://gravitywiz.com/documentation/gravity-forms-notification-scheduler/
 *
 * Create a form that can manage unsubscribing (or resubscribing) an email address from Notification Scheduler.
 */
// Update "123" to your form ID.
add_action( 'gform_after_submission_123', function( $entry ) {
	global $wpdb;

	// Update "4" to the ID of the field in which the email to be unsubscribed or resubscribed is entered.
	$email = rgar( $entry, '4' );

	// Update "5" to the ID of the field in which the user will indicate if the field should be unsubscribed or resubscribed.
	$action = rgar( $entry, '5' );

	if ( ! $email || ! $action ) {
		return;
	}

	if ( $action === 'unsubscribe' ) {
		$wpdb->insert( $wpdb->gpns_unsubscribes, array(
			'email'         => $email,
			'scope'         => 'all',
			'timestamp_gmt' => current_time( 'mysql', true ),
		) );
	} elseif ( $action === 'resubscribe' ) {
		$wpdb->delete( $wpdb->gpns_unsubscribes, array(
			'email' => $email,
		) );
	}

} );

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.