Ahad abasi rad
Ahad abasi rad

Reputation: 117

How to get value from user in wordpress custom plugin?

I have created a plugin for wordpress that show a form to users by shortcode, i want submit the form ,get the values and store in defined table that created when my plugin is activated.
Now i don't know where this form should be submit. I define a submenu for plugin in admin panel (and set form action to this submenu slug) to get that value and store in db, but only those how logged in can submit that form.

Upvotes: 1

Views: 1180

Answers (1)

Sabbir Ahmed
Sabbir Ahmed

Reputation: 141

You can handle any form submission using template_redirect hook if your form is in the frontend. If your form in the backend then you can use admin_init hook

Say, Your form code looks like in front end

<form method="post">
    <input type="text" name="input_1"/>
    <input type="number" name="input_2"/>
    <?php wp_nonce_field( 'name_of_your_nonce_action', 'name_of_your_nonce_field' ) ?>
    <input type="submit" name="submit_form" value="Submit" />
</form>

Now in theme functions.php file, you can handle this form like

<?php

add_action( 'template_redirect', 'wp1213_handle_custom_form', 11 );

function wp1213_handle_custom_form() {
    if( ! isset( $_POST['submit_form'] ) ) {
        return;
    }

    if( ! wp_verify_nonce( $_POST['name_of_your_nonce_field'], 'name_of_your_nonce_action' ) ) {
        return;
    }

    // Then you can handle all post data ($_POST) and save those data in db
    .......
}

Upvotes: 2

Related Questions