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