Skip to content

Instantly share code, notes, and snippets.

@tjhole
Last active August 29, 2015 14:05
Show Gist options
  • Save tjhole/c40b0632bce6842802ec to your computer and use it in GitHub Desktop.
Save tjhole/c40b0632bce6842802ec to your computer and use it in GitHub Desktop.
WORDPRESS: Jetpack Subscription
<?php $status = isset( $_REQUEST['subscribe'] ) ? $_REQUEST['subscribe'] : false; ?>
<?php if ( $status == 'invalid_email' ) : ?>
<p>You have entered an invalid e-mail, please try again!</p>
<?php elseif ( $status == 'success' ) : ?>
<p>Thank you for subscribing! Please check your e-mail to confirm.</p>
<?php else : ?>
<form method="POST">
<input type="hidden" name="my-form-action" value="subscribe" />
<input name="my-email" value="" placeholder="Enter your e-mail" />
<input type="submit" />
</form>
<?php endif; ?>
/* ========================================================================================================================
Jetpack Subscription Function
======================================================================================================================== */
add_action( 'init', 'process_my_subscription_form' );
function process_my_subscription_form() {
if ( isset( $_POST['my-form-action'] ) && $_POST['my-form-action'] == 'subscribe' ) {
$email = $_POST['my-email'];
$subscribe = Jetpack_Subscriptions::subscribe( $email, 0, false );
// check subscription status (to be continued)
}
}
// check subscription status
if ( is_wp_error( $subscribe ) ) {
$error = $subscribe->get_error_code();
} else {
$error = false;
foreach ( $subscribe as $response ) {
if ( is_wp_error( $response ) ) {
$error = $response->get_error_code();
break;
}
}
}
if ( $error ) {
switch( $error ) {
case 'invalid_email':
$redirect = add_query_arg( 'subscribe', 'invalid_email' );
break;
case 'active': case 'pending':
$redirect = add_query_arg( 'subscribe', 'already' );
break;
default:
$redirect = add_query_arg( 'subscribe', 'error' );
break;
}
} else {
$redirect = add_query_arg( 'subscribe', 'success' );
}
wp_safe_redirect( $redirect );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment