Skip to content

Instantly share code, notes, and snippets.

  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save wpmudev-sls/659f44a63d174f3ead5062b4a2c67888 to your computer and use it in GitHub Desktop.
M2P Mailchimp Opt In Filter Depend on BuddyPress Field - Filter member email to get listed into the mailchimp
<?php
/**
* Plugin Name: M2P Mailchimp Opt In Filter Depend on BuddyPress Field
* Plugin URI: https://premium.wpmudev.org/
* Description: Block member email to get listed into the mailchimp except without their concern by BuddyPress field
* Author: Ariful Islam @ WPMUDEV
* Author URI: https://premium.wpmudev.org/profile/itsarifulislam
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'M2P_MailchimpOptInFilterWithBuddypress' ) ) {
class M2P_MailchimpOptInFilterWithBuddypress {
private static $_instance = null;
public static function get_instance() {
if ( is_null( self::$_instance ) ) {
self::$_instance = new M2P_MailchimpOptInFilterWithBuddypress();
}
return self::$_instance;
}
private function __construct() {
if ( ! class_exists('MS_Addon_Mailchimp') || ! function_exists('xprofile_get_field_data') ) return;
add_filter( 'ms_dont_subscribe_registered', array( $this, 'block_user_from_getting_opted_in' ), 10, 2 );
add_filter( 'ms_dont_subscribe_members', array( $this, 'block_user_from_getting_opted_in' ), 10, 2 );
add_filter( 'ms_dont_subscribe_deactivated', array( $this, 'block_user_from_getting_opted_in' ), 10, 2 );
}
public function block_user_from_getting_opted_in( $block_status, $member ) {
$field_id = 2; // BuddyPress field ID
$xprofile_field_value = xprofile_get_field_data( $field_id, $member->wp_user->data->ID );
// If nothing set in the field then block it by passing: true
return empty( $xprofile_field_value ) ? true : $block_status;
}
}
function Render_m2p_mailchimp_opt_in_filter_with_buddypress(){
$GLOBALS['M2P_MailchimpOptInFilterWithBuddypress'] = M2P_MailchimpOptInFilterWithBuddypress::get_instance();
}
}
add_action( 'plugins_loaded', 'Render_m2p_mailchimp_opt_in_filter_with_buddypress' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment