How to handle forms in a wordpress plugin?

In my Plugin I want to work with the posted form data. My form input button looks like this:

<form action="<?php  echo plugins_url('plugin_directory/my-plugin.php'); ?>"method="post">  
   <input type="submit" name="button1" class="button1" id="button1" value="button 1">
</form>

By clicking that button the plugin script will be executed again. The only thing that changed in my script is the value of a variable:

Read More
if (isset($_POST['button1'])) {
    $file=plugins_url('file_1.txt');
    var_dump('button clicked');
}
elseif(isset($_POST['button2'])){
    $file=plugins_url('file_2.txt');
}
...
else {
$file=plugins_url('file_1.txt');//default value of variable when no button is clicked    
}

But there seem to be some conflicts between wordpress and the plugin script because I got following error: “Call to undefined function add_action()”

So how to handle forms in a wordpress plugin correctly? The form action redirect is correct but the script cant be executed again. I also tried it with form action=”#” and the absolute URL path I copied from the browser. The Page will display then but without the form if statements working.

Anyone knows what to do?

Related posts

Leave a Reply

1 comment