• Skip to primary navigation
  • Skip to main content

RocketGeek

Home of WP-Members, The Original WordPress Membership Plugin

  • WordPress Plugins
    • WP-Members
      • FAQs
      • Quick Start
      • Documentation
      • Extensions
    • Advanced Options
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Download Protect
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Invite Codes
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • MailChimp Integration
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • PayPal Subscriptions
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Salesforce Web-to-Lead
    • Security
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Text Editor
      • Purchase the Plugin
      • Get the Pro Bundle
    • User List
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • User Tracking
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Memberships for WooCommerce
    • WordPass
  • Store
    • Cart
    • Checkout
  • Blog
    • Basics [Free]
    • Tips and Tricks
    • Filters
    • Actions
    • Code Snippets
    • Shortcodes
    • Design
    • Release Announcements
  • Contact
  • Sign In
  • Show Search
Hide Search
Home » Plugins » WP-Members » Documentation » Filter and Action Hooks » wpmem_mc_merge

wpmem_mc_merge

Description

This filter hook allows you to change the MailChimp mail merge fields (if you are using any).  The default in the add-on are first name and last name from the default first and last name fields.  If you have changed those fields, or want to use different fields for other merge values, you can do it with this filter.

Usage

add_filter( 'wpmem_mc_merge', 'my_mc_merge_filter' );

function my_mc_merge_filter( $merge_vars ) {

	/*
	 * The default merge_vars, first and last name come in
	 * with the $merge_vars array.  If you are adding to
	 * the merge fields IN ADDITION TO those, you'll need
	 * to consider that as you filter the array.
	 *
	 * Here is an example where I keep the existing merge
	 * fields (first and last name) and add two additional
	 * fields to that array. Note how I obtain the values
	 * from the posted form data.
 	 */
	
	$my_new_field = array(
		'MC_MERGE_FIELD_1' => $_POST['wp-members-field-meta-key-1'],
		'MC_MERGE_FIELD_2' => $_POST['wp-members-field-meta-key-2'],
	);
	
	$my_new_merge = array_merge( $merge_vars, $my_new_field );
	
	return $my_new_merge;
}

Code Snippet Library [Subscriber Content]

  • Simple MailChimp merge field example
See a list of all filter and action hooks

Welcome to RocketGeek Interactive › Forums › Plugins › WP-Members › WP-Members Documentation › Filter and Action Hooks › wpmem_mc_merge

  • This topic has 9 replies, 5 voices, and was last updated 5 years, 10 months ago by Chad Butler.
Viewing 5 reply threads
  • Author
    Posts
    • December 14, 2012 at 4:23 pm #1693
      Chad Butler
      Keymaster

      Description This filter hook allows you to change the MailChimp mail merge fields (if you are using any).  The default in the add-on are first name an
      [See the full post at: wpmem_mc_merge]

    • June 7, 2013 at 3:36 pm #2712
      CarolineElisa
      Participant

      Hi there, trying this for the first time.

      Can you help me with the syntax for adding more than one field?

      Basically this is what I want to do:

       

      WP-members field name -> Mailchimp merge field

      empresa -> MMERGE3

      cargo -> MMERGE4

      telefone -> MMERGE5

       

      Thanks!

       

    • October 31, 2013 at 11:55 am #3290
      Chad Butler
      Keymaster

      Syntax should actually be the other way around (and use “=>” not “->”). Each couplet should be separated by a comma in the array until the last one. So it would be like this:

      'MailChimp Merge Field' => $_POST['WP-Members Field Option Name'],
      'MMERGE3' => $_POST['empressa'],
      'MMERGE4' => $_POST['cargo'],
      'MMERGE5' => $_POST['telefone']

    • May 14, 2014 at 5:37 pm #4868
      FossilFree
      Participant

      Hi… been using the simple out of the box mc_merge quite nicely. Now looking at merging more fields. But I have a situation where I have two mailchimp signup groups: one for daily emails and another for weekly emails. In wp-members, these groups show up under the pulldown “Subscribe Field” as an either or option. Is there a way to send multiple sign-up option groups info to Mail Chimp?

      Thanks in advance. Mike

      • May 14, 2014 at 10:24 pm #4870
        Chad Butler
        Keymaster

        Hi Mike – that’s an interesting question. I think you could do it with a logical test in your function – something like this:

        add_filter( 'wpmem_mc_merge', 'my_mc_merge_filter' );
        function my_mc_merge_filter( $merge_vars )
        {
        	if( $_POST['your-subscription-field'] == 'some-value' ) {
        		$my_new_field = array(
        			'MC_MERGE_FIELD_1' => $_POST['option-name-1'],
        			'MC_MERGE_FIELD_2' => $_POST['option-name-2']
        		);
        	} else {
        		$my_new_field = array(
        			'MC_MERGE_FIELD_3' => $_POST['option-name-3'],
        			'MC_MERGE_FIELD_4' => $_POST['option-name-4']
        		);
        	}
        	$my_new_merge = array_merge( $merge_vars, $my_new_field );
        	return $my_new_merge;
        }
        • May 20, 2014 at 9:01 pm #4924
          FossilFree
          Participant

          Hi Chad… thanks very much for this. I get the logic and the code. Now that I’ve spent more time with MailChimp, I can be more specific about my question. In my WP-members database and signup form, I have two checkboxes for two different email list options. As I said in my initial email, these two checkboxes show up as “Subscribe Fields” in the WP-Members-MailChimp. The code you suggested clearly shows how to test and map to which boxes are checked.

          In our MailChimp database, these two checkboxes are not actual fields with merge codes, they are “segments”. And I don’t see any segment code to send from WordPress.

          Has this situation been dealt with before? Any suggestions on how to make it work?

          Thanks again, Mike

    • March 4, 2015 at 3:28 am #7340
      tuvaho
      Participant

      We have installed WP-members with Mail chimp integration on our page.
      Using your code for “wpmem_mc_merge” we managed to get the information over to MC, except for two fields where we used your code for Multiple Selection Checkboxes.
      The checkboxes works fine on our page, but how do we set up the wpmem_mc_merg for these fields?

      Thank you for support!

      • March 6, 2015 at 5:34 pm #7349
        Chad Butler
        Keymaster

        For anyone reading this later, there was an email conversation to this as well that was not part of the forum. To fill in that gap, the mail merge field was a radio group (radio groups only allow one of several selections) and of course the multiple select checkbox group allows multiple selections.

        To discuss this, I’ll be using the the cookie group example from the multiple selection checkbox tutorial), the checkbox labels are Chocolate Chip, Oatmeal, Macaroon, and Peanut Butter. The checked values are chocolatechip, oatmeal, macaroon, and peanutbutter. For testing with the MC mail merge, I set up a field at MC “FAVCOOKIE” with the values being the same as the checkbox field labels.

        The likely reason that the merge was failing is that if you set up the merge value the same as the others directly using the $_POST value ( ‘FAVCOOKIE’ => $_POST[‘fav_cookie’] ). The posted value is an array, even if the user selects only one checkbox. But the merge field is looking for a single string to match one of the possible values. So even if the MC radio group values were the same as the checkbox values (not labels), you couldn’t directly merge the two as they are different types (one is an array, one is a string).

        One possible approach to this dilemma (and probably the easiest solution) is to make the MailChimp field a text field and then implode the checkbox selections to a comma separated string. Assuming still the cookie example, here is a merge taking the “fav_cookie” checkboxes, imploding them into a comma separated string, and passing them to merge with FAVCOOKIE which would need to be a text field at MailChimp:

        add_filter( 'wpmem_mc_merge', 'my_mc_merge_filter' );
        
        function my_mc_merge_filter( $merge_vars ) {
        
        	// get the cookie checkboxes, make them a string for merging
        	$cookie = ( isset( $_POST['fav_cookie'] ) ) ? implode( ",", $_POST['fav_cookie'] ) : '';
        	
        	// repeat for any additional checkbox groups...
        	
        	// the merge array with the $cookie value merging to FAVCOOKIE
        	// (assumes FAVCOOKIE is a text value at MailChimp)
        	// (repeat that concept for any additional checkbox groups
        	$my_new_field = array(
        		'FNAME' => $_POST['first_name'],
        		'LNAME' => $_POST['last_name'],
        		'FAVCOOKIE' => $cookie,
        	);
        	
        	$my_new_merge = array_merge( $merge_vars, $my_new_field );
        	
        	return $my_new_merge;
        }

        Using everything as-is in the example, you will end up with a comma separated string of values at MailChimp that uses the “checked value” of the checkboxes (i.e. “chocolatechip,peanutbutter”). If you want those more ‘readable’ you can make the checked value of the checkboxes like “chocolate chip”.

        Another possibility if you are going to use the radio button group at MC is to work with the checkboxes to set up something that will merge with the MC field.

        To do that has a trade-off with the previous example. You get to keep the radio group at MC, but it can only have one value. So my example looks through the checked values and the first time it gets a match, it makes that the merge value and quits checking for more.

        This example assumes that your MC radio group has the values “Chocolate Chip”, “Oatmeal”, “Macaroon”, and “Peanut Buttler”. The “case” values are the checkbox checked values used in the example snippet (same as before).

        function my_mc_merge_filter( $merge_vars ) {
        
        	$fav_cookie = $_POST['fav_cookie'];
        	
        	foreach( $fav_cookie as $cookie ) {
        		
        		$cookie_merge = '';
        		
        		switch( $cookie ) {
        			case: 'chocolatechip':
        				$cookie_merge = 'Chocolate Chip';
        				break;
        			case: 'oatmeal':
        				$cookie_merge = 'Oatmeal';
        				break;
        			case: 'macaroon':
        				$cookie_merge = 'Macaroon';
        				break;
        			case: 'peanutbutter':
        				$cookie_merge = 'Peanut Butter';
        				break;
        		}
        	
        		// if we have a value, quit the loop
        		if( $cookie_merge ) {
        			break;
        		}
        	}
        	
        	// repeat for any additional checkbox groups...
        	
        	// the merge array with the $cookie value merging to FAVCOOKIE
        	// (assumes FAVCOOKIE is a radio group at MailChimp)
        	$my_new_field = array(
        		'FNAME' => $_POST['first_name'],
        		'LNAME' => $_POST['last_name'],
        		'FAVCOOKIE' => $cookie_merge,
        	);
        	
        	$my_new_merge = array_merge( $merge_vars, $my_new_field );
        	
        	return $my_new_merge;
        }

        I know that’s a lot to digest. Hopefully it makes some sense.

    • March 6, 2015 at 2:04 pm #7346
      Bookpraiser
      Participant

      Having the same problem here.

      Checkbox updates fine on WP Page but it doesn’t update in Mailchimp.

      Any solution, suggestion?

      • March 6, 2015 at 5:37 pm #7350
        Chad Butler
        Keymaster

        @bookpraiser – if your situation is matching up a radio group, you can see the explanation here. If it is anything else, we’ll need to look at what might be failing.

        Regardless of if it is the same issue, though, you should still take a look at that explanation because are other field types – it could be a field type issue since the example given in the documentation is dealing with text fields.

  • Author
    Posts
Viewing 5 reply threads
  • You must be logged in to reply to this topic.
Log In

Ready to get started?

Join Today!

© 2021 · butlerblog.com · RocketGeek is built using WordPress, WP-Members, and the Genesis Framework

  • butlerblog.com
  • WP-Members Support Subscription
  • Terms of Service
  • Refund Policy