Skip to content

Instantly share code, notes, and snippets.

@NateWr
Last active November 6, 2018 09:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NateWr/6bc65b49d3ee5e9a75dd to your computer and use it in GitHub Desktop.
Save NateWr/6bc65b49d3ee5e9a75dd to your computer and use it in GitHub Desktop.
Prevents an error from being displayed if the user does not enter an email address.
<?php
/**
* Plugin Name: Don't Require Email Field for Restaurant Reservations
* Plugin URI: http://themeofthecrop.com
* Description: Prevents an error from being displayed if the user does not enter an email address.
* Version: 1.0
* Author: Theme of the Crop
* Author URI: http://themeofthecrop.com
* License: GNU General Public License v2.0 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
* Requires at least: 4.0
* Tested up to: 4.01
*
* Text Domain: rtbdomain
* Domain Path: /languages/
*
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU
* General Public License as published by the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU General Public License along with this program; if not, write
* to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
if ( ! defined( 'ABSPATH' ) )
exit;
/**
* Remove the required attribute for the email field
*/
if ( !function_exists( 'rtbdref_unset_email_required' ) ) {
add_filter( 'rtb_booking_form_fields', 'rtbdref_unset_email_required', 10, 3 );
function rtbdref_unset_email_required( $fields, $request, $args ) {
foreach( $fields as $fieldsetName => $fieldset) {
if (!empty($fieldset['fields'])) {
foreach ($fieldset['fields'] as $fieldName => $field) {
if ($fieldName === 'email') {
$fields[$fieldsetName]['fields']['email']['required'] = false;
}
}
}
}
return $fields;
}
} // endif
/**
* Adjust validation results
*/
if ( !function_exists( 'rtbdref_validate_email' ) ) {
add_action( 'rtb_validate_booking_submission', 'rtbdref_validate_email' );
function rtbdref_validate_email( $booking ) {
// Remove any validation errors attached to the email field
foreach( $booking->validation_errors as $key => $error ) {
if ( $error['field'] == 'email' ) {
unset( $booking->validation_errors[ $key ] );
}
}
// If no email address is provided, add your own email address so
// you can still send a notification email
if ( empty( $booking->email ) ) {
$booking->email = 'your.email@example.org';
}
}
} // endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment