Я искал HTML5-совместимый и доступный плагин формы для сайта, но его там нет, поэтому я сам кодировал его (из информации, которую я нашел здесь).Это работает, но я хочу иметь возможность отправлять все поля в теле письма при отправке.Это просто отправляет сообщение из текстовой области.Я не эксперт в PHP, поэтому ответ для меня не очевиден.Я ценю любую помощь !!
<?php
function html_form_code()
{
?>
" method="post">
" />
" />
<?php
}
// Form validation
function my_validate_form()
{
$errors = new WP_Error();
if (isset($_POST[ 'url' ]) && $_POST[ 'url' ] !== '') {
$errors->add('cheater', 'Ignore this text box. It is used to detect spammers. If you enter anything into it, your message will not be sent.');
}
if (isset($_POST[ 'cf-name' ]) && $_POST[ 'cf-name' ] == '') {
$errors->add('name_error', 'Please tell us your name.');
}
if (isset($_POST[ 'cf-email' ]) && $_POST[ 'cf-email' ] == '') {
$errors->add('email_error', 'Please fill in a valid email.');
}
if (isset($_POST[ 'cf-message' ]) && $_POST[ 'cf-message' ] == '') {
$errors->add('message_error', 'Please send us a message.');
}
return $errors;
}
// Form delivery
function deliver_mail($args = array())
{
$defaults = array(
'name' => '',
'email' => '',
'phone' => '',
'to' => get_option('admin_email'), // can change this to an email address
);
$args = wp_parse_args($args, $defaults);
$headers = "From: {$args['name']} <{$args['email']}>"."\r\n";
$subject = "Website Inquiry";
// Send email returns true on success, false otherwise
if (wp_mail($args['to'], $subject, $args['message'], $headers)) {
return;
} else {
return false;
}
}
// Form Sanitize
function my_sanitize_field($input)
{
return trim(stripslashes(sanitize_text_field($input)));
}
// Success Message
function my_form_message()
{
global $errors;
if (is_wp_error($errors) && empty($errors->errors)) {
echo '';
echo 'Thank you for contacting us '.$_POST['cf-name'].', we will be in touch with you soon.';
echo '';
//Empty $_POST because we already sent email
$_POST = '';
} else {
if (is_wp_error($errors) && !empty($errors->errors)) {
$error_messages = $errors->get_error_messages();
foreach ($error_messages as $k => $message) {
echo '';
echo ''.$message.'';
echo '';
}
}
}
}
// Form shortcode
add_shortcode('gr_contact_form', 'cf_shortcode');
function cf_shortcode()
{
ob_start();
my_form_message();
html_form_code();
return ob_get_clean();
}
// Error validation
add_action('init', 'my_cf_form');
function my_cf_form()
{
if (isset($_POST['cf-submitted'])) {
global $errors;
$errors = my_validate_form();
if (empty($errors->errors)) {
$args = array(
'name' => my_sanitize_field($_POST['cf-name']),
'email' => my_sanitize_field($_POST['cf-email']),
'message' => my_sanitize_field($_POST['cf-message']),
);
deliver_mail($args);
} else {
return $errors;
}
}
}
?>