I have this code to submit a contact form inside a wordpress page. The problem is that I'm not experienced with the WP api and I can't find a simple way to submit the form. Can anyone help me? I was using the form action
but this way don't work.
<?php get_header(); ?>
<?php
if( $_POST['send_message'] ){
$name = filter_var($_POST['name'], FILTER_SANITIZE_STRING );
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL );
$message = filter_var($_POST['message'], FILTER_SANITIZE_STRING );
$to = '[email protected]';
$subject = 'Info request from'. $name .'dal sito demo';
$headers = 'From: [email protected]' . "\r\n" .
'Reply-To: '.$email.' . "\r\n"';
mail( $to, $subject, $message, $headers );
}
?>
<!-- page cover -->
<div class="jumbotron jumbotron-fluid" style="background-image:url('<?php echo get_the_post_thumbnail_url($post->ID); ?>');" id="contact-cover-wrapper">
<div class="container" id="">
<h1 class="scroll-icon text-center animated infinite slideInDown slow"><i class="fas fa-angle-down"></i></h1>
</div>
</div>
<div class="container" id="contact-wrapper">
<div class="row">
<?php if( have_posts() ): while( have_posts() ): the_post(); ?>
<div class="col-sm-12 col-md-6 col-lg-6">
<?php the_content(); ?>
</div>
<?php endwhile; ?>
<?php endif; wp_reset_postdata(); ?>
<div class="col-sm-12 col-md-6 col-lg-6">
<from action="<?php get_permalink( $post->ID ) ?>" method="POST" id="">
<input type="text" class="form-control" name="name" placeholder="Name" required />
<br>
<input type="email" class="form-control" name="email" placeholder="Email" required />
<br>
<textarea class="form-control" rows="5" name="message" placeholder="Messagearea>
<br>
<button type="submit" name="send_message" class="btn btn-outline-warning text-uppercase"><?php _e('Send') ?></button>
</form>
</div>
</div>
</div>
<?php get_template_part('assets/contacts'); ?>
<?php get_footer(); ?>
EDIT: I was made a typo error. Instead of form
I wrote from
. Now the submit button works, I need only to test if messages are forwarded correctly. It's seems that the messages are blocked by the hosting provider, do I need to configure something in wp? Thanks for the help.
I have this code to submit a contact form inside a wordpress page. The problem is that I'm not experienced with the WP api and I can't find a simple way to submit the form. Can anyone help me? I was using the form action
but this way don't work.
<?php get_header(); ?>
<?php
if( $_POST['send_message'] ){
$name = filter_var($_POST['name'], FILTER_SANITIZE_STRING );
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL );
$message = filter_var($_POST['message'], FILTER_SANITIZE_STRING );
$to = '[email protected]';
$subject = 'Info request from'. $name .'dal sito demo';
$headers = 'From: [email protected]' . "\r\n" .
'Reply-To: '.$email.' . "\r\n"';
mail( $to, $subject, $message, $headers );
}
?>
<!-- page cover -->
<div class="jumbotron jumbotron-fluid" style="background-image:url('<?php echo get_the_post_thumbnail_url($post->ID); ?>');" id="contact-cover-wrapper">
<div class="container" id="">
<h1 class="scroll-icon text-center animated infinite slideInDown slow"><i class="fas fa-angle-down"></i></h1>
</div>
</div>
<div class="container" id="contact-wrapper">
<div class="row">
<?php if( have_posts() ): while( have_posts() ): the_post(); ?>
<div class="col-sm-12 col-md-6 col-lg-6">
<?php the_content(); ?>
</div>
<?php endwhile; ?>
<?php endif; wp_reset_postdata(); ?>
<div class="col-sm-12 col-md-6 col-lg-6">
<from action="<?php get_permalink( $post->ID ) ?>" method="POST" id="">
<input type="text" class="form-control" name="name" placeholder="Name" required />
<br>
<input type="email" class="form-control" name="email" placeholder="Email" required />
<br>
<textarea class="form-control" rows="5" name="message" placeholder="Messagearea>
<br>
<button type="submit" name="send_message" class="btn btn-outline-warning text-uppercase"><?php _e('Send') ?></button>
</form>
</div>
</div>
</div>
<?php get_template_part('assets/contacts'); ?>
<?php get_footer(); ?>
EDIT: I was made a typo error. Instead of form
I wrote from
. Now the submit button works, I need only to test if messages are forwarded correctly. It's seems that the messages are blocked by the hosting provider, do I need to configure something in wp? Thanks for the help.
If you're using WordPress, you shouldn't send POST requests directly to current URL. There is an API to process such requests.
So how should that be done?
First of all, you should send request to admin-ajax.php
:
So instead of this:
<from action="<?php get_permalink( $post->ID ) ?>" method="POST" id="">
You should have this (also, there was a typo in 'form' word):
<form action="<?php echo esc_attr( admin_url('admin-post.php') ); ?>" method="POST" id=""> // changed URL
<input type="hidden" name="action" value="my_custom_form_submit" /> // action, because WP needs that
Then you have to register your actions that will process this form:
function my_custom_form_submit_process_callback() {
$name = filter_var($_POST['name'], FILTER_SANITIZE_STRING );
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL );
$message = filter_var($_POST['message'], FILTER_SANITIZE_STRING );
$to = '[email protected]';
$subject = 'Info request from'. $name .'dal sito demo';
$headers = 'From: [email protected]' . "\r\n" .
'Reply-To: '.$email.' . "\r\n"';
mail( $to, $subject, $message, $headers ); // <- you should use wp_mail instead
wp_redirect( '<URL>' ); // <- replace with proper url that should be visible after sending the form;
die;
}
add_action( 'admin_post_nopriv_my_custom_form_submit', 'my_custom_form_submit_process_callback' ); // for anonymous users
add_action( 'admin_post_my_custom_form_submit', 'my_custom_form_submit_process_callback' ); // for logged in users