Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save petermann/dab8bde28ba0d6529c721793d779b22f to your computer and use it in GitHub Desktop.
Save petermann/dab8bde28ba0d6529c721793d779b22f to your computer and use it in GitHub Desktop.
Automatically Enable Contact Form 7 Endpoints with Disable WP REST API Plugin
/**
* Automatically Enable Contact Form 7 Endpoints with Disable WP REST API Plugin
* Plugin URI: https://wordpress.org/plugins/disable-wp-rest-api/
*/
function disable_wp_rest_api_enable_contact_form7_endpoints() {
$active_plugins = get_option('active_plugins');
// Check if the "Disable WP REST API" plugin is active
if (in_array('disable-wp-rest-api/disable-wp-rest-api.php', $active_plugins)
// Check if the "Contact Form 7" plugin is active
&& in_array('contact-form-7/wp-contact-form-7.php', $active_plugins)
// Check if the "WPCF7_ContactForm" class exists
&& class_exists('WPCF7_ContactForm')
) {
// If all conditions are met, add a filter to enable the Contact Form 7 endpoints
add_filter('disable_wp_rest_api_server_var', function () {
// Get all Contact Form 7 forms
$forms = WPCF7_ContactForm::find();
$form_itens = [];
// Loop through all forms and add their endpoints to an array
foreach ($forms as $form) {
$form_id = $form->id();
$form_itens[] = '/wp-json/contact-form-7/v1/contact-forms/' . $form_id . '/refill';
$form_itens[] = '/wp-json/contact-form-7/v1/contact-forms/' . $form_id . '/feedback';
$form_itens[] = '/wp-json/contact-form-7/v1/contact-forms/' . $form_id . '/feedback/schema';
}
// Return the array of endpoints
return $form_itens;
});
}
}
add_action('after_setup_theme', 'disable_wp_rest_api_enable_contact_form7_endpoints');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment