1

I need to create a blank PHP page that processes a POST request. Basically, a site will send me a few post variables. I need to use these variables with the mycred plugin. Here is my code:

require_once("../../../../wp-load.php");
if (isset($_POST['reward_amount'])) {

$toaward = $_POST['reward_amount'];
    $user = $_POST['sid'];

    if (!function_exists(mycred_add))
            echo "Failed to add!";
    if (mycred_add('Points_for_______________', 1, 1, '%plural% for ------ completion', $_POST['oid'] )) {
            echo "Success!";
            //header("HTTP/1.1 200 OK");
    } else {
            echo "Fail!";
            //header("HTTP/1.1 412 Precondition Failed");
    }
}

This works, but the site I am using does not allow HTML to be returned in the HTTP response. How do I remove the HTML code that is included from wp-load.php but still keep the Wordpress function support so I can use wordpress functions? I also tried including wp-blog-header.php as well, but to no avail.

2

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' );
  • It worked just like my code. I added that code to thee functions.php, and then told the website to send the HTTP POST to /mycred/hello, but it still returned all the HTML code. – ashraj98 Jul 16 '14 at 20:18
  • 1
    what do you mean by "all the HTML code"? if you visit the page within the browser and you see anything other than what you echo within the function, you either haven't flushed rewrites, or you haven't correctly exited script execution. – Milo Jul 16 '14 at 20:41
  • I added the code to my functions.php, then flushed via Settings > Permalinks like you said. When that didn't work I added the flush_rewrite_rules(); to the code. That didn't work either, it still returns the Wordpress header – ashraj98 Jul 17 '14 at 15:20
  • Never mind. It works, I just mistyped something. Thanks! – ashraj98 Jul 17 '14 at 15:40
0

You can create a page template and then create a page using that template with url ie http://mydomain.com/reward-amount. Inside the page template you can put your code without the require_once.

EDIT

Just read Milo answer. Is far way better than mine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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