0

I am having a form page called Test Form with this form :

<div id="container">
<form action="wp-content/themes/max-magazine/TestForm.php" method="post" name="myForm">
User <input type="text"  name="uname" />
Email  <input id="email" type="text" name="uemail" />
Password  <input type="password"  name="upass" />
<input type="submit" value="Submit" /></form>
</div>

And in backend am having a php file named TestForm.php

<?php 
function create_account(){
$user = 'AccountID';
$pass = 'AccountPassword';
$email = 'email@domain.com';
if ( !username_exists( $user )  && !email_exists( $email ) ) {
    $user_id = wp_create_user( $user, $pass, $email );
    $user = new WP_User( $user_id );
    $user->set_role( 'contributor' );
} 
}
add_action('init','create_account');
?>

Now if this function is defined in function.php then it works fine and create the user but same function in TestForm.php does not work. Please help

Also I will be fetching data from form like this :

<?php
$user = $_POST['uname'];
$pass = $_POST['upass'];
$email = $_POST['uemail'];
?>
4

If you send the data of the form directly to TestForm.php file don't expect Wordpress to be loaded. Instead, you should not set any action in the form, set it to a empty string or set it to the same page where the registration form is shown.

<div id="container">
    <form method="post" name="myForm">
        User <input type="text"  name="uname" />
        Email  <input id="email" type="text" name="uemail" />
        Password  <input type="password"  name="upass" />
        <input type="submit" value="Submit" />
    </form>
</div>

Then, you send the data to a Wordpress page and the add_action('init','create_account'); is triggered if you add it to functions.php file:

function create_account(){
    //You may need some data validation here
    $user = ( isset($_POST['uname']) ? $_POST['uname'] : '' );
    $pass = ( isset($_POST['upass']) ? $_POST['upass'] : '' );
    $email = ( isset($_POST['uemail']) ? $_POST['uemail'] : '' );

    if ( !username_exists( $user )  && !email_exists( $email ) ) {
       $user_id = wp_create_user( $user, $pass, $email );
       if( !is_wp_error($user_id) ) {
           //user has been created
           $user = new WP_User( $user_id );
           $user->set_role( 'contributor' );
           //Redirect
           wp_redirect( 'URL_where_you_want_redirect' );
           exit;
       } else {
           //$user_id is a WP_Error object. Manage the error
       }
    }

}
add_action('init','create_account');

P.D.: I suggest you to use a modern HTML5 form markup and input types

  • So i cant make TestForm.php page ? – Gaurav Jul 16 '14 at 15:57
  • Then problem is how to get values of user,email and password in functions.php from the form myForm? – Gaurav Jul 16 '14 at 15:58
  • You have access to $_POST inside the create_account() function, so you get the values as you do in any other form in PHP. And you can make TestForm.php but you will have to load Wordpress engine in that file, which I really think you don't want. – cybmeta Jul 16 '14 at 16:07
  • And where am i defining this function?Is it in functions.php?Or in TestForm.php? – Gaurav Jul 16 '14 at 16:09
  • In functions.php – cybmeta Jul 16 '14 at 16:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.