A big thank you to Joomla Jones for contacting me to highlight an issue with my plugin where if you duplicate a CF7 form or you create a new form the default form is deleted.
This was the code block with the issue, this is a new WP_Query() post loop which retrieves the contact forms from the database to be displayed in the post editor.
$args = array(
'post_type' => 'wpcf7_contact_form',
'posts_per_page' => 20,
'post_status' => 'publish',
'orderby' => 'modified',
'order' => 'DESC',
);
// The Query
$contact_forms_query = new WP_Query($args);
$contact_forms = [];
if ($contact_forms_query->have_posts()) {
while ($contact_forms_query->have_posts()) {
$contact_forms_query->the_post();
$contact_ID = get_the_ID();
// Assuming 'wpcf7-messages' is the correct meta key for the serialized messages
$messages = get_post_meta($contact_ID, '_messages', true);
$success_message = is_array($messages) && isset($messages['mail_sent_ok']) ? $messages['mail_sent_ok'] : 'Default success message or empty string';
$contact_forms[] = [
'id' => $contact_ID,
'title' => get_the_title(),
'modified' => get_the_modified_date(),
'success_message' => $success_message,
];
}
}
I fixed the issue by replacing the WP_Query() function with a get_posts() function.
$args = array(
'post_type' => 'wpcf7_contact_form',
'posts_per_page' => 20,
'post_status' => 'publish',
'orderby' => 'modified',
'order' => 'DESC',
);
// Get posts directly
$contact_forms_posts = get_posts($args);
$contact_forms = [];
foreach ($contact_forms_posts as $post) {
$contact_ID = $post->ID;
// Assuming 'wpcf7-messages' is the correct meta key for the serialized messages
$messages = get_post_meta($contact_ID, '_messages', true);
// Attempt to access the 'mail_sent_ok' message within the possibly serialized data
$success_message = is_array($messages) && isset($messages['mail_sent_ok']) ? $messages['mail_sent_ok'] : 'Default success message or empty string';
$contact_forms[] = [
'id' => $contact_ID,
'title' => $post->post_title,
'modified' => $post->post_modified,
'success_message' => $success_message,
];
}
If you enjoy using this plugin please help support its development by leaving positive feedback at the link below
Style Contact Form 7 – Reviews