{"id":73851,"date":"2011-11-12T08:00:00","date_gmt":"2011-11-12T08:00:00","guid":{"rendered":"https:\/\/www.weixiaoduo.com\/plugins\/2024\/02\/22\/form-to-post\/"},"modified":"2016-03-16T05:05:00","modified_gmt":"2016-03-16T05:05:00","slug":"form-to-post","status":"publish","type":"post","link":"https:\/\/www.weixiaoduo.com\/plugins\/form-to-post\/","title":{"rendered":"Form to Post"},"content":{"rendered":"<p>Create a WP Post from a Form Submission. Create a form using Contact Form 7, Fast Secure Contact Form, or just a plain<br \/>\nform, be sure to name your fields correctly, then the form submission will be sent to a post.<\/p>\n<p>Very limited.<\/p>\n<ul>\n<li>Only accepts text, no images, videos, etc.<\/li>\n<li>No error handling.<\/li>\n<\/ul>\n<p>Essentially you can think of this as a form wrapper around the <a href=\"https:\/\/codex.wordpress.org\/Function_Reference\/wp_insert_post\" rel=\"nofollow ugc\">wp_insert_post function<\/a>.<br \/>\nThe field name-value pairs of the form become inputs to a wp_insert_post call.<\/p>\n<p>WARNING: using this plugin provides spammers the opportunity to send you automated spam form submissions.<\/p>\n<p><strong>How To<\/strong><\/p>\n<ol>\n<li>Create a form using Contact Form 7 (CF7), Fast Secure Contact Form (FSCF), or create your own form HTML.<\/li>\n<li>Name your fields according to the parameters of the <a href=\"https:\/\/codex.wordpress.org\/Function_Reference\/wp_insert_post\" rel=\"nofollow ugc\">wp_insert_post function<\/a>.<\/li>\n<\/ol>\n<p>Minimally, your form must have the following two fields. A post will not be created if one or both is missing.<\/p>\n<ul>\n<li>post_title<\/li>\n<li>post_content<\/li>\n<\/ul>\n<p><strong>Special Fields<\/strong><\/p>\n<ul>\n<li>post_category_name can be used to set the category of the post<\/li>\n<\/ul>\n<p>As hidden field: If you want to make all the posts be of the same category, create a \u201cpost_category_name\u201d hidden field<br \/>\nwhose value is the category name. If using CF7, it does not provide<br \/>\nhidden fields. But you can add them by adding the plugin <a href=\"https:\/\/wordpress.org\/extend\/plugins\/contact-form-7-modules\/\" rel=\"ugc\">Contact Form 7 Modules<\/a>.<\/p>\n<p>As checkboxes: to make the category choosable by the user via a checkbox,<br \/>\ncreate a checkbox form field named \u201cpost_category_name\u201d with each having a value that is a category name.<br \/>\n(If writing your own HTML directly, use \u201cpost_category_name[]\u201d, see \u201cPlain Form Example\u201d below).<\/p>\n<p>CF7 Form Definition Example:<\/p>\n<pre><code>Post Title [text* post_title] <br\/>\n\nPost Content (required) <br\/>\n   [textarea* post_content] <br\/>\n\nCategories (required) <br\/>\n  [checkbox* post_category_name \"Uncategorized\" \"Cat1\" \"Cat2\" \"Cat3\"] <br\/>\n\n[submit \"Post\"] <br\/>\n<\/code><\/pre>\n<p>Aside: if you are familiar with wp_insert_post, then you will know that there is a \u201cpost_category\u201d parameter but no<br \/>\n\u201cpost_category_name\u201d. The problem with \u201cpost_category\u201d is it requires category ids (the numbers). But what you really<br \/>\nwant in a form are the category names. So this plug allows for \u201cpost_category_name\u201d which can be one or more<br \/>\ncategory names. It looks up the associated category numbers and sets \u201cpost_category\u201d for wp_insert_post.<br \/>\nBut you can use \u201cpost_category\u201d instead if you like. But do not use both in the same form.<\/p>\n<ul>\n<li>post_author_name<\/li>\n<\/ul>\n<p>Whereas post_author requires an user id number, you can alternatively use post_author_name takes the login name.<br \/>\n(Same idea as post_category_name an an alternative to post_category).<\/p>\n<ul>\n<li>post_author_default<\/li>\n<\/ul>\n<p>A weaker form of post_author_name, takes a login name. When not using post_author_name or post_author_default,<br \/>\nthen 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<br \/>\nthen that ignores the user\u2019s login and sets the author to the post_author_name value. If you use post_author_default instead,<br \/>\nthen it will use the user\u2019s 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.<\/p>\n<p><strong>Not using CF7 nor FSCF<\/strong><br \/>\nYou can define your own form naming fields as described above. But you need to do one extra step in this case.<br \/>\nYou 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].<br \/>\nSimply place the short code on your form\u2019s target page and it will capture the submission and create a post.<\/p>\n<p>Plain Form Example:<\/p>\n<p>In this example, we create our own form, that posts to the same page. So we put both the short code and the form<br \/>\ndefinition in the same page. The short code only does something when there are post parameters.<br \/>\nNOTE: your form must have method=\u201dpost\u201d not \u201cget\u201d.<\/p>\n<pre><code>[capture-form-to-post]\n<form action=\"\" method=\"post\">\n   Post Title: <input type=\"text\" name=\"post_title\" value=\"\"\/><br\/>\n   Post Content: <br\/>\n   <textarea rows=\"10\" name=\"post_content\" cols=\"20\"><\/textarea>\n   <input type=\"checkbox\" name=\"post_category_name[]\" value=\"Uncategorized\">Uncategorized<br>\n   <input type=\"checkbox\" name=\"post_category_name[]\" value=\"Cat1\">Cat1<br>\n   <input type=\"checkbox\" name=\"post_category_name[]\" value=\"Cat2\">Cat2<br>\n   <input type=\"checkbox\" name=\"post_category_name[]\" value=\"Cat3\">Cat3<br>\n   <input type=\"submit\" \/>\n<\/form>\n<\/code><\/pre>\n<p>Remember: do NOT use [capture-form-to-post] if your form is a CF7 or FSCF form.<\/p>\n<p><strong>Advanced:<\/strong><\/p>\n<p>There are many more parameters to wp_insert_post that can be set simply by putting a form fields in your form<br \/>\nof the same name as the wp_insert_post parameter. Examples are:<\/p>\n<ul>\n<li>post_status which will be set to \u2018publish\u2019 by default making the post published automatically. But you could set that in a hidden field to \u2018draft\u2019, \u2018publish\u2019, \u2018pending\u2019, \u2018future\u2019, \u2018private\u2019<\/li>\n<li>comment_status which can be \u2018closed\u2019 or \u2018open\u2019<\/li>\n<li>post_excerpt<\/li>\n<li>post_date (format: <a href=\"http:\/\/php.net\/manual\/en\/function.date.php\" rel=\"nofollow ugc\">Y-m-d H:i:s<\/a>, e.g. \u201c2012-01-01 15:30:00\u201d)<\/li>\n<li>And many more, see <a href=\"https:\/\/codex.wordpress.org\/Function_Reference\/wp_insert_post\" rel=\"nofollow ugc\">wp_insert_post function<\/a><\/li>\n<li>page_template: set to the name of a page template file (e.g. \u201cnew_template.php\u201d).<\/li>\n<li>NOTE: tax_input is NOT supported.<\/li>\n<li>NOTE: If you would want to edit a form, you would need to get the post\u2019s ID and put it in a form \u2018ID\u2019 field.<\/li>\n<\/ul>\n<p><strong>Setting Post Meta (Custom Fields):<\/strong><\/p>\n<p>You can optionally set \u201cpost meta\u201d (custom field) key-value pairs on your post. To do this, add fields to your form whose name start<br \/>\nwith \u201cmeta_\u201d. For example, if you want to set the post meta key \u201cmy_key\u201d then create a form field named \u201cmeta_my_key\u201d.<br \/>\nThe \u201cmeta_\u201d prefix is used to identify the field as a post meta field and the \u201cmeta_\u201d gets stripped off. Then a call<br \/>\nto <a href=\"https:\/\/codex.wordpress.org\/Function_Reference\/update_post_meta\" rel=\"nofollow ugc\">update_post_meta<\/a><br \/>\nis made give that key and the field\u2019s value.<\/p>\n<p><strong>Setting Values on the Server Side Using a Filter:<\/strong><\/p>\n<p>Example Situation 1: you want to set the post_status set to \u2018pending\u2019 so that you can review posts before they are published.<br \/>\nHowever, you don\u2019t want to put that as a field in your form because you are concerned about a hacker changing its value \u2018published\u2019.<br \/>\nYou want to control that on the server side, not in the form in the user\u2019s browser.<\/p>\n<p>Example Situation 2: you want to apply some logic about whether a post is automatically published (perhaps based on the user\u2019s login).<\/p>\n<p>To address these situations, Form To Post provides a WordPress filter where you can add PHP code.<\/p>\n<ul>\n<li>The filter name is <code>form_to_post_before_create_post<\/code><\/li>\n<li>A filter function takes 1 array parameter, which will be the $post array passed to the<br \/>\n<a href=\"https:\/\/codex.wordpress.org\/Function_Reference\/wp_insert_post\" rel=\"nofollow ugc\">wp_insert_post function<\/a><\/li>\n<\/ul>\n<p>Example Situation 1 Solution:<br \/>\nIn your theme or using<br \/>\n<a href=\"https:\/\/wordpress.org\/plugins\/add-actions-and-filters\/\" rel=\"ugc\">Add Shortcodes Actions And Filters<\/a><br \/>\nplugin, add a filter function and register it to the<br \/>\nform_to_post_before_create_post hook.<\/p>\n<pre><code>function form_to_post_set_values($post) {\n    $post['post_status'] = 'pending';\n    return $post;\n}\n\nadd_filter('form_to_post_before_create_post', 'form_to_post_set_values');\n<\/code><\/pre>\n<p><strong>Setting Custom Fields Server Side Using a Filter:<\/strong><\/p>\n<p>You will need a different hook to set custom fields. Here is an example.<\/p>\n<pre><code>function form_to_post_set_meta($post) {\n    $post['order-status'] = 'Order received';\n    return $post;\n}\n\nadd_filter('form_to_post_before_update_post_meta', 'form_to_post_set_meta');\n<\/code><\/pre>\n<p>Notice: you do <em>not<\/em> prefix the custom fields values with \u201cmeta_\u201d in this case.<br \/>\nOnly do that when you are setting them in the form itself.<\/p>\n<p><strong>Dynamically Manipulating the Form Data After Submission<\/strong><\/p>\n<p>An advanced application is to manipulate the raw form data just before the plugin uses it.<br \/>\nThe hook form_to_post_form_data allows this. Functions subscribing to this hook take one argument<br \/>\nwhich is the form data. The data structure is the same as that in <a href=\"http:\/\/cfdbplugin.com\/?page_id=646\" rel=\"nofollow ugc\">CFDB Plugin<\/a><br \/>\n(another one of my plugins).<\/p>\n<p>Example: you don\u2019t have a \u201cpost-content\u201d or \u201cpost-title\u201d field in your form, but you want to set those values as a<br \/>\ncombination of values from other form fields. In this simply example, our form has field \u201cyour-name\u201d and \u201cyour-email\u201d.<br \/>\nThe hook is used to create the \u201cpost-content\u201d or \u201cpost-title\u201d fields to be text with the value of fields<br \/>\n\u201cyour-name\u201d and \u201cyour-email\u201d embedded in them.<\/p>\n<pre><code>function f2p_consolidate_fields( $form_data ) { \/\/ change the function name as needed\n    $form_title = 'F2P With Hook Example'; \/\/ Change this to your form name\n    if ($form_data->title = $form_title) {\n\n        \/\/ Next line sets the post_title value\n        $form_data->posted_data['post_title'] = \"Post from {$form_data->posted_data['your-name']}\";\n\n        \/\/ Next line sets the post_content value\n        $form_data->posted_data['post_content'] =\n            \"This is a post from {$form_data->posted_data['your-name']} with email {$form_data->posted_data['your-email']}\";\n    }\n    return $form_data;\n}\nadd_filter( 'form_to_post_form_data', 'f2p_consolidate_fields', 10, 1 ); \/\/ make sure the function name matches above\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Create a WP Post from a Form Submission.<\/p>\n","protected":false},"author":65,"featured_media":250234,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"Form to Post - \u8587\u6653\u6735\u63d2\u4ef6\u76ee\u5f55","description":"Create a WP Post from a Form Submission."},"footnotes":""},"categories":[1],"tags":[],"class_list":["post-73851","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.weixiaoduo.com\/plugins\/wp-json\/wp\/v2\/posts\/73851","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.weixiaoduo.com\/plugins\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.weixiaoduo.com\/plugins\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.weixiaoduo.com\/plugins\/wp-json\/wp\/v2\/users\/65"}],"replies":[{"embeddable":true,"href":"https:\/\/www.weixiaoduo.com\/plugins\/wp-json\/wp\/v2\/comments?post=73851"}],"version-history":[{"count":0,"href":"https:\/\/www.weixiaoduo.com\/plugins\/wp-json\/wp\/v2\/posts\/73851\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.weixiaoduo.com\/plugins\/wp-json\/wp\/v2\/media\/250234"}],"wp:attachment":[{"href":"https:\/\/www.weixiaoduo.com\/plugins\/wp-json\/wp\/v2\/media?parent=73851"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.weixiaoduo.com\/plugins\/wp-json\/wp\/v2\/categories?post=73851"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.weixiaoduo.com\/plugins\/wp-json\/wp\/v2\/tags?post=73851"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}