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.

Add a form to create a new custom post type

  1. Anonymous
    Unregistered

    With Custom Post Types in WordPress v2.9 we now have a need for forms to create custom post types. For example, I have a website where I'd like users to be able to submit their solutions to our directory of solutions. That custom post types would be called "solution" and it would be great if in form settings I could select what kind of post type to create.

    Posted 14 years ago on Tuesday February 16, 2010 | Permalink
  2. WordPress 2.9 did improve custom post types, however things went begin to get more useful until WordPress 3.0 which is going to introduce even more custom post type capabilities.

    We do plan on adding custom post type capabilities, however we are holding off until the new features in WordPress 3.0 are introduced.

    Posted 14 years ago on Tuesday February 16, 2010 | Permalink
  3. Anonymous
    Unregistered

    Fair point. FYI I've been following the custom post type ticket on Trac very closely as it's the killer feature WordPress has really been needing.

    Is there a way through hooks I can change the post type on a form submit?

    Posted 14 years ago on Tuesday February 16, 2010 | Permalink
  4. Yes, it is possible to use a hook to set the post type. Alex will stop by this thread shortly and post a code snippet example.

    Posted 14 years ago on Tuesday February 16, 2010 | Permalink
  5. Yes, you can use a hook to change the post type. The following code snippet creates a page instead of a post. You will need to replace 5 with your actual form id (in $form["id"] == '5' )

    add_filter("gform_post_data", "set_post_type", 10, 2);
    function set_post_type($post_data, $form){
    
        if($form["id"] == '5'){
            $post_data["post_type"] = "page";
        }
        return $post_data;
    }
    Posted 14 years ago on Tuesday February 16, 2010 | Permalink
  6. Anonymous
    Unregistered

    Awesome, you rock dudes! :)

    One question though, any chance you can enhance the API in the future to allow me to associate a programmatic name with the form rather than have to use the numeric ID?

    Posted 14 years ago on Sunday February 21, 2010 | Permalink
  7. We will keep that in mind. I understand the advantages of using a name instead of the id, but that would require adding a "Name" field to the form editor that doesn't mean anything for non "power-users". We are try very hard to keep the UI clean.

    Posted 14 years ago on Monday February 22, 2010 | Permalink
  8. I guess my post is semi-related to this but I am looking for a way to have a custom post field that posts to the meta description and have the uploaded image post its link in post image field in thesis 1.6. Is this possible?

    I thought maybe that I might be able to change the excerpt field to post into the thesis meta description instead.

    Posted 14 years ago on Wednesday March 10, 2010 | Permalink
  9. Alex-

    Regarding this snippet:

    add_filter("gform_post_data", "set_post_type", 10, 2);
    function set_post_type($post_data, $form){

    if($form["id"] == '5'){
    $post_data["post_type"] = "page";
    }
    return $post_data;
    }

    I have tried to recreate this functionality on my end and it hasn't worked. While trying to figure out what the problem is, I had the system echo $form["id"] and this value appears to reflect not the ID of the form in the system but the ID of the entry. On my end, this value reflects the number of times the form has been filled out. Every time I fill the form out, this number increases by 1.

    I need to add an action dependent upon what form is being filled out.

    Posted 14 years ago on Saturday March 27, 2010 | Permalink
  10. Nevermind...pre submission I get the Form ID. Post submission I get the Entry ID.

    Posted 14 years ago on Saturday March 27, 2010 | Permalink
  11. I'm looking for this same functionality. When I insert the provided snippet into my functions.php page I get a white screen on refresh.

    I'm using Wordpress 3.0 Beta 1, current version of GF forms, form ID 1, and a Custom Post type of "events".

    Posted 14 years ago on Friday April 30, 2010 | Permalink
  12. Anonymous
    Unregistered

    @alex: Ah yes, the old "We can't add that highly useful feature because the end users would be too confused if we did" mantra. You didn't work for Apple in a past life, did you? :-)

    Seriously though; there are several ways to achieve this. You can use sanitize_title_with_dashes() on the name of the form but require them to be unique, easy-peasy.

    And you could easily add a non-conspicuous field on the advanced tab such as Name form for programmatic access" the only reveals when checked (like "Allow field to be populated dynamically" does for fields) that would default to the title passed to sanitize_title_with_dashes().

    I do hope you'll consider this. I had thought I'd gotten past having to use hard-coded numbers in code to identify things when I wrote my last FORTRAN program back in the early 80's, hope you won't prove me wrong. ;-)

    (BTW, did you know your forum doesn't display italics?)

    Posted 14 years ago on Sunday May 2, 2010 | Permalink
  13. Jan Egbert
    Member

    @lukeass7
    You need to change set_post_type to something else. I presume it is interfering with a new WordPress function called set_post_type().

    Posted 14 years ago on Monday May 3, 2010 | Permalink
  14. @Jan

    Thanks for the idea. I updated "set_post_type" to "update_post_type" and had success. Idea came from WordPress Trac.

    Posted 14 years ago on Tuesday May 4, 2010 | Permalink
  15. Works well, especially if you use "update_post_type" instead of "set_post_type" and "sp_events" as the type (for The Events Calendar plugin)

    Posted 14 years ago on Monday June 21, 2010 | Permalink

This topic has been resolved and has been closed to new replies.