Skip to content

Instantly share code, notes, and snippets.

@artikus11
Last active January 11, 2022 08:59
Show Gist options
  • Save artikus11/867d91ab63005ffbd28c686447c93d97 to your computer and use it in GitHub Desktop.
Save artikus11/867d91ab63005ffbd28c686447c93d97 to your computer and use it in GitHub Desktop.
Создание записи при отправке формы из Contact Form 7
add_action( 'wpcf7_before_send_mail', 'created_post_in_cf7' );
function created_post_in_cf7( $contact_form ) {
$title = $_POST['text-title-post'] && ! empty( $_POST['text-title-post'] ) ? sanitize_text_field( $_POST['text-title-post'] ) : '';
$content = $_POST['text-contant-post'] && ! empty( $_POST['text-contant-post'] ) ? wp_strip_all_tags( $_POST['text-contant-post'] ) : '';
$field = $_POST['text-field-post'] && ! empty( $_POST['text-field-post'] ) ? sanitize_text_field( $_POST['text-field-post'] ) : '';
$ars = [
'post_type' => 'post',
'post_title' => $title,
'post_content' => $content,
'post_status' => 'pending',
'meta_input' => [
'text_meta_field' => $field,
],
];
$post_id = wp_insert_post( $ars );
$mail = $contact_form->prop('mail');
if ( false !== $post_id ) {
$mail['subject'] = $mail['subject'] .' Создана запись №'. $post_id;
}
$contact_form->set_properties(['mail' => $mail ]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment