Blog /

Add reCaptcha to Restaurant Reservations plugin

Snippet for adding reCaptcha to reservation form of Restaurant Reservations plugin.

If you are a fan of Restaurant Reservations plugin, you might want to secure it by Google’s reCaptcha. The plugin itself doesn’t support it However, implementation is not hard and you could make it just in two steps.

1. Install and setup Invisible reCaptcha for WordPress plugin.

2. Implement a custom form support (for our reservation). Add the following code into functions.php of your child template or use a plugin that allows you to add custom snippets such as Code Snippets. Please, do not add snippets directly into your theme template – you will lose changes in the next theme update.


<?php // don't copy this line
/**
* @snippet Add reCaptcha to Restaurant Reservations plugin
* @sourceCS https://kybernaut.cz/?p=2992
* @sourceEN https://kybernaut.cz/?p=2998
* @author Karolína Vyskočilová (https://kybernaut.cz)
* @testedwith WordPress 5.2
* @requires https://wordpress.org/plugins/invisible-recaptcha/
*/
// ——————-
// Check if both required plugins are activated.
if ( in_array('restaurant-reservations/restaurant-reservations.php', apply_filters('active_plugins', get_option('active_plugins'))) && in_array('invisible-recaptcha/invisible-recaptcha.php', apply_filters('active_plugins', get_option('active_plugins'))) ){
add_action( 'rtb_booking_form_after_fields', 'kbnt_google_invre_render_widget_action' );
add_action( 'rtb_validate_booking_submission', 'kbnt_rtb_validate_booking_submission' );
}
// Include reCaptcha field.
function kbnt_google_invre_render_widget_action() {
do_action('google_invre_render_widget_action');
}
// Valide reCaptcha field.
function kbnt_rtb_validate_booking_submission( $booking ) {
$is_valid = apply_filters('google_invre_is_valid_request_filter', true);
if ( ! $is_valid ) {
$booking->validation_errors[] = array(
'field' => 'name',
'error_msg' => 'Robot Allert',
'message' => __( 'You shall not pass, robot.' ),
);
}
}

Done. If you need help, get in touch in comments.