Skip Navigation

[Resolved] What is the URL naming convention for posts created with CRED forms?

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.

This topic contains 2 replies, has 2 voices.

Last updated by PaulS4783 1 month, 1 week ago.

Assigned support staff: Waqar.

Author
Posts
#2089129

I'm looking at the URL of some custom posts created with these URLs

/custom-post-type/cred-auto-draft-f062a0888d0b0c16609b84f87cf630c5/
/custom-post-type/cred-auto-draft-f062a0888d0b0c16609b84f87cf630c5-2/
/custom-post-type/cred-auto-draft-f062a0888d0b0c16609b84f87cf630c5-3/

Which, as you can see, are following a predictable pattern.

But then the next post that was created:

/custom-post-type/cred-auto-draft-1d02000c1880f3583e53b0d18d4dda96/

So I am curious about how CRED constructs the URL?

If it is completely randomized, we can use that as a form of cross user security (ie. logged in users would have great difficulty checking each other's posts.

Is there a way to assign a truly random URL for each CRED created post?

#2089301

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Thank you for contacting us and I'd be happy to assist.

When no title is specified, the Form adds a placeholder title "cred-auto-draft........" from the current timestamp. The slug part of the URL for each post is derived from the post's title, itself.

If your goal is to assign a random, unique and hard to get title and URL of the posts created using the Form, you can use the "cred_save_data" hook to programmatically set them:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

For example, to set the title and slug of the created post from the current user's ID and the current timestamp in an encrypted form, you can use:


add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
	// if a specific form
	if ($form_data['id']==12345)
	{
		// create a new title from current user's ID + current time and then encrypt it with sha1
		$new_title = sha1(get_current_user_id().time());

		// sanitize the title and the slug values 
		$new_title = sanitize_text_field( $new_title );
		$new_slug = sanitize_title( $new_title );

		// update the title and the slug of the created post
		$args = array('ID' => $post_id, 'post_title' => $new_title, 'post_name' => $new_slug);
		wp_update_post($args);
	}
}

Note: Please replace "12345" with the actual ID of your form. The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.

I hope this helps and for more personalized assistance around custom code, you can also consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

regards,
Waqar

#2091119

Thank you. This is perfect! Although I used a different hook. You can close the ticket.

function set_post_to_timestamp( $post_id, $form_data ) {
  // only for these specific forms - add a payment OR add a vehicle
  if ( ($form_data['id']==4648) || ($form_data['id']==4652) ) {
         
    // create a new title from current user's ID + current time and then encrypt it with sha1
        $new_title = sha1(get_current_user_id().time());
 
        // sanitize the title and the slug values 
        $new_title = sanitize_text_field( $new_title );
        $new_slug = sanitize_title( $new_title );
         
    // set the post title and slug
    
    $my_args = array(
        'ID' => $post_id,
        'post_title' => $new_title,
        'post_name' => $new_slug,
    );
    wp_update_post( $my_args );
  }
}
add_action( 'cred_submit_complete', 'set_post_to_timestamp', 10,2 );