{"id":1622635,"date":"2020-05-13T14:24:10","date_gmt":"2020-05-13T14:24:10","guid":{"rendered":"https:\/\/toolset.com\/?post_type=course-lesson&#038;p=1622635"},"modified":"2021-01-19T07:06:46","modified_gmt":"2021-01-19T07:06:46","slug":"extending-form-functionality-with-hooks","status":"publish","type":"course-lesson","link":"https:\/\/toolset.com\/fr\/course-lesson\/extending-form-functionality-with-hooks\/","title":{"rendered":"Extending Form Functionality with Hooks"},"content":{"rendered":"\n<p>Toolset Forms API allows you to hook into different events of submitting a form. For example, you can add a custom action right before saving or processing any form data, but after the form was validated. <\/p>\n\n\n\n<p>Or, you could add a custom type of an event that triggers notifications. The possibilities are limitless.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>To use the Toolset Forms API, you will need a good knowledge of PHP.<\/p><\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">Adding custom PHP from the WordPress admin<\/h2>\n\n\n\n<p>Toolset allows you to add any custom PHP code directly from the WordPress admin. This is great because you don&#8217;t have to create a child theme to add custom PHP code. You also don&#8217;t need to edit any PHP files.<\/p>\n\n\n\n<p>Use the following steps to add custom PHP code to your site:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Go to the <strong>Toolset<\/strong> \u2192 <strong>Settings<\/strong> page and click the <strong>Custom Code<\/strong> tab.<\/li><li>To add a code snippet, click the <strong>Add new<\/strong> button, type in the name (slug), and click <strong>Create<\/strong>.<\/li><li>Your snippet is created and listed on the page. Click on its name to edit its options and add code to it.<\/li><\/ol>\n\n\n\n<p>Check out the related page to learn more about <a href=\"https:\/\/toolset.com\/documentation\/programmer-reference\/adding-custom-code\/using-toolset-to-add-custom-code\/\">using Toolset to add custom code<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Examples of using the Forms API<\/h2>\n\n\n\n<p>To help you get started, let&#8217;s take a look at some of the common uses of Forms API.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Create a post when registering a user<\/h3>\n\n\n\n<p>After using a Toolset User form to register a user, you might need to automatically create a custom post for this new user. For example, when someone registers on the site about events, you could create a separate post in the &#8220;attendees&#8221; post type.<\/p>\n\n\n\n<p>To achieve this, you can use the <a href=\"https:\/\/toolset.com\/documentation\/programmer-reference\/cred-api\/#cred_save_data\">cred_save_data<\/a> API hook:<\/p>\n\n\n\n\n                    <div class='codesnipdiv' id='2a886'>\n                    <a class='Create a post when registering a user' data-titleselector='create-a-post-when-registering-a-user' data-randspinner ='2a886' data-sniphost='1622635' title='Click to Expand or Minimize' id='codesnipfrontend'><img src='https:\/\/toolset.com\/wp-content\/plugins\/code-snips\/res\/img\/code_icon.png' \/>Create a post when registering a user<\/a>\n                    <div id='2a886_innerdiv' class='clearfix'>\n                    <pre class='brush:php;'>add_action(&#039;cred_save_data&#039;, &#039;my_save_data_action&#039;,10,2);\r\nfunction my_save_data_action($post_id, $form_data) {\r\n    if ($form_data[&#039;id&#039;]==9999) {     \/\/ replace with your form&#039;s ID\r\n    {  \r\n         \/\/ Create new entry after registration.\r\n         $my_post = array(\r\n             &#039;post_title&#039;\t=&gt; &#039;Attendee&#039;,  \/\/ adjust the title\r\n             &#039;post_content&#039;  =&gt; &#039;&#039;,\r\n             &#039;post_status&#039;   =&gt; &#039;publish&#039;,\r\n             &#039;post_author&#039;   =&gt; 1,  \/\/ adjust post_author if\/as required\r\n             &#039;post_type&#039; =&gt; &#039;attendees&#039;   \/\/ replace with the slug of your post type\r\n    );\r\n       \r\n    \/\/ Insert the post into the database\r\n    wp_insert_post( $my_post );\r\n    }\r\n}<\/pre>\n                    <script>\n                    if ('undefined' === typeof cacheObj_open) {\n                        window.cacheObj_open = {};\n                    }\n                    SyntaxHighlighter.highlight();\n                    <\/script>\n                    <input type='hidden' id='2a886_minimize' value='Minimize'>\n                    <input type='hidden' id='2a886_cachedstartopen' value='cachedstartopen'>\n                    <\/div>\n                    <\/div>\t\t\t\n                    <script>\t\t\t\t\t\t\t\n                    var insertname_outputs_startopen\t= '2a886_create-a-post-when-registering-a-user';\t \t\t\t\n                    cacheObj_open[insertname_outputs_startopen]='<pre class=\"brush: php;\">add_action(&#039;cred_save_data&#039;, &#039;my_save_data_action&#039;,10,2);\\r\\nfunction my_save_data_action($post_id, $form_data) {\\r\\n    if ($form_data[&#039;id&#039;]==9999) {     \\\/\\\/ replace with your form&#039;s ID\\r\\n    {  \\r\\n         \\\/\\\/ Create new entry after registration.\\r\\n         $my_post = array(\\r\\n             &#039;post_title&#039;\\t=&gt; &#039;Attendee&#039;,  \\\/\\\/ adjust the title\\r\\n             &#039;post_content&#039;  =&gt; &#039;&#039;,\\r\\n             &#039;post_status&#039;   =&gt; &#039;publish&#039;,\\r\\n             &#039;post_author&#039;   =&gt; 1,  \\\/\\\/ adjust post_author if\\\/as required\\r\\n             &#039;post_type&#039; =&gt; &#039;attendees&#039;   \\\/\\\/ replace with the slug of your post type\\r\\n    );\\r\\n       \\r\\n    \\\/\\\/ Insert the post into the database\\r\\n    wp_insert_post( $my_post );\\r\\n    }\\r\\n}<\/pre>';            \n                    <\/script>\n\n\n\n<p>You will need to adjust the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Replace <strong>9999<\/strong> with the ID of your user form for registering users.<\/li><li>Replace the post type name &#8220;attendees&#8221; with the slug of your post type.<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Set a post taxonomy automatically when a post is created<\/h3>\n\n\n\n<p>When a user submits a new post from the front-end, you might want to set a specific, predefined taxonomy for that post. For example, you could have multiple categories but you only want to allow submitting a &#8220;report&#8221; post.<\/p>\n\n\n\n<p>To achieve this, you can use the <a href=\"https:\/\/toolset.com\/documentation\/programmer-reference\/cred-api\/#cred_save_data\">cred_save_data<\/a> API hook in combination with the core <strong>has_term <\/strong>function:<\/p>\n\n\n\n\n                    <div class='codesnipdiv' id='8161c'>\n                    <a class='Set a taxonomy when post is created' data-titleselector='set-a-taxonomy-when-post-is-created' data-randspinner ='8161c' data-sniphost='1622635' title='Click to Expand or Minimize' id='codesnipfrontend'><img src='https:\/\/toolset.com\/wp-content\/plugins\/code-snips\/res\/img\/code_icon.png' \/>Set a taxonomy when post is created<\/a>\n                    <div id='8161c_innerdiv' class='clearfix'>\n                    <pre class='brush:php;'>add_action(&quot;cred_save_data&quot;, &quot;attach_default_terms&quot;,10,2);\r\nfunction attach_default_terms($post_id, $form_data)\r\n {\r\n    \/\/ if a specific form\r\n    if ($form_data[&quot;id&quot;] == 9999) {     \/\/ replace with your form&#039;s ID\r\n        if(!has_term(&#039;&#039;,&#039;category&#039;,$post_ID))\r\n        {\r\n            $category_ids = array(6);  \/\/ replace with your category ID\r\n            wp_set_object_terms( $post_id, $category_ids, &#039;category&#039;);\r\n        }\r\n    wp_update_post( $my_post );\r\n    }\r\n}<\/pre>\n                    <script>\n                    if ('undefined' === typeof cacheObj_open) {\n                        window.cacheObj_open = {};\n                    }\n                    SyntaxHighlighter.highlight();\n                    <\/script>\n                    <input type='hidden' id='8161c_minimize' value='Minimize'>\n                    <input type='hidden' id='8161c_cachedstartopen' value='cachedstartopen'>\n                    <\/div>\n                    <\/div>\t\t\t\n                    <script>\t\t\t\t\t\t\t\n                    var insertname_outputs_startopen\t= '8161c_set-a-taxonomy-when-post-is-created';\t \t\t\t\n                    cacheObj_open[insertname_outputs_startopen]='<pre class=\"brush: php;\">add_action(&quot;cred_save_data&quot;, &quot;attach_default_terms&quot;,10,2);\\r\\nfunction attach_default_terms($post_id, $form_data)\\r\\n {\\r\\n    \\\/\\\/ if a specific form\\r\\n    if ($form_data[&quot;id&quot;] == 9999) {     \\\/\\\/ replace with your form&#039;s ID\\r\\n        if(!has_term(&#039;&#039;,&#039;category&#039;,$post_ID))\\r\\n        {\\r\\n            $category_ids = array(6);  \\\/\\\/ replace with your category ID\\r\\n            wp_set_object_terms( $post_id, $category_ids, &#039;category&#039;);\\r\\n        }\\r\\n    wp_update_post( $my_post );\\r\\n    }\\r\\n}<\/pre>';            \n                    <\/script>\n\n\n\n<p>You will need to adjust the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Replace <strong>9999<\/strong> with the ID of your form.<\/li><li>Change the <strong>$category_ids<\/strong> array to contain the IDs of the taxonomy terms you want to assign to the post created by the form.<\/li><\/ul>\n\n\n\n<p>Additionally, in this example, our form is set to submit standard WordPress posts. This is why we&#8217;re checking for the &#8220;category&#8221; taxonomy using the <strong>has_term <\/strong>function. If you need to check for custom taxonomy, simply replace &#8220;category&#8221; with the slug of your taxonomy.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Connect the submitted post to a predefined parent post<\/h3>\n\n\n\n<p>When a user submits a new post using a form, you might want to automatically connect it to a predefined parent post.<\/p>\n\n\n\n<p>To achieve this, you can use the <strong>toolset_connect_posts function<\/strong>:<\/p>\n\n\n\n\n                    <div class='codesnipdiv' id='b12f8'>\n                    <a class='Connect submitted post to a parent post' data-titleselector='connect-submitted-post-to-a-parent-post' data-randspinner ='b12f8' data-sniphost='1622635' title='Click to Expand or Minimize' id='codesnipfrontend'><img src='https:\/\/toolset.com\/wp-content\/plugins\/code-snips\/res\/img\/code_icon.png' \/>Connect submitted post to a parent post<\/a>\n                    <div id='b12f8_innerdiv' class='clearfix'>\n                    <pre class='brush:php;'>add_action(&#039;cred_save_data&#039;,&#039;func_connect_child_posts&#039;,15,2);\r\nfunction func_connect_child_posts($post_id,$form_data) {\r\n    if ($form_data[&#039;id&#039;]==999999)     \/\/ replace with your form&#039;s ID\r\n    {\r\n    toolset_connect_posts(&#039;relationship-slug&#039;,$_POST[&#039;@relationship-slug.parent&#039;], $post_id);     \/\/ replace with the slug of your actual relationship\r\n    }\r\n}<\/pre>\n                    <script>\n                    if ('undefined' === typeof cacheObj_open) {\n                        window.cacheObj_open = {};\n                    }\n                    SyntaxHighlighter.highlight();\n                    <\/script>\n                    <input type='hidden' id='b12f8_minimize' value='Minimize'>\n                    <input type='hidden' id='b12f8_cachedstartopen' value='cachedstartopen'>\n                    <\/div>\n                    <\/div>\t\t\t\n                    <script>\t\t\t\t\t\t\t\n                    var insertname_outputs_startopen\t= 'b12f8_connect-submitted-post-to-a-parent-post';\t \t\t\t\n                    cacheObj_open[insertname_outputs_startopen]='<pre class=\"brush: php;\">add_action(&#039;cred_save_data&#039;,&#039;func_connect_child_posts&#039;,15,2);\\r\\nfunction func_connect_child_posts($post_id,$form_data) {\\r\\n    if ($form_data[&#039;id&#039;]==999999)     \\\/\\\/ replace with your form&#039;s ID\\r\\n    {\\r\\n    toolset_connect_posts(&#039;relationship-slug&#039;,$_POST[&#039;@relationship-slug.parent&#039;], $post_id);     \\\/\\\/ replace with the slug of your actual relationship\\r\\n    }\\r\\n}<\/pre>';            \n                    <\/script>\n\n\n\n<p>You will need to adjust the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Replace <strong>9999<\/strong> with the ID of your form.<\/li><li>Replace <strong>relationship-slug <\/strong>with the actual slug of your relationship.<\/li><\/ul>\n\n\n\n<p>Finally, edit your form and remove the parent field if it&#8217;s already added. Then, <a href=\"https:\/\/toolset.com\/fr\/course-lesson\/adding-generic-fields-to-forms\/\">add a Generic field<\/a> that will hold the parent post&#8217;s ID.<\/p>\n\n\n\n\n                    <div class='codesnipdiv' id='81c84'>\n                    <a class='Add generic field for the parent post ID' data-titleselector='add-generic-field-for-the-parent-post-id' data-randspinner ='81c84' data-sniphost='1622635' title='Click to Expand or Minimize' id='codesnipfrontend'><img src='https:\/\/toolset.com\/wp-content\/plugins\/code-snips\/res\/img\/code_icon.png' \/>Add generic field for the parent post ID<\/a>\n                    <div id='81c84_innerdiv' class='clearfix'>\n                    <pre class='brush:html;'>[cred_generic_field field=&#039;@relationship-slug.parent&#039; type=&#039;hidden&#039; class=&#039;&#039; urlparam=&#039;&#039;]\r\n{\r\n&quot;required&quot;:0,\r\n&quot;validate_format&quot;:0,\r\n&quot;default&quot;:&quot;[wpv-post-id]&quot;\r\n}\r\n[\/cred_generic_field]<\/pre>\n                    <script>\n                    if ('undefined' === typeof cacheObj_open) {\n                        window.cacheObj_open = {};\n                    }\n                    SyntaxHighlighter.highlight();\n                    <\/script>\n                    <input type='hidden' id='81c84_minimize' value='Minimize'>\n                    <input type='hidden' id='81c84_cachedstartopen' value='cachedstartopen'>\n                    <\/div>\n                    <\/div>\t\t\t\n                    <script>\t\t\t\t\t\t\t\n                    var insertname_outputs_startopen\t= '81c84_add-generic-field-for-the-parent-post-id';\t \t\t\t\n                    cacheObj_open[insertname_outputs_startopen]='<pre class=\"brush: html;\">[cred_generic_field field=&#039;@relationship-slug.parent&#039; type=&#039;hidden&#039; class=&#039;&#039; urlparam=&#039;&#039;]\\r\\n{\\r\\n&quot;required&quot;:0,\\r\\n&quot;validate_format&quot;:0,\\r\\n&quot;default&quot;:&quot;[wpv-post-id]&quot;\\r\\n}\\r\\n[\\\/cred_generic_field]<\/pre>';            \n                    <\/script>\n\n\n\n<p>You will need to adjust the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Replace <strong>relationship-slug <\/strong>with the actual slug of your relationship.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Further documentation<\/h2>\n\n\n\n<p>To add custom functions to Toolset forms you will need the page about <a href=\"https:\/\/toolset.com\/documentation\/programmer-reference\/cred-api\/\">Forms API Hooks<\/a>.<\/p>\n\n\n\n<p>We also recommend reading our comprehensive guide for <a href=\"https:\/\/toolset.com\/documentation\/programmer-reference\/toolset-hooks\/\">getting started with Toolset hooks<\/a>.<\/p>\n","protected":false},"author":4341,"featured_media":1631633,"template":"","related-lesson":[8429],"class_list":["post-1622635","course-lesson","type-course-lesson","status-publish","has-post-thumbnail","hentry","related-lesson-post-forms"],"_links":{"self":[{"href":"https:\/\/toolset.com\/fr\/wp-json\/wp\/v2\/course-lesson\/1622635","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/toolset.com\/fr\/wp-json\/wp\/v2\/course-lesson"}],"about":[{"href":"https:\/\/toolset.com\/fr\/wp-json\/wp\/v2\/types\/course-lesson"}],"author":[{"embeddable":true,"href":"https:\/\/toolset.com\/fr\/wp-json\/wp\/v2\/users\/4341"}],"version-history":[{"count":5,"href":"https:\/\/toolset.com\/fr\/wp-json\/wp\/v2\/course-lesson\/1622635\/revisions"}],"predecessor-version":[{"id":1910199,"href":"https:\/\/toolset.com\/fr\/wp-json\/wp\/v2\/course-lesson\/1622635\/revisions\/1910199"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/toolset.com\/fr\/wp-json\/wp\/v2\/media\/1631633"}],"wp:attachment":[{"href":"https:\/\/toolset.com\/fr\/wp-json\/wp\/v2\/media?parent=1622635"}],"wp:term":[{"taxonomy":"related-lesson","embeddable":true,"href":"https:\/\/toolset.com\/fr\/wp-json\/wp\/v2\/related-lesson?post=1622635"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}