{"id":30,"date":"2020-04-30T06:42:43","date_gmt":"2020-04-30T01:12:43","guid":{"rendered":"https:\/\/codexcoach.com\/?p=30"},"modified":"2022-12-31T05:46:40","modified_gmt":"2022-12-31T05:46:40","slug":"wordpress-login-form-without-plugin","status":"publish","type":"post","link":"https:\/\/codexcoach.com\/wordpress-login-form-without-plugin\/","title":{"rendered":"How to create a login form in wordpress without plugin"},"content":{"rendered":"\n<p>Are you need simple login form without plugin for your WordPress website? Yes, Then i have created <strong>[cxc_login_form]<\/strong> shortcode with quick simple code. So you can put login form anywhere like in Pages, Posts and Widget etc\u2026 Therefore you just need to put <strong>[cxc_login_form]<\/strong> shortcode where you need login form. So you need to follow below steps.<\/p>\n\n\n\n<h2>Code for shortcode.<\/h2>\n\n\n\n<p>You need to put below code in theme\u2019s <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<figure class=\"wp-block-image size-full is-resized is-style-twentytwentyone-border\"><img loading=\"lazy\" src=\"https:\/\/codexcoach.com\/app\/uploads\/2020\/04\/screenshot-webbydemo.in-2022.12.26-14_55_22-e1672046813253.png\" alt=\"Login form\" class=\"wp-image-1896\" width=\"699\" height=\"349\" srcset=\"https:\/\/codexcoach.com\/app\/uploads\/2020\/04\/screenshot-webbydemo.in-2022.12.26-14_55_22-e1672046813253.png 699w, https:\/\/codexcoach.com\/app\/uploads\/2020\/04\/screenshot-webbydemo.in-2022.12.26-14_55_22-e1672046813253-300x150.png 300w\" sizes=\"(max-width: 699px) 100vw, 699px\" \/><\/figure>\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_shortcode( &#39;cxc_login_form&#39;, &#39;cxc_login_form_callback&#39; );\n\nfunction cxc_login_form_callback() {\n\tob_start();\n\n\tif ( !is_user_logged_in() ) {\n\n\t\tglobal $errors_login;\n\n\t\tif (!empty( $errors_login ) ) {\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 $errors_login; ?&gt;\n\t\t\t&lt;\/div&gt;\n\t\t&lt;?php } ?&gt;\n\t\t&lt;form method=&quot;post&quot; class=&quot;wc-login-form&quot;&gt;\n\t\t\t&lt;div class=&quot;login_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_name&quot;&gt;Username&lt;\/label&gt;\n\t\t\t\t\t&lt;input name=&quot;log&quot; type=&quot;text&quot; id=&quot;user_name&quot; value=&quot;&lt;?php echo isset($_POST[&#39;log&#39;]) ? $_POST[&#39;log&#39;] : &#39;&#39;; ?&gt;&quot;&gt;\n\t\t\t\t&lt;\/div&gt;\n\t\t\t\t&lt;div class=&quot;log_pass&quot;&gt;\n\t\t\t\t\t&lt;label for=&quot;user_password&quot;&gt;Password&lt;\/label&gt;\n\t\t\t\t\t&lt;input name=&quot;pwd&quot; id=&quot;user_password&quot; type=&quot;password&quot;&gt;\n\t\t\t\t&lt;\/div&gt;\n\t\t\t\t&lt;?php\n\t\t\t\tob_start();\n\t\t\t\tdo_action( &#39;login_form&#39; );\n\t\t\t\techo ob_get_clean();\n\t\t\t\t?&gt;\n\t\t\t\t&lt;?php wp_nonce_field( &#39;userLogin&#39;, &#39;formType&#39; ); ?&gt;\n\t\t\t&lt;\/div&gt;\n\t\t\t&lt;button type=&quot;submit&quot;&gt;LOG IN&lt;\/button&gt;\n\t\t&lt;\/form&gt;\n\t\t&lt;?php\n\t} else {\n\t\techo &#39;&lt;p class=&quot;error-logged&quot;&gt;You are already logged in.&lt;\/p&gt;&#39;;\n\t}\n\n\t$login_form = ob_get_clean();\n\treturn $login_form;\n}\n?&gt;<\/code><\/pre><\/div>\n\n\n\n<h2>Code for Login.<\/h2>\n\n\n\n<p>In this code i am validating form and log in WordPress 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_login_callback&#39; );\n\nfunction cxc_user_login_callback() {\n\n\tif ( isset( $_POST[&#39;formType&#39;] ) && wp_verify_nonce( $_POST[&#39;formType&#39;], &#39;userLogin&#39; ) ) {\n\t\tglobal $errors_login;\n\t\t$uName = $_POST[&#39;log&#39;];\n\t\t$uPassword = $_POST[&#39;pwd&#39;];\t\t\n\n\t\tif ($uName == &#39;&#39; && $uPassword != &#39;&#39;) {\n\t\t\t$errors_login = &#39;&lt;strong&gt;Error! &lt;\/strong&gt; Username is required.&#39;;\n\t\t} elseif ($uName != &#39;&#39; && $uPassword == &#39;&#39;) {\n\t\t\t$errors_login = &#39;&lt;strong&gt;Error! &lt;\/strong&gt; Password is required.&#39;;\n\t\t} elseif ($uName == &#39;&#39; && $uPassword == &#39;&#39;) {\n\t\t\t$errors_login = &#39;&lt;strong&gt;Error! &lt;\/strong&gt; Username & Password are required.&#39;;\n\t\t} elseif ($uName != &#39;&#39; && $uPassword != &#39;&#39;) {\n\t\t\t$creds = array();\n\t\t\t$creds[&#39;user_login&#39;] = $uName;\n\t\t\t$creds[&#39;user_password&#39;] = $uPassword;\n\t\t\t$creds[&#39;remember&#39;] = false;\n\t\t\t$user = wp_signon( $creds, false );\n\t\t\tif ( is_wp_error($user) ) {\n\t\t\t\t$errors_login = $user-&gt;get_error_message();\n\t\t\t} else {\t\t\t\t\n\t\t\t\twp_set_current_user( $user-&gt;ID );\n\t\t\t\twp_set_auth_cookie( $user-&gt;ID );\n\t\t\t\tdo_action( &#39;wp_login&#39;, $user-&gt;user_login, $user );\n\t\t\t\twp_redirect( site_url() );\n\t\t\t\texit;\n\t\t\t}\n\t\t}\n\t}\n}\n?&gt;<\/code><\/pre><\/div>\n\n\n\n<h2>WordPress Default Login Form.<\/h2>\n\n\n\n<p>Provides a simple login form for use anywhere within WordPress. You can get login form using <strong>wp_login_form()<\/strong> function.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-file=\"function.php\" data-lang=\"PHP\"><code>&lt;?php\n  if (!is_user_logged_in()) {\n      $args = array(\n          &#39;echo&#39; =&gt; true,\n          &#39;remember&#39; =&gt; true,\n          &#39;redirect&#39; =&gt; ( is_ssl() ? &#39;https:\/\/&#39; : &#39;http:\/\/&#39; ) . $_SERVER[&#39;HTTP_HOST&#39;] . $_SERVER[&#39;REQUEST_URI&#39;],\n          &#39;form_id&#39; =&gt; &#39;loginform&#39;,\n          &#39;id_username&#39; =&gt; &#39;user_login&#39;,\n          &#39;id_password&#39; =&gt; &#39;user_pass&#39;,\n          &#39;id_remember&#39; =&gt; &#39;rememberme&#39;,\n          &#39;id_submit&#39; =&gt; &#39;wp-submit&#39;,\n          &#39;label_username&#39; =&gt; __(&#39;Username or Email Address&#39;),\n          &#39;label_password&#39; =&gt; __(&#39;Password&#39;),\n          &#39;label_remember&#39; =&gt; __(&#39;Remember Me&#39;),\n          &#39;label_log_in&#39; =&gt; __(&#39;Log In&#39;),\n          &#39;value_username&#39; =&gt; &#39;&#39;,\n          &#39;value_remember&#39; =&gt; false\n      );\n      wp_login_form($args);\n  } else {\n      echo &#39;&lt;h4&gt;Welcome to the Community!&lt;\/h4&gt;&#39;;\n  }\n?&gt;<\/code><\/pre><\/div>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1672293541868\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">Is it possible to construct a form in WordPress without using a plugin?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>You must embed the code into your WordPress site functions.php to create a contact form without using a plug-in. This entails copying and pasting an auto-generated string of code into the text editor for your WordPress web page.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1672293614931\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">How can I change the URL of my WordPress login page without using plugins?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>The wp-login.php file contains all of the code that creates the login page and manages the login procedure by default. In our new file, we may reuse the code from wp-login.php.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1672293709475\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">What is the URL for logging into WordPress?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>To access the WordPress login page, add \/login\/, \/admin\/, or \/wp-login.php to the end of your site&#8217;s URL.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div><div id=\"ezoic-pub-ad-placeholder-118\"><\/div>","protected":false},"excerpt":{"rendered":"<p>Are you need simple login form without plugin for your WordPress website? Yes, Then i have created [cxc_login_form] shortcode with quick simple code. So you can put login form anywhere like in Pages, Posts and Widget etc\u2026 Therefore you just need to put [cxc_login_form] shortcode where you need login form. So you need to follow [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":2123,"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\/30"}],"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=30"}],"version-history":[{"count":10,"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/posts\/30\/revisions"}],"predecessor-version":[{"id":1998,"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/posts\/30\/revisions\/1998"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/media\/2123"}],"wp:attachment":[{"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/media?parent=30"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/categories?post=30"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/tags?post=30"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}