Skip to content

Instantly share code, notes, and snippets.

@christianwach
Last active June 22, 2017 12:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christianwach/aab0f25b831fd914ac3fe45577c7d218 to your computer and use it in GitHub Desktop.
Save christianwach/aab0f25b831fd914ac3fe45577c7d218 to your computer and use it in GitHub Desktop.
Delay list subscription in MailChimp for WordPress when registration is moderated by BP Registration Options
<?php
/**
* Delay MailChimp subscription when registration is moderated.
*
* Only users who have requested to be signed up to the list will be passed
* to this callback, so store that fact in usermeta for later processing.
*
* @param bool $delay False delays subscription, true does not
* @param int $user_id The user ID to subscribe
* @return bool False delays subscription, otherwise passes through
*/
function my_mailchimp_subscribe_delay( $delay, $user_id ) {
// check for presence of BP Registration Options plugin on main site
if ( defined( 'BP_REGISTRATION_OPTIONS_VERSION' ) AND is_main_site() ) {
// if moderation is enabled
$moderate = get_option( 'bprwg_moderate' );
if ( $moderate ) {
// store flag in usermeta
bp_update_user_meta( $user_id, '_my_mailchimp_meta_key', 'y' );
// delay
return false;
}
}
// fall back to existing
return $delay;
}
add_filter( 'mc4wp_integration_buddypress_should_subscribe', 'my_mailchimp_subscribe_delay', 10, 2 );
/**
* Do the delayed MailChimp subscription when registered user is approved via
* the BP Registration Options plugin.
*
* @param int $user_id The user ID to subscribe
*/
function my_mailchimp_subscribe_now( $user_id ) {
// get subscription flag
$subscribe = bp_get_user_meta( $user_id, '_my_mailchimp_meta_key', true );
// bail if we don't have it
if ( $subscribe !== 'y' ) return;
// delete subscription flag
bp_delete_user_meta( $user_id, '_my_mailchimp_meta_key' );
// fire action to subscribe user to MailChimp list
do_action( 'mc4wp_integration_buddypress_subscribe_user', $user_id );
}
add_action( 'bpro_hook_approved_user', 'my_mailchimp_subscribe_now', 10, 1 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment