• Resolved Dvelop IT

    (@dvelop-it)


    Hi there,

    Is it possible to map WordPress Username field to a Mailchimp field? I can’t seem to find the option on the dropdown list.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Contributor Lap

    (@lapzor)

    UserSync is designed to sync user meta fields, so this does indeed not include the username.

    It would be possible with custom code, here is the filter you would likely use for that (untested example)

    add_filter('mc4wp_user_sync_subscriber_data', function (\MC4WP_MailChimp_Subscriber $subscriber, \WP_User $user) {
    $subscriber->merge_fields['USERNAME'] = $user->ID;
    return $subscriber;
    }, 14, 2);

    But since UserSync is only meant to keep data in sync, not to handle initial subscription, and a username in wordpress normally can’t be changed, I think the better place to send in the username is at the initial subscription moment.

    You use a MC4WP form, or any specific integration for the initial subscription? Or both? Then I can point you in the right direction to send in the username at that time, and since it will never change you won’t need to sync it with usersync after that.

    More code snippet here: https://github.com/ibericode/mailchimp-for-wordpress/blob/main/sample-code-snippets/

    Hope that helps! If you have any question please let me know.

    Kind regards, Arne

    Plugin Author Danny van Kooten

    (@dvankooten)

    Hi @dvelop-it,

    This is regarding the User Sync feature from Mailchimp for WordPress Premium, correct?

    This is a great idea and I’m not sure why we haven’t thought of supporting it before, as the dropdown currenly indeed only allows for meta keys, not properties on the user directly. While we work on adding that feature, you can get this to work by using the following code snippet.

    /**
    * This hooks into the data that is sent to Mailchimp by User Sync.
    *
    * It sends the WordPress username (user_login) to a Mailchimp field named WP_USERNAME.
    * Make sure to create an audience field with the tag WP_USERNAME in Mailchimp first.
    */

    add_filter('mc4wp_user_sync_subscriber_data', function ($subscriber, $user) {

    /** @var MC4WP_MailChimp_Subscriber $subscriber */
    /** @var WP_User $user */
    $subscriber->merge_fields['WP_USERNAME'] = $user->user_login;

    return $subscriber;
    }, 10, 2);

    https://github.com/ibericode/mailchimp-for-wordpress/blob/main/sample-code-snippets/premium/user-sync/send-wp-username.php

    I hope that helps. If not, let us know please.

    EDIT: Oops. @lapzor beat me to it. Sorry for the double message!

Viewing 2 replies - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.