The gform_post_subscription_started action in Gravity Forms is a handy feature that’s activated right after a new subscription gets created. This offers an opportunity to perform additional tasks post-subscription.
Usage
To use this action, you simply need to bind it to a custom function using add_action. Below is a template:
add_action( 'gform_post_subscription_started', 'your_custom_function', 10, 2 );
function your_custom_function( $entry, $subscription ) {
// your custom code here
return $entry;
}
Parameters
$entry(Entry Object): This is the object representing the entry.$subscription(Array): Contains details about the subscription action. It includes:- ‘type’
- ‘amount’
- ‘transaction_type’
- ‘transaction_id’
- ‘subscription_id’
- ‘entry_id’
- ‘payment_status’
- ‘note’
More information
You can learn more about the gform_post_subscription_started action in the Gravity Forms Docs. This action hook is located in GFPaymentAddOn::start_subscription() in includes/addon/class-gf-payment-addon.php.
Examples
Log subscription details
Record the details of the subscription in a log file.
add_action( 'gform_post_subscription_started', 'log_subscription_details', 10, 2 );
function log_subscription_details( $entry, $subscription ) {
// Log the subscription details
error_log( print_r( $subscription, true ) );
return $entry;
}
Send an email
Send a welcome email to the user once the subscription starts.
add_action( 'gform_post_subscription_started', 'send_welcome_email', 10, 2 );
function send_welcome_email( $entry, $subscription ) {
// Get the user's email from the entry data
$user_email = $entry['3']; // Assuming field 3 is the user's email
// Set email subject and body
$subject = 'Welcome to our service!';
$body = 'Thanks for subscribing! We hope you enjoy our service.';
// Send the email
wp_mail( $user_email, $subject, $body );
return $entry;
}
Update a custom field in the user’s profile
Store the subscription ID in a custom field in the user’s profile.
add_action( 'gform_post_subscription_started', 'update_user_profile', 10, 2 );
function update_user_profile( $entry, $subscription ) {
// Get the user's ID from the entry data
$user_id = $entry['created_by'];
// Store the subscription ID in a custom field
update_user_meta( $user_id, 'subscription_id', $subscription['subscription_id'] );
return $entry;
}
Create a post
Create a new WordPress post every time a subscription starts.
add_action( 'gform_post_subscription_started', 'create_post_on_subscription', 10, 2 );
function create_post_on_subscription( $entry, $subscription ) {
// Set up the post data
$post_data = array(
'post_title' => 'New subscription: ' . $subscription['subscription_id'],
'post_content' => 'A new subscription has started.',
'post_status' => 'publish',
'post_author' => $entry['created_by'],
''post_type' => 'post',
);
// Create the post
wp_insert_post( $post_data );
return $entry;
}
Add a note to the entry
Add a note to the entry data when a new subscription starts.
add_action( 'gform_post_subscription_started', 'add_note_to_entry', 10, 2 );
function add_note_to_entry( $entry, $subscription ) {
// Define the note text
$note_text = 'Subscription started with ID: ' . $subscription['subscription_id'];
// Add the note to the entry
gform_add_note( $entry['id'], $note_text, 'admin', false );
return $entry;
}