0

I´m new in developing Plugins for wordpress.

I need a formular where all viewers of the website can fill in data and send it. The send it-button should save it to database.

So I think the way is to define the shortcut and the Formular in the plugin like this:

function formular() {
  return 'This is html of Formular';
}
add_shortcode("formular", "formular");

But then I don´t now how to define the Formular with a button to save the data into database. A Database-Table where the data should be saved is already added.

Somebody has got an idea?

0

You can handle that with sending the form to the script admin-post.php with that in the form :

<form method="POST" action="<?php echo admin_url("admin-post.php");?>?action=myPlugin__save_datas">

after that, you use this hook for the PHP treatment

add_action("admin_init", function () {

    if (    !isset($_POST["action"])
        ||  ("myPlugin__save_datas" !== $_POST["action"])
    ) {
        return;
    }


    // save datas
    // ...


    // redirection

    $url = home_url("/");

    wp_redirect($url);
    exit();

});

If you want just send form on connected or unconnected user, you have respectively the actions admin_post_myPlugin__save_datas and admin_post_nopriv_myPlugin__save_datas

  • Wouldn't admin_init never get triggered for visitors that aren't logged in? Any particular reason for using admin-post.php instead of the regular ajax? – janh Nov 25 '17 at 13:29
  • Hi, I tried your code but there is an error. The error message is "syntax error, unexpected 'function'" – Alexander Menger Nov 25 '17 at 18:18

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.