Feature API
Since ElasticPress 3.0, to register a feature you will need to extend the Feature class. Here is a code example:
PHP
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* Your feature class.
*/
class My_ElasticPress_Feature extends \ElasticPress\Feature {
/**
* Initialize feature settings.
*/
public function __construct() {
$this->slug = 'feature_slug';
$this->title = esc_html__( 'Feature Name', 'plugin-text-domain' );
$this->summary = esc_html__( 'Your feature description.', 'plugin-text-domain' );
$this->requires_install_reindex = true;
$this->default_settings = [
'my_feature_setting' => '',
];
parent::__construct();
}
/**
* Setup your feature functionality.
* Use this method to hook your feature functionality to ElasticPress or WordPress.
*/
public function setup() {
add_filter( 'ep_post_sync_args', [ $this, 'method_example' ] );
}
/**
* Set the `settings_schema` attribute
*/
public function set_settings_schema() {
$this->settings_schema = [
[
'default' => '',
'help' => __( '<p>Your custom field description</p>', 'elasticpress-labs' ),
'key' => 'my_feature_setting',
'label' => __( 'Your custom field', 'plugin-text-domain' ),
'type' => 'textarea',
],
];
}
}
The Feature class is an abstract class, which means it is required to implement at least the setup() method.
Optionally, you can implement some other methods, like requirements_status()
to change the feature availability based on custom checks.
Don’t forget to register your new feature:
PHP
function load_my_elasticpress_feature() {
if ( class_exists( '\ElasticPress\Features' ) ) {
// Include your class file.
require 'class-my-elasticpress-feature.php';
// Register your feature in ElasticPress.
\ElasticPress\Features::factory()->register_feature(
new My_ElasticPress_Feature()
);
}
}
add_action( 'plugins_loaded', 'load_my_elasticpress_feature', 11 );
If you build an open-source custom feature, let us know! We’d be happy to list the feature within ElasticPress documentation.