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 protected]';
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'];
?>

1 Answer 1

6

You are sending the data from the form directly to TestForm.php file, which is a PHP script outside of WordPress logic. It is and independent script. You could set the form's action attribute to a empty string, this way the form data is sent to same page that contains the form, which is part of WordPress:

<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>

In this case, the form data is sent to a Wordpress page and the add_action('init','create_account'); is triggered if you add it to functions.php file, for example like this:

add_action('init','create_account');
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
       }
    }

}

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

11
  • So i cant make TestForm.php page ?
    – Gaurav
    Jul 16, 2014 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, 2014 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, 2014 at 16:07
  • And where am i defining this function?Is it in functions.php?Or in TestForm.php?
    – Gaurav
    Jul 16, 2014 at 16:09
  • In functions.php
    – cybmeta
    Jul 16, 2014 at 16:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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