Plugin development: how to create a form and get custom data?

The details of processing form data is specific to PHP therefore I will not go into it. The WordPress-specific part is that you want your code to be run on the ‘init’ event in order to have access to WordPress functions and globals (such as the users permissions).

<?php add_action('init', function(){
    $reservation_capacity = sanitize_text_field( $_POST['reservation_capacity'] );
    update_user_meta( get_current_user_id(), 'reservation_capacity', $reservation_capacity );
}); ?>

This is just an example where you save the form value to the users meta database (that you probably wouldn’t use in real life).
sanitize_text_field is one of many WP specific functions to sanitize input values. get_current_user_id becomes available after init if a user is logged-in.

More on the topic:

Leave a Comment

tech