Handling form request from plugin file

In a plugin I have a payment form that needs to be submitted (via the action= attribute) to a .php file, also located in my plugin directly. After some research, it seems like the “wordpress way” to call up individual plugin files is to actually use custom queries on index.php instead. I’ve done so with the below code, and it’s working, however the code in question simply needs to process the form and then redirect the user to a confirmation page, I don’t need to display anything. Right now it seems like it’s actually loading the index.php template, which seems like a waste.

Am I going about this correctly? If not, how should I do this instead?

//Register our custom request hook
function tps_space_rental_query_vars($vars) {
$vars[] = 'tps-rent-space';
return $vars;
}
add_filter('query_vars', 'tps_space_rental_query_vars');

//This will allow us to process our payment form the wordpress way
function tps_payment_parse_request($wp) {
if (array_key_exists('tps-rent-space', $wp->query_vars) 
        && $wp->query_vars['tps-rent-space'] == 'chargeform') {

    // process the request, just testing for now
    echo 'This request happened!';
}
}
add_action('parse_request', 'tps_payment_parse_request');

2 Answers
2

the easiest way is using init action hook

add_action("init", "your_form_handler_action");
function your_form_handler_action(){
    if( isset( $_REQUEST["action"] ) && $_REQUEST["action"] == "your_action_name" ) {
        echo "Response!!!";
    }
}

Leave a Comment