PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Setting GF-form submitted posts to password protected?

  1. RichardBest
    Member

    Hi All - I'm interested in setting up a pay-to-post mechanism, using the Paypal Add-On, that works like this:
    - the form contains post fields and is integrated with Paypal via the Add-On, such that the post is not published until payment is received;
    - the redirect for the form is set-up in such a way that, once payment is received, the site redirects the user to the post he or she has just submitted
    - only the person that has just paid to submit the post can see the post

    I've got all this working except for the third step. I know how to make the third step work with a change to the functions file which automatically sets posts submitted through the form to private, but for the person who entered the post to be able to see it, he or she needs to be a logged in user/member. I can make that happen with a membership plugin but it occurred to me last night that there might be another way, by which I wouldn't need to worry about creating users/members.

    The idea I have is this: if possible, I'd like to be able to:
    - add a bit of code to the functions file which makes post entries through a specific form password-protected posts;
    - add a single line text field to the form which asks the user to enter a password for the post
    - somehow link the password that goes into that text field as the password WordPress requires for access to the submitted post

    Is that possible please? If so, can anyone help please with the code that would be required for the functions.php file?

    Many thanks in advance for any help.

    Best regards
    Richard

    Posted 13 years ago on Friday January 7, 2011 | Permalink
  2. http://pastie.org/1438417

    Just replace the 1 after "gform_post_data_" with the ID of your form and the 3 in "$_POST['input_3'] with the field ID of your password field.

    Posted 13 years ago on Friday January 7, 2011 | Permalink
  3. RichardBest
    Member

    Absolutely superb. Thanks so much David. I'm so impressed with the GF crew, that I'm off to Tweet! @richard_best

    Posted 13 years ago on Friday January 7, 2011 | Permalink
  4. RichardBest
    Member

    Thanks again for this David. I'm not sure, though, that this code has the effect of designating that all posts submitted through the specified form are to be treated as password-protected posts. It's not doing that for me with my testing. I'd be grateful for any further thoughts please. Is it also possible, for example, to set post_status for all posts submitted through a form to password protected?

    Many thanks.
    Richard

    Posted 13 years ago on Saturday January 8, 2011 | Permalink
  5. Okie dokie. Did some more testing and the snippet I shared with you definitely works; however... WordPress handles post passwords in an odd way that may be causing some confusion.

    When a user enters a password for a post, it stores that password as a PHP cookie. The problem is, it uses that same cookie for all other password protected posts on the site as well. This means that if more than one post have the same password, any user who enters the password for one will have access to view all posts with that same password without being prompted to reenter the password.

    Is this what you are experiencing or are your posts not being password protected at all?

    Posted 13 years ago on Saturday January 8, 2011 | Permalink
  6. RichardBest
    Member

    Hi David

    The problem I'm having is that the posts aren't being password protected at all.

    I wonder whether this might have something to do with the fact that I'm running the Paypal Add-On in conjunction with the form?

    I presume also that it doesn't make any difference whether the form is set, in the post field settings, to go to draft, pending review or published?

    I can send you a login to the testing site I'm testing this on if that would help.

    Many thanks
    Richard

    Posted 13 years ago on Saturday January 8, 2011 | Permalink
  7. That would be helpful Richard: david@rocketgenius.com. Thanks!

    Posted 13 years ago on Saturday January 8, 2011 | Permalink
  8. Hi Richard,

    The issue did reside in the fact you were using the delay post creation PayPal functionality. Here is an updated script that should do the trick:

    http://pastie.org/1441553

    The issue is that the values of the $_POST array are different when the gform_post_data hook is run without the delay vs with the delay. Instead of having all of the entry data available in the the $_POST, we have to use the invoice ID (only available in the $_POST when a PayPal payment is being processed) to get the entry data.

    Let me know if you have any issues with this.

    Posted 13 years ago on Sunday January 9, 2011 | Permalink
  9. RichardBest
    Member

    Many thanks David. Much appreciated. I've tried the revised code but cannot get it working. Presumably I change the number in "gform_post_data_4" to the ID of my form (i.e., 1) and the number in "$post_data['post_password'] = $entry['6']" to the ID no. of the field into which the password is being entered (i.e., 63). I've tried this but the posts are still not being set as password protected. The full code I'm using in my relevant functions file is:

    add_filter("gform_post_data_1", "set_post_password");
    function set_post_password($post_data){
    
        $entry = RGFormsModel::get_lead($_POST["invoice"]);
    
        $post_data["post_password"] = $entry["63"];
    
        return $post_data;
    }

    (As you can see, I tried double as opposed to single quotes to see if that made any difference; as expected, it didn't.)

    Sorry to keep bothering you with this. I much appreciate your help.

    Best
    Richard

    Posted 13 years ago on Sunday January 9, 2011 | Permalink
  10. Haha, this just doesn't like us does it? I spoke with Alex about this and he made a few recommendations. Here is the updated code:

    http://pastie.org/1442490

    This has already been implemented on your dev site, so you won't need to add it again, just wanted to post it here for posterity.

    For anyone else reading this thread, keep in mind that the "invoice" variable returned by the IPN in the $_POST will not always be the same as the entry ID for which the payment is being made. Instead, you will need to use the "custom" variable in the $_POST ($_POST['custom']) which can be used to not only retrieve the entry ID, but also to test the validity of "custom" variable demonstrated in the code sample above.

    Posted 13 years ago on Sunday January 9, 2011 | Permalink
  11. RichardBest
    Member

    That's perfect. Thanks so much David and Alex.

    Just one final question please if I may: If I wanted to do the same thing for a number of forms on the same site, would it be OK to replicate the code a number of times, but adding the different form IDs and password field IDs, or would it be necessary to modify the code (into an array perhaps)?

    Thanks again
    Richard

    Posted 13 years ago on Sunday January 9, 2011 | Permalink