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

1

You need to change the from action url correctly.
You just using this in your form

<form action="http://yourdomain.com/Test-form" method="post" name="myForm"/>  

to give this action url to form follow the steps

Step1 --Creating page

Here Test-form is a page you need to create in your wordpress dashboard through
pages->Add new, give name Test-form for page.
and on creating this page assign template as Test form template from Default template (see right panel of dashboard to select templates).

step2 --creating page template Test form template in TestForm.php

<?php
/*
Template Name: Test form template
*/
?>
<?php get_header(); ?>
<?php include (TEMPLATEPATH . '/functions.php');//already have their works if your file location changed ?>
<!-- Your action page contents goes here -->
<?php
$user = $_POST['uname'];
$pass = $_POST['upass'];
$email = $_POST['uemail'];
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' );
} 
}
do_action('init');
?>
<?php get_sidebar(); ?>
<?php get_footer(); ?>

Now save this in your theme directory as TestForm.php(wp-contents/themes/yourtheme/TestForm.php)
and this TestForm.php file will add Test form template , this you can add in step1

In your theme's functions.php file, or the like:

add_action('init','create_account');

In the page template Test form template use

do_action('init');
Sign up to request clarification or add additional context in comments.

3 Comments

But the form is directing to the TestForm.php on click of submit button.So i dont find any issues with url
The problem am facing is the same function is working fine in functions.php but when i use it in TestForm.php then it doen't work.So is their a way that i can use function defined their in TestForm.php with arguments being the (user,pass,email)?
i think i just edit the answer as you expecting try the code,comment the issue if you have any more

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.