Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save JarrydLong/6f8e2c2a61f56eb0e5472fcfc347fa90 to your computer and use it in GitHub Desktop.
Save JarrydLong/6f8e2c2a61f56eb0e5472fcfc347fa90 to your computer and use it in GitHub Desktop.
<?php //do not copy
/**
* This recipe will prefix the Mailchimp merge tags that get sent through.
* Each site that is connected to the same MC account that might have the same subscribers should then have this recipe on it.
* You can choose between a hardcoded prefix or one that takes the first 3 letters of the site name.
*
* You can add this recipe to your site by creating a custom plugin
* or using the Code Snippets plugin available for free in the WordPress repository.
* Read this companion article for step-by-step directions on either method.
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
/**
* Adds the prefix to the default fields that we add for MC
*/
function mypmpro_subscribe_merge_fields( $fields, $list_id ) {
//Use a prefix that you prefer
// $site_prefix = 'SWS_';
//Generate one dynamically based on site name
$site_prefix = strtoupper( substr( get_bloginfo(), 0, 3 ) ).'_';
$fields = array_combine(
array_map( function($k){ return $site_prefix.$k; }, array_keys( $fields ) ),
$fields
);
return $fields;
}
add_filter( 'pmpro_mailchimp_merge_fields', 'mypmpro_subscribe_merge_fields', 10, 2 );
/**
* Adds the prefix for any custom merge tags that might have been added
* @param [type] $fields [description]
* @param [type] $user [description]
* @param [type] $list [description]
* @return [type] [description]
*/
function mypmpro_subscribe_fields_prefix( $fields, $user, $list ) {
//Use a prefix that you prefer
// $site_prefix = 'SWS_';
//Generate one dynamically based on site name
$site_prefix = strtoupper( substr( get_bloginfo(), 0, 3 ) ).'_';
$fields = array_combine(
array_map( function($k){ return $site_prefix.$k; }, array_keys( $fields ) ),
$fields
);
return $fields;
}
add_filter( 'pmpro_mailchimp_listsubscribe_fields', 'mypmpro_subscribe_fields_prefix', 99, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment