---
title: How to programmatically sync WP Ultimo domains to your email server
description: "Use the following code snippets to sync domains to your [email server](https://wpemailmanager.com/documentation/settings/general-settings/). To sync to a different server, please refer to this [article](https://wpemailmanager.com/documentation/code-snippets/how-to-programmatically-sync-domains-to-a-server-different-from-your-email-server/). Add this code in a must use plugin, of which you can learn to create [here](https://www.itsupportguides.com/knowledge-base/wordpress/wordpress-how-to-create-a-must-use-plugin/). If…"
url: "https://wpemailmanager.com/docs/how-to-programmatically-sync-domains-to-your-email-server/"
updated: "2023-04-29"
category: Developers / Code Snippets
---

# How to programmatically sync WP Ultimo domains to your email server

Use the following code snippets to sync domains to your [email server](https://wpemailmanager.com/documentation/settings/general-settings/). To sync to a different server, please refer to this [article](https://wpemailmanager.com/documentation/code-snippets/how-to-programmatically-sync-domains-to-a-server-different-from-your-email-server/).

Add this code in a must use plugin, of which you can learn to create [here](https://www.itsupportguides.com/knowledge-base/wordpress/wordpress-how-to-create-a-must-use-plugin/). If you have installed the [Domain Manager addon](https://wpemailmanager.com/downloads/domain-manager/), using these code snippets may result to unexpected results.

Ensure that:

1. You are using WP Email Manager V 1.0.6.4 +.
2. You have enabled [WP Ultimo integration](https://wpemailmanager.com/documentation/settings/general-settings/) in the general settings section.

NOTE: If a particular subsite has the [Custom Configuration Addon](https://wpemailmanager.com/downloads/custom-server-configuration/) enabled, the domain syncing will be associated with the subsite's email server and **not** the network's email server.

## Domain Syncing With WP Ultimo V2.0+

### Adding Domains

The following code snippet adds a domain to the email server when it is added via WP Ultimo's domain mapping functionality in WP Ultimo V2.0+.

```
/**
 * Adds a domain to the server when it is added via WP Ultimo
 *
 * @param string $domain The domain name being mapped.
 * @param int    $site_id ID of the site that is receiving that mapping.
 */
add_action('wu_add_domain', 'wpem_add_wp_ultimo_v2_domain', 10, 2);
function wpem_add_wp_ultimo_v2_domain($domain, $site_id)
{
	// Get the current email server api integration
	$api = wp_email_manager_get_api();

	// Check if WP Ultimo integration is active
	$wp_ultimo_is_active = \WP_Email_Manager\Integrations\WP_Ultimo\Ultimo::get_instance()->is_active;

	if(false == $wp_ultimo_is_active || null == $api)
	{
		return;
	}

	// Add domain to the server
	$api->add_domain($domain);
}
```

### Deleting Domains

The following code snippet deletes a domain from the email server, including its associated email accounts when it is deleted via WP Ultimo's domain mapping functionality in WP Ultimo V2.0+.

```
/**
 * Deletes the domain and associated email accounts from the server when custom domain is removed from WP Ultimo V2.0+
 *
 * @param $deleted bool
 * @param $domain \WP_Ultimo\Models\Domain
 */
add_action('wu_domain_post_delete', 'wpem_delete_wp_ultimo_v2_domain', 10, 2);
function wpem_delete_wp_ultimo_v2_domain($deleted, $domain)
{
	// Get the current email server api integration
	$api = wp_email_manager_get_api();

	// Check if WP Ultimo integration is active
	$wp_ultimo_is_active = \WP_Email_Manager\Integrations\WP_Ultimo\Ultimo::get_instance()->is_active;

	if(false == $wp_ultimo_is_active || null == $api)
	{
		return;
	}

	if(true == $deleted)
	{
		// Delete associated email accounts
		wp_email_manager_delete_accounts_by_domain($domain->get_domain());

		// Delete the domain from the server
		$api->delete_domain($domain->get_domain());
	}
}
```

## Domain Syncing With WP Ultimo V1.0+

### Adding Domains

The following code snippet adds a domain to the email server when it is added via WP Ultimo's domain mapping functionality in WP Ultimo V1.0+.

```
/**
 * Adds a domain to the server when it is added via WP Ultimo V1.0
 *
 * @param \Mercator\Mapping $mapping
 *
 */
add_action('mercator.mapping.created', 'wpem_add_wp_ultimo_v1_domain', 10, 1);
function wpem_add_wp_ultimo_v1_domain($mapping)
{
	// Get the current email server api integration
	$api = wp_email_manager_get_api();

	// Check if WP Ultimo integration is active
	$wp_ultimo_is_active = \WP_Email_Manager\Integrations\WP_Ultimo\Ultimo::get_instance()->is_active;

	if(false == $wp_ultimo_is_active || null == $api)
	{
		return;
	}

	// Add domain to the server
	$api->add_domain($mapping->get_domain());
}
```

### Deleting Domains

The following code snippet deletes a domain from the email server, including its associated email accounts when it is deleted via WP Ultimo's domain mapping functionality in WP Ultimo V1.0+.

```
/**
 * Delete a domain from the server when it removed added via WP Ultimo V1.0
 *
 * @param \Mercator\Mapping $mapping
 *
 */
add_action('mercator.mapping.deleted', 'wpem_delete_wp_ultimo_v1_domain', 10, 1);
function wpem_delete_wp_ultimo_v1_domain($mapping)
{
	// Get the current email server api integration
	$api = wp_email_manager_get_api();

	// Check if WP Ultimo integration is active
	$wp_ultimo_is_active = \WP_Email_Manager\Integrations\WP_Ultimo\Ultimo::get_instance()->is_active;

	if(false == $wp_ultimo_is_active || null == $api)
	{
		return;
	}

    // Delete associated email accounts
	wp_email_manager_delete_accounts_by_domain($mapping->get_domain());

	// Delete the domain from the server
	$api->delete_domain($mapping->get_domain());
}
```
