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.

Passing post ID to function

  1. Hi, hoping this is within the scope of support you can reasonably give. I'm trying my hand at using of the developer hooks to achieve some stuff not available out of the box. I'm trying to achieve the following:

    I have a loop containing posts from a custom post type (created by a specific author using a gravity form - no problems with this, all working as it should)

    Each item in this loop is a job post that has an expiry date (so that the system knows when to turn it into a draft). This expiry date is calculated using the date of publication/posting and adding either 14, 30 or 45 days depending on what the user selected when they posted (again no problems with the logic for this - I have it all working elsewhere).

    For each item in this loop I want to have a link that takes the user to a gravity form that allows them to update the expiry date by adding an extension of either 5, 10 or 15 days. The system would then add this many days to the current expiry date associated with that post (in a custom field) and update the post.

    Having scoured the documentation and forums I've got as far as the code below:

    <?php
    // Updates expiration field on post extension form
    add_action("gform_post_submission_4", "form_update_expiry_date", 10, 2);
    function form_update_expiry_date($entry,$form){
    $targetPost = $entry["1"];  //Gets the value of form field which is dynamically populated with the post ID passed to it via a query string
    $new_date = $entry["2"]; //Gets the value of form field containing a new date - this is not really what I need though - did this more for a proof of concept
    global $wpdb;
    $wpdb->query("UPDATE $wpdb->postmeta SET meta_value= '$new_date' WHERE post_id=$targetPost AND meta_key='expiry date'");
    }
    ?>

    I'm using a query string to grab the ID of the post that the form should edit and passing that to a variable to use later in the wpdb query.

    The biggest problem with the above block of code is that I need to somehow get the custom field value for the current expiry date. At the moment the code just doesn't really do what I need it to do and I'm really not sure that I'm doing things in the most straightforward way.

    My question is this:

    How would I pass the ID of the post to be edited to the form so that I can access values within that posts custom fields (assuming this can even be done?). It's just occurred to me that I could probably pass the expiry date custom field value to the form using a query string though that would leave it open to URL manipulation (someone could change the date or ID which isn't great).

    Any pointers would be great - can probably muddle through once I know how to pass the ID to the function above is a more robust way (maybe using the Gform field value hook to pass the value to the form then grabbing the value from the form?).

    Many thanks

    Posted 13 years ago on Monday July 18, 2011 | Permalink
  2. Whatever link the user clicks on to extend the expiry date for a post, I would add that post ID as a variable in the query string.

    When the form loads, you can use the dynamic population option on the advanced tab of a hidden field to store the passed post ID from the query string.

    When the form is submitted, the post ID (now stored in the hidden field) will be submitted as part of the entry and will be accessible like so: $entry[12] (updating the "12" to the ID of the hidden field).

    Now that you have the post ID in the gform_post_submission function, you can use the WP function get_post_meta to retrieve the current expiry date and update_post_meta to update the expiry date.

    Posted 13 years ago on Monday July 18, 2011 | Permalink
  3. Hi David - thanks very much for the reply. So it looks as though I'm on the right track with the above - hadn't occurred to me to use get_post_meta - pretty obvious when it's put in front of you!

    The only bit about that solution I'm not so sure about is passing the post ID in a query string. I know the vast majority of users wouldn't know how to manipulate one but if someone who does know what they're doing changes the ID they could potentially change up the expiry date of someone elses post. Might need to look into ways of obfuscating the ID or something. However, that's not really a GF question - one for Google!

    Thanks again for your help - I'll give what you suggest a go and see how I get on

    Posted 13 years ago on Tuesday July 19, 2011 | Permalink
  4. sascha
    Member

    Hi jw902,
    how did you get on in the end? I'm just looking into exactly the same setup to let users edit only their own post/submissions. Did you find a way to secure the whole setup, so that not anyone just use the query string to change other users posts/submissions?

    Any ideas on that from the super helpful and knowledgeable Gravity Forms guys?

    Posted 13 years ago on Friday August 12, 2011 | Permalink
  5. Hi Sascha

    Sorry for the very slow reply - din't notice until this morning that there had been any more replies on this thread.

    If it's still of any help then I did kind of come up with a way to do it securely (though some might spot some holes in this method I've done some testing and it seems to work OK). I couldn't find a way to obfuscate the query string without lots of lines of extra code so in the end I went for some conditional logic on the form page itself. So basically I did the following - http://www.pastie.org/2496286

    The only thing this doesn't guard against is a logged in user entering a post ID that matches another one of their own posts into the URL. To get around this I was thinking of writing a few lines of code that add 3 random numbers to the front of the ID number and to the end then stripping these away before I use them in variables etc.

    Hope this helps a bit - feel free to post again if you have any questions - I'll follow replies on this thread by email again

    Cheers!

    Posted 13 years ago on Wednesday September 7, 2011 | Permalink
  6. sascha
    Member

    Hi jw902,

    sorry, you must have a real name! Thanks for the code. Would it be possible if you could post the whole code/describe setup of how to edit an existing post with a form? So basically calling the right form, then letting the user edit it, changing the metadata and updating the form.
    How do you set the expiry date? Do you use a custom field? In what data form do you save the data in there and calculate the difference?
    I'm not a coder, just picking up bits here and there. Is it possible to see your working example too, if it is online?
    Cheers,
    Sascha

    Posted 13 years ago on Friday November 25, 2011 | Permalink