Here are two examples of how you can use the Greyd Form Actions.
Excecute code before saving an entry
PHP
function greyd_forms_action_before_entry_example( $form_data, $form_id ) {
$form = get_post( $form_id );
if ( $form && $form->post_name === 'my-contact-form' ) {
$option_value = get_option( 'my_custom_option' );
do_action( 'greyd_forms_log', $option_value, 'Custom option value' );
if ( $option_value === 'prevent_submission' ) {
do_action( 'greyd_forms_error', 'The form submission was prevented due to a custom option value.' );
}
}
}
add_action( 'greyd_forms_action_before_entry', 'greyd_forms_action_before_entry_example', 10, 2 );Execute code after saving an entry
PHP
function greyd_forms_action_after_entry_example( $form_data, $form_id, $entry_id ) {
$form = get_post( $form_id );
if ( $form && $form->post_name === 'my-contact-form' ) {
$response = wp_remote_post( 'https://api.example.com/submit', array(
'body' => array( 'form_data' => json_encode( $form_data ) ),
) );
do_action( 'greyd_forms_log', $response, 'API response' );
if ( is_wp_error( $response ) ) {
do_action( 'greyd_forms_error', 'An error occurred while sending the data to the API.' );
} else {
do_action( 'greyd_forms_success', array(
'post_id' => $form_id,
'message' => 'Thank you for your submission. Your data has been sent to the API.',
) );
}
}
}
add_action( 'greyd_forms_action_after_entry', 'greyd_forms_action_after_entry_example', 10, 3 );