Skip to content

Commit 2ebb73a

Browse files
luizbillsk1sul1
authored andcommitted
Allow using submission id in submission title format #143
1 parent b9a1d7d commit 2ebb73a

2 files changed

Lines changed: 21 additions & 6 deletions

File tree

classes/class-cpt-wplf-form.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,8 @@ public function meta_box_title_format( $post ) {
598598
?>
599599
<p><?php esc_html_e( 'Submissions from this form will use this formatting in their title.', 'wp-libre-form' ); ?></p>
600600
<p><?php esc_html_e( 'You may use any field values enclosed in "%" markers.', 'wp-libre-form' ); ?></p>
601+
<?php // translators: %submission-id% is not meant to be translated ?>
602+
<p><?php esc_html_e( 'In addition, you may use %submission-id%.', 'wp-libre-form' ); ?></p>
601603
<p>
602604
<input
603605
type="text"

inc/wplf-ajax.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,17 @@ function wplf_ajax_submit_handler() {
3232
// the title is the value of whatever the first field was in the form
3333
$title_format = get_post_meta( $form->ID, '_wplf_title_format', true );
3434

35+
// create submission post
36+
$post_id = wp_insert_post( array(
37+
'post_title' => '',
38+
'post_status' => 'publish',
39+
'post_type' => 'wplf-submission',
40+
) );
41+
42+
// exposes $post_id in $_POST to be able to use in the title
43+
// hacky, but uses less memory than a copy of $_POST
44+
$_POST['submission-id'] = $post_id;
45+
3546
// substitute the %..% tags with field values
3647
$post_title = $title_format;
3748

@@ -44,12 +55,14 @@ function wplf_ajax_submit_handler() {
4455
$post_title = preg_replace( '/%.+?%/', $replace, $post_title, 1 );
4556
}
4657

47-
// create submission post
48-
$post_id = wp_insert_post( array(
49-
'post_title' => $post_title,
50-
'post_status' => 'publish',
51-
'post_type' => 'wplf-submission',
52-
) );
58+
// save the title
59+
wp_update_post( [
60+
'ID' => $post_id,
61+
'post_title' => $post_title,
62+
] );
63+
64+
// don't save the post id in meta
65+
unset( $_POST['submission-id'] );
5366

5467
// add submission data as meta values
5568
foreach ( $_POST as $key => $value ) {

0 commit comments

Comments
 (0)