Skip to main content

Sync a verified email change to your CRM

Free Beginner

Keep an external list in step with a subscriber’s email address, using benecaster_email_changed.

Signature: benecaster_email_changed( int $user_id, string $old_email, string $new_email ). Free, never licence-gated.

It fires on the VERIFIED change only, and that is the reason to use it rather than watching profile_update. A subscriber who edits the Email address field on their account page changes nothing yet: the new address gets a single-use link, and wp_users.user_email is untouched until that link is used. A listener that fired on the request would push whatever the subscriber mistyped to your CRM and then have to undo it.

You get the old address as well as the new one, so you can update a record keyed on the old address rather than creating a duplicate.

Nothing else in the plugin writes the address this way, so a change arriving through wp-admin or through wp_update_user() does not fire it. If you need every route, hook WordPress’s own profile_update too and treat this as the subscriber-initiated case.

Code

<?php
add_action(
    'benecaster_email_changed',
    function ( int $user_id, string $old_email, string $new_email ): void {
        // Cheap: the hook fires inside the verification request, which is a
        // browser redirect the subscriber is waiting on.
        wp_schedule_single_event(
            time() + 10,
            'my_addon_sync_contact',
            [ $user_id, $old_email, $new_email ]
        );
    },
    10,
    3
);

View on GitHub →

Hooks Used

Need this built rather than just documented? See our services →