Create a WP Post from a Form Submission.
Create a WP Post from a Form Submission. Create a form using Contact Form 7, Fast Secure Contact Form, or just a plain form, be sure to name your fields correctly, then the form submission will be sent to a post.
Essentially you can think of this as a form wrapper around the wp_insert_post fuction. The field name-value pairs of the form become inputs to a wp_insert_post call.
WARNING: using this plugin provides spammers the opportunity to send you automated spam form submissions.
Minimally, your form must have the following two fields. A post will not be created if one or both is missing.
As hidden field: If you want to make all the posts be of the same category, create a "post_category_name" hidden field whose value is the category name. If using CF7, it does not provide hidden fields. But you can add them by adding the plugin Contact Form 7 Modules.
As checkboxes: to make the category choosable by the user via a checkbox, create a checkbox form field named "post_category_name" with each having a value that is a category name. (If writing your own HTML directly, use "post_category_name", see "Plain Form Example" below).
CF7 Form Definition Example:
Post Title [text* post_title] <br/> Post Content (required) <br/> [textarea* post_content] <br/> Categories (required) <br/> [checkbox* post_category_name "Uncategorized" "Cat1" "Cat2" "Cat3"] <br/> [submit "Post"] <br/>
Aside: if you are familiar with wp_insert_post, then you will know that there is a "post_category" parameter but no "post_category_name". The problem with "post_category" is it requires category ids (the numbers). But what you really want in a form are the category names. So this plug allows for "post_category_name" which can be one or more category names. It looks up the associated category numbers and sets "post_category" for wp_insert_post. But you can use "post_category" instead if you like. But do not use both in the same form.
Whereas post_author requires an user id number, you can alternatively use post_author_name takes the login name. (Same idea as post_category_name an an alternative to post_category).
A weaker form of post_author_name, takes a login name. When not using post_author_name or post_author_default, then an author will only be set on the post if a person is logged in. In that case his login is used. If you use post_author_name then that ignores the user's login and sets the author to the post_author_name value. If you use post_author_default instead, then it will use the user's login id if he is logged in, but if he is not logged in it will set the author to the value of post_author_default.
Not using CF7 nor FSCF You can define your own form naming fields as described above. But you need to do one extra step in this case. You need to have the target page of your form insert the data in the post. You do this by means of a short code [capture-form-to-post]. Simply place the short code on your form's target page and it will capture the submission and create a post.
Plain Form Example:
In this example, we create our own form, that posts to the same page. So we put both the short code and the form definition in the same page. The short code only does something when there are post parameters. NOTE: your form must have method="post" not "get".
[capture-form-to-post] <form action="" method="post"> Post Title: <input type="text" name="post_title" value=""/><br/> Post Content: <br/> <textarea rows="10" name="post_content" cols="20"></textarea> <input type="checkbox" name="post_category_name" value="Uncategorized">Uncategorized<br> <input type="checkbox" name="post_category_name" value="Cat1">Cat1<br> <input type="checkbox" name="post_category_name" value="Cat2">Cat2<br> <input type="checkbox" name="post_category_name" value="Cat3">Cat3<br> <input type="submit" /> </form>
Remember: do NOT use [capture-form-to-post] if your form is a CF7 or FSCF form.
There are many more parameters to wp_insert_post that can be set simply by putting a form fields in your form of the same name as the wp_insert_post parameter. Examples are:
Setting Post Meta (Custom Fields):
You can optionally set "post meta" (custom field) key-value pairs on your post. To do this, add fields to your form whose name start with "meta_". For example, if you want to set the post meta key "my_key" then create a form field named "meta_my_key". The "meta_" prefix is used to identify the field as a post meta field and the "meta_" gets stripped off. Then a call to update_post_meta is made give that key and the field's value.
Requires: 3.2.1 or higher
Compatible up to: 3.3.2
Last Updated: 2014-8-29
Active Installs: 600+
0 of 4 support threads in the last two months have been resolved.
Got something to say? Need help?