form action wordpress and php

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.com';

    $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

tech