Enabling Comments and Terms in ElasticPress 5.0

by

Starting from version 5.0, ElasticPress will no longer show the Comments or Terms Features in the Dashboard by default. Site owners will need to add a code snippet in functions.php or in a custom plugin to enable one or both of these features. This change was made partly due to a lack of overall adoption of these Features, in order to reduce the number of items displayed in the ElasticPress Features submenu.

In addition, the Terms Feature often caused confusion, as it is not necessary for querying Terms within WordPress posts or Custom Post Types. Instead, this Feature provides a wrapper for WP_Term_Query and requires custom code to be written for most queries that utilize it. As a result, we have moved the Feature into a more developer-centric model.

Please note that both Features will remain active on your site, if they were active prior to upgrading to 5.0.

In this article, we will explain how to add a code snippet to WordPress to make the Comments or Terms Feature visible in ElasticPress 5.0+.

Step 1: Create a Custom Plugin or Add Code to functions.php

The first step is to create a custom plugin to hold the code snippet that will activate the Comments or Terms Feature in ElasticPress. Navigate to your WordPress plugins directory (wp-content/plugins) and create a new folder with a unique name. Inside the new folder, create a new file and name it something descriptive like “elasticpress-comments.php”. You can also add the snippet to your theme’s functions.php file or a custom plugin where you add various 3rd-party snippets.

For more ways to add custom code to your website, check this support article.

Step 2: Add the Code Snippet

Open the new file you created in Step 1 (or your functions.php file) and add the code snippet that will activate the Comments or Terms Feature in ElasticPress.

PHP
add_filter(
	'ep_feature_is_visible',
	function ( $is_visible, $feature_slug ) {
		return 'comments' === $feature_slug ? true : $is_visible;
	},
	10,
	2
);

The previous example adds the Comments Feature to the ElasticPress Features page. You can swap the word comments for terms in this example to add Terms instead. Make sure to save the file after adding the code snippet.

Step 3: Upload the Plugin

Now that you have created the custom plugin and added the code snippet, it’s time to upload it to your WordPress site. You can do this by adding the plugin to your version control system or by uploading it via the WordPress Plugin upload feature in your site’s Dashboard. Once the plugin is uploaded, activate the plugin to make the Comments and/or Terms Feature(s) visible in ElasticPress. If you edited your functions.php file instead, skip this step.

Using WP-CLI

Alternatively, if you have access to WP-CLI you can enable the features directly, by calling wp elasticpress activate-feature terms.

That’s it! Once you return to the ElasticPress Features submenu in your WordPress site’s Dashboard, you’ll see the Features you activated available for use.