{"id":142,"date":"2020-05-01T10:02:21","date_gmt":"2020-05-01T10:02:21","guid":{"rendered":"https:\/\/codexcoach.com\/?p=68"},"modified":"2023-01-30T07:42:05","modified_gmt":"2023-01-30T07:42:05","slug":"wordpress-forgot-password-form-without-plugin","status":"publish","type":"post","link":"https:\/\/codexcoach.com\/wordpress-forgot-password-form-without-plugin\/","title":{"rendered":"WordPress Forgot Password Form Without Plugin"},"content":{"rendered":"\n<p>Are you need simple Forgot Password form without plugin for your WordPress website? Yes, Then i have created <strong>[cxc_forgot_pwd_form]<\/strong> shortcode with quick simple code. So you can put Forgot Password form anywhere like in Pages, Posts and Widget etc\u2026 Therefore you just need to put <strong>[cxc_forgot_pwd_form]<\/strong> shortcode where you need login form. So you need to follow below steps.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-twentytwentyone-border\"><img loading=\"lazy\" width=\"1210\" height=\"248\" src=\"https:\/\/codexcoach.com\/app\/uploads\/2022\/12\/screenshot-webbydemo.in-2022.11.29-16_07_53.png\" alt=\"\" class=\"wp-image-1372\" srcset=\"https:\/\/codexcoach.com\/app\/uploads\/2022\/12\/screenshot-webbydemo.in-2022.11.29-16_07_53.png 1210w, https:\/\/codexcoach.com\/app\/uploads\/2022\/12\/screenshot-webbydemo.in-2022.11.29-16_07_53-300x61.png 300w, https:\/\/codexcoach.com\/app\/uploads\/2022\/12\/screenshot-webbydemo.in-2022.11.29-16_07_53-1024x210.png 1024w, https:\/\/codexcoach.com\/app\/uploads\/2022\/12\/screenshot-webbydemo.in-2022.11.29-16_07_53-768x157.png 768w\" sizes=\"(max-width: 1210px) 100vw, 1210px\" \/><\/figure>\n\n\n\n<h2>Code for shortcode.<\/h2>\n\n\n\n<p>You need to put below code in theme&#8217;s <strong>functions.php<\/strong> file. If you have created child theme then add this in function file of child theme.<\/p>\n\n\n\n<h2><strong>Forgot Password Form | Shortcode: [cxc_forgot_pwd_form]<\/strong><\/h2>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-file=\"functions.php\" data-lang=\"PHP\"><code>&lt;?php\n add_shortcode( &#39;cxc_forgot_pwd_form&#39;, &#39;cxc_forgot_pwd_form_callback&#39; );\n\nfunction cxc_forgot_pwd_form_callback() {\n\tob_start();\n\n\tif ( !is_user_logged_in() ) {\n\n\t\tglobal $getPasswordError, $getPasswordSuccess;\n\n\t\tif ( !empty( $getPasswordError ) ) {\n\t\t\t?&gt;\n\t\t\t&lt;div class=&quot;alert alert-danger&quot;&gt;\n\t\t\t\t&lt;?php echo $getPasswordError; ?&gt;\n\t\t\t&lt;\/div&gt;\n\t\t&lt;?php } ?&gt;\n\n\t\t&lt;?php if ( !empty( $getPasswordSuccess ) ) { ?&gt;\n\t\t\t&lt;br\/&gt;\n\t\t\t&lt;div class=&quot;alert alert-success&quot;&gt;\n\t\t\t\t&lt;?php echo $getPasswordSuccess; ?&gt;\n\t\t\t&lt;\/div&gt;\n\t\t&lt;?php } ?&gt;\n\n\t\t&lt;form method=&quot;post&quot; class=&quot;wc-forgot-pwd-form&quot;&gt;\n\t\t\t&lt;div class=&quot;forgot_pwd_form&quot;&gt;\n\t\t\t\t&lt;div class=&quot;log_user&quot;&gt;\n\t\t\t\t\t&lt;label for=&quot;user_login&quot;&gt;Username or E-mail:&lt;\/label&gt;\n\t\t\t\t\t&lt;?php $user_login = isset($_POST[&#39;user_login&#39;]) ? $_POST[&#39;user_login&#39;] : &#39;&#39;; ?&gt;\n\t\t\t\t\t&lt;input type=&quot;text&quot; name=&quot;user_login&quot; id=&quot;user_login&quot; value=&quot;&lt;?php echo $user_login; ?&gt;&quot; \/&gt;\n\t\t\t\t&lt;\/div&gt;\n\t\t\t\t&lt;div class=&quot;log_user&quot;&gt;\n\t\t\t\t\t&lt;?php\n\t\t\t\t\tob_start();\n\t\t\t\t\tdo_action( &#39;lostpassword_form&#39; );\n\t\t\t\t\techo ob_get_clean();\n\t\t\t\t\t?&gt;\n\t\t\t\t\t&lt;?php wp_nonce_field(&#39;userGetPassword&#39;, &#39;formType&#39;); ?&gt;\n\t\t\t\t\t&lt;button type=&quot;submit&quot; class=&quot;get_new_password&quot;&gt;Get New Password&lt;\/button&gt;\n\t\t\t\t&lt;\/div&gt;\n\t\t\t&lt;\/div&gt;\n\t\t&lt;\/form&gt;\n\t\t&lt;?php\n\t}\n\n\t$forgot_pwd_form = ob_get_clean();\n\treturn $forgot_pwd_form;\n}\n?&gt;<\/code><\/pre><\/div>\n\n\n\n<h2>Code for Forgot Password<\/h2>\n\n\n\n<p>In this code i am validating form and Send mail for Forgot Password on <strong>wp<\/strong> action. Add it in <strong>functions.php<\/strong> file after above code. If this code will not work then change action <strong>wp<\/strong> to <strong>init<\/strong>.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-file=\"functions.php\" data-lang=\"PHP\"><code>&lt;?php\nadd_action( &#39;wp&#39;, &#39;cxc_user_forgot_pwd_callback&#39; );\n\nfunction cxc_user_forgot_pwd_callback() {\n\tif ( isset( $_POST[&#39;formType&#39;] ) && wp_verify_nonce( $_POST[&#39;formType&#39;], &#39;userGetPassword&#39; ) ) {\n\n\t\tglobal $getPasswordError, $getPasswordSuccess;\n\n\t\t$email = trim( $_POST[&#39;user_login&#39;] );\n\n\t\tif ( empty( $email ) ) {\n\t\t\t$getPasswordError = &#39;&lt;strong&gt;Error! &lt;\/strong&gt;Enter a e-mail address.&#39;;\n\t\t} else if ( !is_email( $email ) ) {\n\t\t\t$getPasswordError = &#39;&lt;strong&gt;Error! &lt;\/strong&gt;Invalid e-mail address.&#39;;\n\t\t} else if ( !email_exists( $email ) ) {\n\t\t\t$getPasswordError = &#39;&lt;strong&gt;Error! &lt;\/strong&gt;There is no user registered with that email address.&#39;;\n\t\t} else {\n\n              \/\/ lets generate our new password\n\t\t\t$random_password = wp_generate_password( 12, false );\n\n              \/\/ Get user data by field and data, other field are ID, slug, slug and login\n\t\t\t$user = get_user_by( &#39;email&#39;, $email );\n\n\t\t\t$update_user = wp_update_user( array(\n\t\t\t\t&#39;ID&#39; =&gt; $user-&gt;ID,\n\t\t\t\t&#39;user_pass&#39; =&gt; $random_password\n\t\t\t) );\n\n              \/\/ if  update user return true then lets send user an email containing the new password\n\t\t\tif ( $update_user ) {\n\t\t\t\t$to = $email;\n\t\t\t\t$subject = &#39;Your new password&#39;;\n\t\t\t\t$sender = get_bloginfo( &#39;name&#39; );\n\n\t\t\t\t$message = &#39;Your new password is: &#39; . $random_password;\n\n                  \/* $headers[] = &#39;MIME-Version: 1.0&#39; . &quot;\\r\\n&quot;;\n                    $headers[] = &#39;Content-type: text\/html; charset=iso-8859-1&#39; . &quot;\\r\\n&quot;;\n                    $headers[] = &quot;X-Mailer: PHP \\r\\n&quot;;\n                    $headers[] = &#39;From: &#39; . $sender . &#39; &lt; &#39; . $email . &#39;&gt;&#39; . &quot;\\r\\n&quot;; *\/\n                    $headers = array( &#39;Content-Type: text\/html; charset=UTF-8&#39; );\n\n                    $mail = wp_mail( $to, $subject, $message, $headers );\n                    if ( $mail ) {\n                    \t$getPasswordSuccess = &#39;&lt;strong&gt;Success! &lt;\/strong&gt;Check your email address for you new password.&#39;;\n                    }\n                } else {\n                \t$getPasswordError = &#39;&lt;strong&gt;Error! &lt;\/strong&gt;Oops something went wrong.&#39;;\n                }\n            }\n        }\n    }\n?&gt;<\/code><\/pre><\/div>\n<div id=\"ezoic-pub-ad-placeholder-118\"><\/div>","protected":false},"excerpt":{"rendered":"<p>Are you need simple Forgot Password form without plugin for your WordPress website? Yes, Then i have created [cxc_forgot_pwd_form] shortcode with quick simple code. So you can put Forgot Password form anywhere like in Pages, Posts and Widget etc\u2026 Therefore you just need to put [cxc_forgot_pwd_form] shortcode where you need login form. So you need [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":2513,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_aib_schema_json_ld":""},"categories":[10],"tags":[],"_links":{"self":[{"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/posts\/142"}],"collection":[{"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/comments?post=142"}],"version-history":[{"count":5,"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/posts\/142\/revisions"}],"predecessor-version":[{"id":1970,"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/posts\/142\/revisions\/1970"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/media\/2513"}],"wp:attachment":[{"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/media?parent=142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/categories?post=142"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/tags?post=142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}