Create a post variable processing page

You see a lot of code out there which includes wp-load.php or wp-blog-header.php to use the WordPress API within a php page loaded outside the context of WordPress. There’s often a better way to accomplish this. It’s also worth noting that any plugin which does that will get rejected from the WordPress.org plugin repository, with good reason- it’s prone to several sorts of failures.

Anyway, in this case, I would add a rewrite endpoint to direct requests to, so the request is processed by WordPress.

function wpd_mycred_endpoint(){
    add_rewrite_endpoint( 'mycred', EP_ROOT );
}
add_action( 'init', 'wpd_mycred_endpoint' );

As always, when rewrite rules are modified, they must be flushed to take effect. This can be done via the API, or just by visiting your Settings > Permalinks page.

Then hook the parse_request action, check if your endpoint is set, and do your processing there. Then exit script execution so nothing else is output.

function wpd_mycred_parse( $request ){
    if( array_key_exists( 'mycred', $request->query_vars ) ){
        // do your POST processing...
        die;
    }
}
add_action( 'parse_request', 'wpd_mycred_parse' );

Leave a Comment