Integrations / Platforms / WordPress / Copying Configuration to Production

Copying Configuration to Production

If you’re managing relevance in the Algolia dashboard and you have a staging environment, you may want to copy your index configuration to a production index once you’re ready to go live. To do so, you can create a custom command and call it during your deployment process.

You can also copy settings manually from the dashboard.

Creating the copy command

In your algolia-custom-integration/wp-cli.php file, add the following copy_config method to the Algolia_Command class.

In this case, you’re not passing the index names through the algolia_index_name filter, because the point of the command is to copy across existing indices, not creating new ones.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public function copy_config($args, $assoc_args) {
    global $algolia;

    $srcIndexName = $assoc_args['from'];
    $destIndexName = $assoc_args['to'];

    if (!$srcIndexName || !$destIndexName) {
        throw new InvalidArgumentException('--from and --to arguments are required');
    }

    $scope = [];

    if (isset($assoc_args['settings']) && $assoc_args['settings']) {
        $scope[] = 'settings';
    }
    if (isset($assoc_args['synonyms']) && $assoc_args['synonyms']) {
        $scope[] = 'synonyms';
    }
    if (isset($assoc_args['rules']) && $assoc_args['rules']) {
        $scope[] = 'rules';
    }

    if (!empty($scope)) {
        $algolia->copyIndex($srcIndexName, $destIndexName, ['scope' => $scope]);
        WP_CLI::success('Copied '.implode(', ', $scope)." from $srcIndexName to $destIndexName");
    } else {
        WP_CLI::warning('Nothing to copy, use --settings, --synonyms or --rules.');
    }
}

This command lets you pass the source and destination indices, and choose what to copy by specifying the --settings, --rules, and --synonyms flags.

1
wp algolia copy_config --from=staging_posts --to=prod_posts --settings --rules --synonyms
Did you find this page helpful?