By default, submitting a form will cause all the fields in the form to be written to their tables.
If you want something simple to happen when the form is submitted (but before it is processed or written to the database), you can write PHP code in the text area at the bottom of the form (only visible in the editor). You can use the field names from your form as variables (for example, $firstname) — they will already be populated.
This code will execute at the server, so any WordPress functions you might want to use will be available to you.
Filters #
If you want finer-grained control, there are three points at which you can intervene in the form data processing using filters:
pz_pre_form_processing — you receive the full $_POST array as a parameter
pz_pre_record_save — sends an array of all fields in the person table and, if needed, an array of all fields in the interactivity table.
pz_filter_redirectURL — your function receives the redirect URL saved for this block (if any). Return the URL you want used.
Actions #
There’s also one action you can hook:
pz_data_written — sends you the id of the record just written
Custom Form Handlers #
One approach to taking control of what happens when a form is submitted is simply writing your own function.
To do this, you’ll need to write the function itself, plus you’ll need to register the function to handle your specific function.
You can place your registration and function code pretty much anywhere you’ve got PHP code running, but we recommend placing the code either in your functions.php file or (a somewhat more versatile choice), you can create a small plugin for the purpose.
Here’s an example of a complete plugin that uppercases all the string data submitted with the form, then hands that data off to the internal PZ form handling function (though you don’t have to do this). then dumps it to the screen.
<?php
/**
* Plugin Name: pzExample
* Description: A plugin that provides a custom handler function for a form.
* Requires at least: 6.1
* Requires PHP: 7.0
* Version: 0.1.0
* Author: Robert Richardson
* License: GPL-2.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: peakzebra
*
* @package PeakZebra
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
// let's register the function, first for calls made from accounts with admin privileges,
// then once again for everyone else who might submit the form
// The registration is based on the name you used as the unique id for the form when you
// configured the relevant form block.
// let's assume you gave the form 'mywork' for the form's unique id. You'd hook the relevant
// actions like this:
add_action('admin_post_do-mywork', 'do_mywork');
add_action('admin_post_nopriv_do-mywork', 'do_mywork');
// now let's create the function that is called when the form is submitted
function do_mywork( ) {
// all the fields submitted as part of the form will be found in the $_POST variable, which
// is a global array.
// before doing anything, though, let's check to see if a bot filled in a form we set as a trap
// botcheck
if( $_POST['name'] != '' ) {
wp_redirect( '/');
}
// and now we'll just dump the input from the form to the screen
// but you could perform whatever work you needed at this point in the form.
var_dump( $_POST );
exit;
// we exited, as is usual following a var_dump, but ordinarily, you'd redirect either
// to the next page the use needs to see, or else to an error page. The URLs for both
// possibilities are stored as configurations of the form, and they are included in the
// $_POST array
if( $terrible_mishap ) {
if( isset( $_POST['errorURL'])) {
wp_redirect( $_POST['errorURL']);
}
} else {
if( isset( $_POST['redirectURL'])) {
wp_redirect( $_POST['redirectURL']);
}
exit; // it's good practice to exit after a redirect
}
}Remember that you also have to have given the form block a unique name that matches up with the function name, along with checking the box saying you’re using a custom handler:
