0

I am creating a wordpress plugin that takes form inputs and stores them as wordpress options. However, I receive the error Fatal error: Uncaught Error: Call to undefined function update_option() in C:\xampp\htdocs\wordpress\wp-content\plugins\maxipoo-inventory\form-processing.php:6 Stack trace: #0 {main} thrown in C:\xampp\htdocs\wordpress\wp-content\plugins\maxipoo-inventory\form-processing.phpon line**6 **when I hit the submit button of my form.

Here is form-processing.php:

<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $consumer_key = $_POST['consumer-key'];
    $consumer_secret = $_POST['consumer-secret'];

    update_option("mp_consumer_key", $consumer_key);
    update_option("mp_consumer_secret", $consumer_secret);
}

Here is my form if it matters:

                    <form id="api-key-form" method="post" action="<?php echo plugins_url('form-processing.php', dirname(__FILE__)); ?>">
                    <input type="hidden" name="action" value="api_key_form">
                    <div class="api-key-form-input">
                      <label for="consumer-key" class="bold">Consumer Key:</label>
                        <input type="text" id="consumer-key" name="consumer-key" required><br>
                    </div>
                    <div class="api-key-form-input">
                      <label for="consumer-secret" class="bold">Consumer Secret:</label>
                        <input class="api-form-text-input" type="text" id="consumer-secret" name="consumer-secret" required>
                    </div>
                    <div id="api-key-form-submit-wrap">
                    <input type="submit" value="Save" id="api-key-form-submit-btn">
                    </div>
                    </form>

Interestingly, I originally had the standard if (!defined('ABSPATH')) { exit; } at the beginning of form-processing.php, but my if statement wouldn't run unless it was removed (I removed it and replaced the update_option() with an echo and my text was indeed echoed). This almost certainly means that ABSPATH isn't defined in this context. If ABSPATH isn't defined, then it makes sense why the update_function() isn't defined either; I'm not in a wordpress environment for some reason. However the url in the error I provided clearly shows that I'm hosting wordpress using XAMPP.

maxipoo
  • 1
  • 1

1 Answers1

0

The way you handle the form submission is incorrect. At least, if you want to use wordpress functions. All wordpress functions are available only when you load root/index.php. So you should send the form to index.php or require the index.php file in your file.

Then, in your theme or plugin (in functions.php for example) you should check whether form fields are present, using isset($_REQUEST['my form field').

Another way to handle form submissions in Wordpress is via its ajax method. You would need to write some javascript and php code. See this link:

Wordpress codex page on ajax

Klaassiek
  • 2,795
  • 1
  • 23
  • 41