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.

How to update a post?

  1. sascha
    Member

    Hi there,

    I am not 100% sure how to use the gform_disable_post_creation hook to update a post. Could you post a quick example on how to use that hook. The documentation gives this example to disable post creation:

    <?php
    add_filter("gform_disable_post_creation", "disable_post_creation", 10, 3);
    function disable_post_creation($is_disabled, $form, $entry){
        return true;
    }
    ?>

    So if I wanted to disable post creation and update a post instead, how would I do this lets say for form id 6? Like below?

    <?php
    add_filter("gform_disable_post_creation_6", "my_update_post", 10, 3);
    function my_update_post($is_disabled, $form, $entry){
        wp_update_post($post_id); // not sure about the right syntax here
        return true;
    }
    ?>

    Which information would I have to pass to wp_update_post and how would I take this information from that just filled out form? Would I also need to use the gform_after_submission hook? Which hook gets fired first?

    Posted 10 years ago on Wednesday January 18, 2012 | Permalink
  2. sascha, the wp_update_post is a WordPress function:
    http://codex.wordpress.org/Function_Reference/wp_update_post

    That will not be visible today due to the SOPA blackout. But the correct syntax can be found there.

    Posted 10 years ago on Wednesday January 18, 2012 | Permalink
  3. sascha
    Member

    Hi Chris,

    thanks. The wp_update_post will not be the problem. The problem is using the right GF hooks and using the correct GF objects to get all the necessary information from the just filled in form. Could you just give me a little outline on which hooks to call. Both mentioned above or just the one? Or any others even? There is for instance a post on the gform_disable_post_creation filter where David mentioned the below:

    Hi Tracy,

    Try adding the "gform_disable_post_creation" filter in the gform_pre_submission hook instead. You will need to run your check again to verify whether the post creation should actually be disabled.

    I do not totally understand why I would have to add it to the gform_pre_submission hook?
    I really just want to get info about the GF side of things. For Wordpress functions I will consult the Codex.

    Posted 10 years ago on Wednesday January 18, 2012 | Permalink
  4. Sounds like you are disabling post creation just fine. True?

    Then, you need to update an existing post, so you could probably use the gform_after_submission hook to do whatever you want with the submitted data. Before you run that, you will need the ID if the existing post you want to update.

    Is that enough information to go on?

    Posted 10 years ago on Wednesday January 18, 2012 | Permalink
  5. sascha
    Member

    Hi Chris,
    that worked like a charm! That is disabling post creation. One thing though which I am not sure yet on how to override/sort out: I have the form set up to redirect after submission with a query string of p={post_id}. See: http://grab.by/bB3e
    Obviously if I edit the post, there is no post created, but I would still like the form to redirect to the just edited post. Is there a way to do this? How can I pass the ID to the query string? Or would I do it differently?

    Posted 10 years ago on Wednesday January 18, 2012 | Permalink
  6. Seems like you need to have the post ID of the post you're updating, right? Is it in a hidden field in the form? If so, then use that merge tag instead of {post_id} in your redirect. Otherwise, please explain how you know which post ID to update. If you have it, we need to get it to the query string ?p=

    Posted 10 years ago on Wednesday January 18, 2012 | Permalink
  7. sascha
    Member

    Hi Chris,
    the idea is to allow post editing only on the currently displayed post, which has the form embedded in it, let's say in a spoiler or a tab. So I just get the id with get_current_post_id or whatever it's called. But putting the ID of each post into a hidden field sounds like a great idea! Then I just use the merge tag for that field. If I use the hidden field merge tag it also should redirect on post creation right?

    Posted 10 years ago on Wednesday January 18, 2012 | Permalink
  8. If you have the page/post where the form is embedded, you don't need to use the query string at all. Just use the {embed_url} merge tag for your redirect URL and you're done.

    To be clear, you have a form on a page/post, and the form updates the information on that same page?

    Posted 10 years ago on Wednesday January 18, 2012 | Permalink
  9. sascha
    Member

    Yes, correct. Calling the form from a post and it updates that information. Need to print out the merge tags page. It has grown since I last saw it, I think....

    Should I use 2 separate (duplicate) forms then? One for creating a post (and redirecting to the just created post) and then the duplicated one (obviously with a different merge tag embed_url for the redirect!) for the editing of the post?
    BTW: what makes a form create a post on GF? Is it only the title field that triggers that action?

    Posted 10 years ago on Wednesday January 18, 2012 | Permalink
  10. sascha
    Member

    Hi Chris,
    how do I use the embed_url in the query string? p={embed_url} did not work url={embed_url} neither. Just cannot find anything on google either. It's all about how to write the rewrite rules, but not what to actually write in the query string....

    Posted 10 years ago on Thursday January 19, 2012 | Permalink
  11. sascha
    Member

    Ok, used different merge tag: p={embed_post:ID}. Is it possible to use the embed_url merge tag though? How would I use it?

    Posted 10 years ago on Thursday January 19, 2012 | Permalink