Search code examples
wordpressformspluginssubmission

Can't get Wordpress form submission to work


I know this question has been asked repeatedly, but none of the solutions that I have found are working for me. I have a simple wordpress plugin that outputs the form html via the shortcode on the page. When I fill in the form, it is not hitting the form action callback and I can not figure out why. Here is my code:

<?php
/*
 Plugin Name: Form Submit Test
 Plugin URI: http://10.0.2.15/wp-content/plugins/form-submit-test
 Description: Testing to get these forms to submit
 Version: 1.0.0
 Author: Randy Young
 Author URI: http://example.com
 License: GPLv2 or later
*/


// security check
defined('ABSPATH') or  die('Illegal Access');
define( 'FormSubmitTest_VERSION', '1.0.0' );

class FormSubmitTest
{


    function fst_shortcode() {
        return $this->html_form_code();
    }

    private function html_form_code() {
        error_log('html_form_code()');
        $html = '';
        $html .= '<form action="' . esc_url( admin_url('admin-post.php')) . '" method="post">'; //
        $html .= '<input type="hidden" name="action" value="fst_action_hook"';
        $html .= '<p>';
        $html .= 'Your Name (required) <br />';
        $html .= '<input type="text" name="cf-name" pattern="[a-zA-Z0-9 ]+" value="' . ( isset( $_POST["cf-name"] ) ? esc_attr( $_POST["cf-name"] ) : '' ) . '" size="40" />';
        $html .= '</p>';
        $html .= '<p>';
        $html .= 'Your Email (required) <br />';
        $html .= '<input type="email" name="cf-email" value="' . ( isset( $_POST["cf-email"] ) ? esc_attr( $_POST["cf-email"] ) : '' ) . '" size="40" />';
        $html .= '</p>';
        $html .= '<p>';
        $html .= 'Subject (required) <br />';
        $html .= '<input type="text" name="cf-subject" pattern="[a-zA-Z ]+" value="' . ( isset( $_POST["cf-subject"] ) ? esc_attr( $_POST["cf-subject"] ) : '' ) . '" size="40" />';
        $html .= '</p>';
        $html .= '<p>';
        $html .= 'Your Message (required) <br />';
        $html .= '<textarea rows="10" cols="35" name="cf-message">' . ( isset( $_POST["cf-message"] ) ? esc_attr( $_POST["cf-message"] ) : '' ) . '</textarea>';
        $html .= '</p>';
        $html .= '<p><input type="submit" name="cf-submitted" value="Send"/></p>';
        $html .= '</form>';

        return $html;
    }
}


// instantiate the FormSubmitTest class
if( class_exists('FormSubmitTest')) {
    $formSubmitTest = new FormSubmitTest();
}

add_action( 'wp_post_nopriv_fst_action_hook', 'fst_action_function' ); // need this to serve non logged in users
add_action( 'wp_post_fst_action_hook', 'fst_action_function' );
// THE FUNCTION
function fst_action_function() {

    error_log('fst_action_function()');
    
    wp_redirect(admin_url('admin.php?page=10.0.2.15/index.php/ringwood2/'));
    exit;
}

// create a shortcode to add to the page
add_shortcode('form_submit_test', array( $formSubmitTest, 'fst_shortcode'));

?>

I see the error_log('html_form_code()'); result in the debug.log, but I do not see error_log('fst_action_function()'); result from the callback. I also end up on the wp-admin/admin-post.php page.

Any help would be deeply appreciated.


Solution

  • It looks like you are trying to submit the form via AJAX, and have the wrong action. For AJAX, it should be wp_ajax, not wp_post.

    add_action( 'wp_ajax__nopriv_fst_action_hook', 'fst_action_function' ); // need this to serve non logged in users
    add_action( 'wp_ajax_fst_action_hook', 'fst_action_function' );
    

    You can find more information here: wp_ajax_{action}_. This would essentially allow you to submit the form via AJAX, but, you cannot redirect in the way you are attempting. With AJAX, you would typically return a response, and then do something with that response in Javascript, such as display a message (or redirect).

    If you prefer to simply post the form without AJAX, you should instead consider using the init action. Then you can listen for the form submission. Something like,

    add_action( 'init', 'listen_for_form_submissions' );
    
    function listen_for_form_submissions() {
        if( isset( $_POST['action'] ) && $_POST['action'] === 'fst_action_hook' ) {
            error_log('fst_action_function()');
    
            wp_redirect(admin_url('admin.php?page=10.0.2.15/index.php/ringwood2/'));
            exit;
        }
    }