Skip Navigation

[Resolved] Get CRED form_id by slug

This support ticket is created 6 years, 4 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 2 replies, has 2 voices.

Last updated by markL 6 years, 4 months ago.

Assisted by: Shane.

Author
Posts
#920274

Hi there,

Is there a way to get the form id of a form from its slug using php?

Thanks - Mark

#920547

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

#921032

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 );
			}
		}	
	}