0

I want to make an API request using a simple wp plugin I created by submitting a form and the form will submit to action.php

<?php 

  /** * Plugin Name: Taofeeq Wodpress Plugin demo 
   * Plugin URI: #
   * Description: Display content using a shortcode to insert in a page or post 
   * Version: 0.1 
   * Text Domain: tbare-wordpress-plugin-demo
   * Author: Taofeeq Tajudeen 
   * Author URI: #
   */

function tt_wordpress_plugin_demo($atts) { 

?>
    <form id="ct" method="POST">
        <label for="source">Source:</label>
        <input type="txt" name="source" required>
        <label for="target">Target:</label>
        <input type="text" name="target" id= "trg" required>
        <input type="submit" value="Submit">
    </form>
  <?php
}

add_shortcode('tt-plugin-demo', 'tt_wordpress_plugin_demo');

action.php

`<?php 

if (isset($_post['submit'])){
    $source = $_post['source'];
    $target = $_post['target'];

    $curl = curl_init();

    curl_setopt_array($curl, array(
    CURLOPT_URL => 'https://api.apyhub.com/data/convert/currency',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS =>'{
       "source":$source,
       "target":$target
}',
 
   CURLOPT_HTTPHEADER => array(
   'Content-Type: application/json',
   'apy-token: my_api_token'
     ),
  ));

  $response = curl_exec($curl);
  curl_close($curl);
  return $response;
}

What I need assistance with is submitting the form without reloading the page where I insert the form shortcode. Also how to submit it to action.php since the file is in a plugin? Maybe you guys can assist me in a way that instead of submitting to action.php, it will submit to the same page where i will insert the action.php codes and action.php will no longer be needed.

I tried adding everything on the same page and after submission, the page will reload and return nothing. I tried submitting it to action.php, it says page not found. I tried submitting it to

action="<?php echo esc_attr(admin_url( 'admin-post.php' ) ); ?>"

but it brought me to the admin login page and after logging in it displays nothing.

Lastly, I don't know how to submit without reloading the page.

Michael M.
  • 10,486
  • 9
  • 18
  • 34
  • Your page is reloading because you're submitting the form. Since the action is not explicitly set, it defaults to the URL where the form is located. If you don't want the page to reload, you would need do something like - replace your `input type="submit"` with a `button type="button"`. Then, attach a click event handler to that button, and use an AJAX call to pass the data to your backend PHP cURL logic. One example can be found [here](https://wpmudev.com/blog/using-ajax-with-wordpress/). – FiddlingAway Feb 06 '23 at 21:18
  • Thanks but the code in the link you provided is a little bit confusing. Can you please help me with the Ajax call? – Taofeeq Tajudeen Feb 07 '23 at 09:45
  • Take a look at [this](https://stackoverflow.com/questions/17855846/using-ajax-in-a-wordpress-plugin), perhaps it will make things clearer. – FiddlingAway Feb 07 '23 at 20:14

0 Answers0