0

I’m WP newbie!

I wrote a binary xyz (in C, which can be executed on the server WP is installed), which transforms a .pdf file into a .txt file (after some transformations were done).

I wrote a post explaining details about this binary and how to use it. For demonstration purpose, I would like to include an upload button at the bottom of the post (or inside the post text), which allows the reader to select a .pdf file. After the upload, the binary xyz should run, and return a .txt file which was stored by xyz inside PROGDIR.

How can I include such an upload button? Is there a plugin which allows this (I didn’t find anything)?

Basically, the button should do ...

  1. The user presses the upload button to upload the .pdf
  2. The binary xyz saves the result in a .txt file in PROGDIR
  3. This .txt file should be offered to save on the local computer

"Similar Questions" suggestions of this site didn't help.

1 Answer 1

0

It looks like you want a few things from a single button press.

  1. Initiate request
  2. Process request
  3. Prompt user

WP AJAX will let you utilize jQuery to send a request to admin ajax (action) and trigger your process using shell_exec. When complete you'll compose the response with wp_send_json_success() and read the result back on your main page. If the process has completed, you can prompt the user at that point to download the file with the information you provided in your AJAX response.

You can also utilize custom endpoints with the REST API instead of admin-ajax.php which will handle encoding on the return value.

add_action( 'rest_api_init', function () {

    register_rest_route( 'pdf/v1', '/process/', array(
        'methods' => 'GET',
        'callback' => 'process_pdf',
    ) );
} );

How you prompt the user is up to you.

If you went with admin_post_(action) you could do must of the above and automatically initiate the download with a header response from PHP.

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.