2

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');
3
  • If you are in WordPress and you don't want to go out of WordPress, do it as you are doing; maybe registering your own endpoint to parse the request in the rewrite engine or in the REST API. If you send the form directly to a external PHP file, you go out of WordPress; this can be fine, too, you choose depending on your exact needs. Not sure If this comment can be an answer ....
    – cybmeta
    Commented Jan 10, 2017 at 8:50
  • Thanks, that at least confirms I'm on the right track in terms of how to do it within WordPress. I'd like to keep my referenced file within the plugin but I suppose I could also turn it into an ajax call to avoid hitting index.php with the browser...
    – Eckstein
    Commented Jan 10, 2017 at 8:56
  • @cybmeta, please make it an answer otherwise there will not be any other Commented Jan 10, 2017 at 15:05

2 Answers 2

1

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!!!";
    }
}
3
  • 4
    easier for whom and for what? with your code you are likely to get some html as well as a response, so at least a die() is required. In general, there are other way to do things, but if you need ajax, just use the ajax API Commented Jan 10, 2017 at 15:37
  • you are right @MarkKaplun. i should not use echo. it should redirect or exit after processing the request. Commented Jan 10, 2017 at 16:38
  • This is actually what I ended up doing because I didn't want to use ajax for this part of the ui. But I hooked into 'template_redirect' instead of 'init' so I could use wp_redirect without a headers warning elsewhere in my code.
    – Eckstein
    Commented Jan 12, 2017 at 4:06
3

WordPress has a general POST/GET request endpoint, using the admin_post_(action) to hook your code to an action, similar to how WordPress handles AJAX requests. Use admin_url( 'admin-post.php' ) to output the URL.

3
  • Aha, this looks promising. I will try this method and be back to choose an answer. Thanks.
    – Eckstein
    Commented Jan 10, 2017 at 18:09
  • Upvote for remember that admin-post API exists. But I must say it is a horrible API for frontend, even worst that the admin-ajax API (in my opinion, of course). I know very few people using it on frontend, but it exists and can be used.
    – cybmeta
    Commented Jan 11, 2017 at 8:15
  • According to the documentation (and code) admin_post_$action can only be used by logged in user - so it will not fire on the public frontend for anonymous access. There's the equivalent admin_post_nopriv_{$action}, but then to support both anonymous and logged in users - you'd have to register to both. It does seem to me that this mechanism is supposed to be used to implement action on the control panel and not as a general "post forms to plugin" mechanism.
    – Guss
    Commented Dec 11, 2022 at 17:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.