admin管理员组

文章数量:1424784

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

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'];
?>
Share Improve this question edited Jul 16, 2014 at 15:06 cybmeta 20.6k5 gold badges47 silver badges57 bronze badges asked Jul 16, 2014 at 14:43 GauravGaurav 11 gold badge1 silver badge1 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 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

本文标签: phpCreating wordpress user registration form