Hi there,
Is there a way to get the form id of a form from its slug using php?
Thanks - Mark
Shane
Supporter
Languages:
English (English )
Timezone:
America/Jamaica (GMT-05:00)
Hi Mark,
Thank you for contacting our support forum.
You should be able to as the forms are stored in the wp_posts table.
Here is an example of how it can be done
https://stackoverflow.com/questions/14979837/wordpress-query-single-post-by-slug
Please let me know if this helps.
Thanks,
Shane
Thanks Shane,
Trying to retrieve the form post using get_posts(), which was my first approach didn't work for an unknown reason.
Using either wp_query( $args ) or get_post( $id ) can retrieve the form post, but the solution I preferred used get_page_by_path() as described in the stack exchange question you suggested:
https://stackoverflow.com/questions/14979837/wordpress-query-single-post-by-slug
Below is my solution for updating post titles on CRED save according to the form name:
add_action('cred_save_data', 'update_document',10,2);
function update_document($post_id, $form_data ){
$forms = array(
get_page_by_path('coordinator-new',OBJECT,'cred-form')->ID,
get_page_by_path('coordinator-edit',OBJECT,'cred-form')->ID
);
if (in_array( $form_data['id'], $forms, false ) ) {
if(isset($_POST['wpcf-first-name']) || isset($_POST['wpcf-last-name']) ){
$custom_title = $_POST['wpcf-first-name'].' '.$_POST['wpcf-last-name'];
$custom_slug = sanitize_title_with_dashes( $custom_title );
/*update the title and slug */
$my_doc = array(
'ID' => $post_id,
'post_title' => $custom_title,
'post_name' => $custom_slug,
);
wp_update_post( $my_doc );
}
}
}