-
-
Save aristath/7f5ed7185a35e58c8ea65d1154b3d86d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Intercept form submissions and create posts in a Custom Post Type. | |
* | |
* - Intercepts the form-submissions | |
* - Registers the Custom Post Type | |
* - Adds columns to the admin page for the CPT | |
* - Prints data in the CPT columns in the dashboard. | |
*/ | |
/** | |
* Intercept the form submission. | |
*/ | |
add_filter( 'gutenberg_form_submit_short_circuit', function( $override, $params ) { | |
$post = wp_insert_post( [ | |
'post_type' => 'form_submissions', | |
'meta_input' => [ | |
'name' => $params['name'], | |
'email' => $params['email'], | |
'comment' => $params['comment'], | |
] | |
] | |
); | |
wp_safe_redirect( get_site_url( null, $params['_wp_http_referer'] ) ); | |
exit; | |
}, 10, 2 ); | |
/** | |
* Add the custom post type. | |
*/ | |
add_action( 'init', function() { | |
register_post_type( 'form_submissions', [ | |
'label' => __( 'Form Submissions', 'forms-cpt' ), | |
'public' => true, | |
'show_in_rest' => false, | |
'hierarchical' => false, | |
'excluded_from_search' => true, | |
'publicly_queryable' => false, | |
'show_ui' => true, | |
'show_in_menu' => true, | |
'show_in_admin_bar' => false, | |
'icon' => 'dashicons-feedback', | |
] ); | |
} ); | |
/** | |
* Add colums to the CPT admin page. | |
*/ | |
add_filter( 'manage_form_submissions_posts_columns', function( $columns ) { | |
unset( $columns['title'] ); | |
$columns['name'] = __( 'Name', 'forms-cpt' ); | |
$columns['email'] = __( 'Email', 'forms-cpt' ); | |
$columns['comment'] = __( 'Comment', 'forms-cpt' ); | |
return $columns; | |
} ); | |
/** | |
* Print the content of the columns. | |
*/ | |
add_action( 'manage_posts_custom_column', function( $column_name, $post_id ) { | |
$post = get_post( $post_id ); | |
// Bail early if not a form submission. | |
if ( 'form_submissions' !== $post->post_type ) { | |
return; | |
} | |
switch ( $column_name ) { | |
case 'name': | |
echo esc_html( get_post_meta( $post_id, 'name', true ) ); | |
break; | |
case 'email': | |
$email = esc_html( get_post_meta( $post_id, 'email', true ) ); | |
echo '<a href="mailto:' . $email . '">' . $email . '</a>'; | |
break; | |
case 'comment': | |
echo esc_html( get_post_meta( $post_id, 'comment', true ) ); | |
break; | |
} | |
}, 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The example on
form-submissions-cpt.php
can be seen in action in the screencast below:Screen.Recording.2023-06-06.at.10.58.27.AM.mov
It assumes a default form with
name
,email
andcomment
fields.If the form contains different fields, the code can be adjusted accordingly.
Note: The above is just a proof of concept to show how easy it would be to use the forms/inputs blocks to implement any kind of behavior, from creating a post to sending requests to 3rd-party APIs and anything in between.
As a proof-of-concept, it doesn't handle security etc - but that would be easy to do by adding some additional checks for the
nonce
that form submissions include by default.Prerequisites for the forms to work while this is still an unmerged PR:
npm run build
in GutenbergGutenberg
->Experiments
, and enable theForm and input blocks
experiment.