Skip Navigation

[Resolved] Password protect custom posts with Toolset Forms

This thread is resolved. Here is a description of the problem and solution.

Problem:
How can we make a Post password-protected, when using Toolset forms?

Solution:
There isn't any inbuilt solution, you'd have to use some custom code hooked to a Forms API.

A full example can be seen here:
https://toolset.com/forums/topic/password-protect-custom-posts-with-toolset-forms/#post-1452647

This support ticket is created 4 years, 3 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
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 2 replies, has 2 voices.

Last updated by Rune Brynestad 4 years, 3 months ago.

Assisted by: Beda.

Author
Posts
#1451613
password_protect.jpg

I would like to offer my users to password protect custom posts with the ability to define a password in order to give access to their posts only to their friends.

For this, the idea is to use a Toolset form and define a checkbox custom field "password-protected" in order to define if the user wants to protect the post, and then, if yes, use another text custom field "password" to define the password to use.

Then, I want to update the custom post status to password protected.

My issue is how to affect the "password" custom field to the postype status

Could it be done?

Thanks in advice.

Regards
Rune

#1452647

Toolset Forms misses support for password protection post status.

You'd have to apply Custom Code to update the Password chosen by the user during submission of the Form.

WordPress manages password-protected posts like so:

1. It creates a normal post entry in the database
2. That Post entry gets a column post_password, which holds the password chosen, not encrypted or anything (which is lucky for us who write custom code for it)
3. The post status is still "publish", even if the WP Backend will say "password protected".

This means in Toolset Forms, all we'd need is an input field (can be a generic field) and a checkbox (which also can be generic) that lets the user set "yes, please password protect my post" and then set a password.

We will grab that value, and update it to the post, using cred_save_data() hook.
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

Using that hook we can get data from the form when it's submitted and when it saves things in the database we can interact there and update the entry.

We are not allowed to write a fully working custom code, but the below example is well commented and should help you achieve the goal tailored to your case.
Note that your form needs 2 fields, a Checkbox "checkbox_yes_protect_my_post" and a single line "post_password_custom" or similarly named.
Note that you could hide the post_password_custom line field - using a Forms Conditional - until the checkbox is checked.
That's, however, a minor design detail, here the code that will do the magic (please keep in mind that you need to understand, and then adapt this code to your specific use case)

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']==12) {
    	//if our checkbox field checkbox_yes_protect_my_post is checked 
        if ($_POST['checkbox_yes_protect_my_post'] == 1) {
			//Get the value from post_password_custom field
			$password = $_POST['post_password_custom'];

			//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 );
        }
    }
}
#1455019

My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.