Skip Navigation

[Resolved] Force posts to be password protected for both create and edit forms

This support ticket is created 2 years, 1 month 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 4 replies, has 2 voices.

Last updated by stefanoL-3 2 years, 1 month ago.

Assisted by: Minesh.

Author
Posts
#2472675

Tell us what you are trying to do?
Scenario: we have registered users who can publish and edit only their own posts.
Those posts contain personal data about our users's customers.
Those posts must be password protected because our users need to share a password with their customers in order to grant access to their personal data. The "post submit form" and the "edit post form" have both a password field that is marked as *Required.
Logged in users must submit a post and fill the password field and customer email field. At this point the form will send a notification to customer email with the password and the link to access the post, and the post must be published as password protected status, using the password field as post_password meta.

We achieved everything except the post that is saved as password protected.

we used this code:

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
// Change the ID below to the ID of your Toolset Form
if ($form_data['id']==id-of-the-form-in-my-case-was-71) {
//Get the value from post_password_custom field in my case named password-cantiere
$password = $_POST['password-cantiere'];

//update the post with the new password
//we can use Update, because at this point forms already created the post
//$post_id is the Post we create with Forms, it is given to us by the Forms API, see functions parameters
$my_post = array(
'ID' => $post_id,
'post_password' => $password
);

// Insert the post into the database
wp_update_post( $my_post );
}
}

Is there any documentation that you are following?
https://toolset.com/forums/topic/password-protect-custom-posts-with-toolset-forms/

Is there a similar example that we can see?
https://toolset.com/forums/topic/password-protect-custom-posts-with-toolset-forms/

What is the link to your site?
hidden link

#2473381

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Technically the code you shared should work but I'll have to review your setup and check why its not working at your end.

Can you please share admin access details as well as user access details using which I should submit the form with password and problem URL where you added the form. Please share details where you added the "cred_save_data" hook code.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2473477

Minesh, we found the trick!

every custom field has wpcf- text as prefix. So the system name of my custom field "password-cantiere" is "wpcf-password-cantiere"
changing that the issue has been resolved.
By the way it could be a bug because in Field Slug showed into Edit Post Field Group is different from the one showed within the edit post screen into custom field metabox.

Is there a way to manage that prefix?

#2473845

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

It's not a bug. The custom fields you created using Toolset Types will have prefix "wpcf-".

Please check the following doc and its metioned at top section:
=> https://toolset.com/documentation/customizing-sites-using-php/functions/

However, when you are accessing custom fields through native WordPress functions, you need to prepend the wpcf- prefix to the slug. Continuing from the above example, for native WordPress function to access the “House Price” field, you need to use the wpcf-house-price slug.

#2474481

My issue is resolved now. Thank you!