{"id":457,"date":"2025-04-30T11:39:14","date_gmt":"2025-04-30T10:39:14","guid":{"rendered":"https:\/\/userswp.io\/documentation\/?post_type=gd_place&#038;p=457"},"modified":"2025-04-30T11:41:26","modified_gmt":"2025-04-30T10:41:26","slug":"basic-invite-code-example","status":"publish","type":"gd_place","link":"https:\/\/userswp.io\/documentation\/article\/how-tos\/basic-invite-code-example\/","title":{"rendered":"Basic Invite Code Example"},"content":{"rendered":"\n<p>We had a user who was using a custom text field with the HTML5 validation as a sort of invite code system.  The issue was that bots can easily bypass HTML5 validation, so some server-side validation is also needed. This is a short tutorial on how to implement custom server side validation for a custom field.<\/p>\n\n\n\n<h2 class=\"wp-block-blockstrap-blockstrap-widget-heading mb-3\">First, Add a Custom Text Field <\/h2>\n\n\n\n<p>Go to UsersWP &gt; Form Builder &gt; Add a text field.<br>Make sure you note the &#8220;Key&#8221; in this case &#8220;invite_code&#8221;.<\/p>\n\n\n\n<p>Here, I have not added any HTML5 validation as it is not required if we are doing server-side validation.<\/p>\n\n\n<figure class=\"  embed-has-action overflow-hidden mb-3 col-12 col-lg-8 \" style=\" \"><img loading=\"lazy\" decoding=\"async\" width=\"479\" height=\"867\" src=\"https:\/\/userswp.io\/documentation\/wp-content\/uploads\/2025\/04\/invite-code-field-settings.png\" class=\"mw-100 w-100 shadow embed-item-cover-xy \" alt=\"\" srcset=\"https:\/\/userswp.io\/documentation\/wp-content\/uploads\/2025\/04\/invite-code-field-settings.png 479w, https:\/\/userswp.io\/documentation\/wp-content\/uploads\/2025\/04\/invite-code-field-settings-166x300.png 166w\" sizes=\"auto, (max-width: 479px) 100vw, 479px\" \/><\/figure>\n\n\n<h2 class=\"wp-block-blockstrap-blockstrap-widget-heading mb-3\">Add the field to the register form<\/h2>\n\n\n\n<p>Above, I have set this to &#8220;Include this field ONLY in the register form&#8221;.<\/p>\n\n\n\n<p>Go to UsersWP &gt; Form Builder &gt; Register &gt; Add the new field to the register form and drag it where you want it to show, I have added it as the first field.<\/p>\n\n\n<figure class=\"  embed-has-action overflow-hidden mb-3 \" style=\" \"><img loading=\"lazy\" decoding=\"async\" width=\"704\" height=\"239\" src=\"https:\/\/userswp.io\/documentation\/wp-content\/uploads\/2025\/04\/invite-code-register-form.png\" class=\"mw-100 w-100 shadow embed-item-cover-xy \" alt=\"invite-code-register-form\" srcset=\"https:\/\/userswp.io\/documentation\/wp-content\/uploads\/2025\/04\/invite-code-register-form.png 704w, https:\/\/userswp.io\/documentation\/wp-content\/uploads\/2025\/04\/invite-code-register-form-300x102.png 300w\" sizes=\"auto, (max-width: 704px) 100vw, 704px\" \/><\/figure>\n\n\n<h1 class=\"wp-block-blockstrap-blockstrap-widget-heading mb-3\">Add the server-side validation<\/h1>\n\n\n\n<p>Below we add a code snippet to validate the invite code, note that the &#8220;invite_code&#8221; key used has to match the field Key.  We have a list of valid invite codes, this list can be as long or short as needed.<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-php\">add_filter(&#039;uwp_validate_fields_before&#039;,&#039;_my_invite_code_check&#039;,10,3);\nfunction _my_invite_code_check( $errors, $data, $type ){\n\t\n\t\/\/ List of valid codes\n\t$valid_codes = array(\n\t\t&#039;SAMPLE123&#039;,\n\t\t&#039;CAT345&#039;,\n\t\t&#039;BOB99&#039;,\n\t\t&#039;123456&#039;,\n\t\t&#039;POOL&#039;\n\t);\n\t\n\n\t\/\/ only run chcks if we have data and its a register call\n\tif (!empty($data) &amp;&amp; $type === &#039;register&#039;) {\n\t\t\n\t\t\/\/ if empty add an error\n\t\tif(empty($data[&#039;invite_code&#039;])){\n\t\t\t $errors-&gt;add(&#039;not_logged_in&#039;, __(&#039;&lt;strong&gt;Error&lt;\/strong&gt;: Invite Code Empty&#039;, &#039;userswp&#039;));\n\t\t}elseif(!in_array($data[&#039;invite_code&#039;],$valid_codes)){\n\t\t\t\/\/ if used code is not in our list then add an error\n\t\t\t$errors-&gt;add(&#039;not_logged_in&#039;, __(&#039;&lt;strong&gt;Error&lt;\/strong&gt;: Invite Code Not Valid&#039;, &#039;userswp&#039;));\n\t\t}\n\t\t\n\t}\n\t\n\treturn $errors;\n}<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"wp-block-blockstrap-blockstrap-widget-alert d-flex align-items-center fade show alert alert-info mb-3\" role=\"alert\"><span class=\"fas fa-info-circle me-2\"><\/span><span class=\"flex-grow-1\">We recommend using the <a href=\"https:\/\/wpgeodirectory.com\/documentation\/article\/useful-plugins\/code-snippets-plugin\/\" target=\"_blank\">Code Snippets<\/a> plugin to add snippets to your site.<\/span><\/div>\n\n\n\n<h1 class=\"wp-block-blockstrap-blockstrap-widget-heading mb-3\">Result<\/h1>\n\n\n\n<p>Users will only be able to register if they have a valid invite code.<\/p>\n\n\n<figure class=\"  embed-has-action overflow-hidden mb-3 \" style=\" \"><img loading=\"lazy\" decoding=\"async\" width=\"711\" height=\"204\" src=\"https:\/\/userswp.io\/documentation\/wp-content\/uploads\/2025\/04\/Screenshot-from-2025-04-30-11-37-33.png\" class=\"mw-100 w-100 shadow embed-item-cover-xy \" alt=\"\" srcset=\"https:\/\/userswp.io\/documentation\/wp-content\/uploads\/2025\/04\/Screenshot-from-2025-04-30-11-37-33.png 711w, https:\/\/userswp.io\/documentation\/wp-content\/uploads\/2025\/04\/Screenshot-from-2025-04-30-11-37-33-300x86.png 300w\" sizes=\"auto, (max-width: 711px) 100vw, 711px\" \/><\/figure>","protected":false},"excerpt":{"rendered":"<p>We had a user who was using a custom text field with the HTML5 validation as a sort of invite code system. The issue was that bots can easily bypass HTML5 validation, so some server-side validation is also needed. This is a short tutorial on how to implement custom server side validation for a custom [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","template":"","meta":{"footnotes":""},"article\/tags":[],"article\/categories":[41,7],"class_list":["post-457","gd_place","type-gd_place","status-publish","hentry","gd_placecategory-how-tos","gd_placecategory-tips-tricks-more"],"_links":{"self":[{"href":"https:\/\/userswp.io\/documentation\/wp-json\/wp\/v2\/article\/457","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/userswp.io\/documentation\/wp-json\/wp\/v2\/article"}],"about":[{"href":"https:\/\/userswp.io\/documentation\/wp-json\/wp\/v2\/types\/gd_place"}],"author":[{"embeddable":true,"href":"https:\/\/userswp.io\/documentation\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/userswp.io\/documentation\/wp-json\/wp\/v2\/comments?post=457"}],"version-history":[{"count":0,"href":"https:\/\/userswp.io\/documentation\/wp-json\/wp\/v2\/article\/457\/revisions"}],"wp:attachment":[{"href":"https:\/\/userswp.io\/documentation\/wp-json\/wp\/v2\/media?parent=457"}],"wp:term":[{"taxonomy":"gd_place_tags","embeddable":true,"href":"https:\/\/userswp.io\/documentation\/wp-json\/wp\/v2\/article\/tags?post=457"},{"taxonomy":"gd_placecategory","embeddable":true,"href":"https:\/\/userswp.io\/documentation\/wp-json\/wp\/v2\/article\/categories?post=457"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}