define hook on form post without action

Here is a sample page with a form:
sample here
If you enable dev tools on the browser, you will see that, when the capcha is correct, a POST request is sent to the page, with the data from the form, but without any action set.
As I need to send an email to the user who filled the form, I have defined an action hook in the 'init' event, where I check if POST data is present, and then sends an email.
It works fine but I am not quite satisfied from a performance perspective, since this hook is performed on every page loads, even for page with no form.
I have tried to use hook on admin_post_nopriv, as mentioned here, but it does not work.
Here is my code:

function kps_send_email_on_post() {
	if ( isset(  $_POST['et_pb_contact_nom_1'] ) )
	{
        ... code to build and send email
	}
}
add_action( 'init', 'kps_send_email_on_post' );
add_action( 'admin_post_nopriv', 'kps_send_email_on_post' );

First add_action works fine, not the second one. Why?

  • Panos

    Hi Luce Tancil ,

    Couldn't see the form in the link yo provided, but I suspect that the form's action may have been not set to point to admin-post.php in order to use the admin_post and admin_post_nopriv hooks.

    admin_post_nopriv is used for logged out users and admin_post for logged in.

    You can check out the following snippet. It uses a shortcode to print the form in any page and it simply prints the content of the $_POST in your debug.log file (provided that you have enabled WP_DEBUG and WP_DEBUG_LOG).

    function wpmudev_custom_form_sh( $atts ){
    
    	$return_url = 'http' . (isset($_SERVER['HTTPS']) ? 's' : '') . '://' . "{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
    	ob_start();
    
    	?>
    	<div id="_wpmu_custom_frm_content">
    	    <form action="<?php echo esc_url( admin_url('admin-post.php') ); ?>" method="post">
    	        <label for="fullname">Full Name</label>
    	        <input type="text" name="fullname" id="fullname" required />
    	        <label for="email">Email Address</label>
    	        <input type="email" name="email" id="email" required />
    	        <label for="message">Your Message</label>
    	        <textarea name="message" id="message"></textarea>
    	        <input type="hidden" name="action" value="contact_form" />
    	        <input type="submit" value="Send" />
    	        <input type="hidden" name="return_url" value="<?php echo $return_url; ?>" />
    	    </form>
    	</div>
    	<?php
    	$frm = ob_get_clean();
    
    	return $frm;
    }
    
    add_shortcode( 'wpmudev_custom_form', 'wpmudev_custom_form_sh' );
    
    function wpmudev_custom_form_handle(){
    
    	error_log( print_r( $_POST,true ) );
    
    	if( isset( $_POST['return_url'] ) )
    	wp_redirect(  esc_url_raw( $_POST['return_url'] ) );
    
    }
    
    add_action( 'admin_post_nopriv_contact_form', 'wpmudev_custom_form_handle' );
    add_action( 'admin_post_contact_form', 'wpmudev_custom_form_handle' );

    Please let me know if you need any further explanation in the code above :slight_smile:

    Thanks!
    Panos

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.