Build a HTML Form From Scratch and Use in WordPress Post/Pages

You have many plugin to use form in your post/pages and they can do great job for you. but what about creating a simple form by own-self from scratch and use that in your posts?

so lets start with simple coding style and create a form with three field

<form action="" method="post" enctype="multipart/form-data" name="form" >
<label for="yourname"></label>
<input type="text"  id="yourname" name="yourname">

<label for="gender"></label>
<select id="gender" name="gender">
<option value="male">Male</option>
<option value="female">Female</option>
</select>

<label for="address"></label>
<input type="text"  id="address" name="address">

<input  type="submit" name="submit" value="submit">

this is a simple form to use in html page but not for WordPress. WordPress has its on ways.

to use this form in some post or page you need to create a page template or use shorcode to display form. page template are great way to go but in this case i am suggesting you to use wp shortcode.

So just move to your WordPress template directory and  open functions.php file

and place this short code there.

function createform() {
// your form code

}

add_shortcode('form', 'createform');

shortcode made our job so simple, now you can use [form ] to place everywhere in post/page. just put [form] in your posts and this will fetch code inside createform(); function.

so now put code in function

function createform() {
return '
<pre><div id="forms">
<form action="" method="post" enctype="multipart/form-data" name="form" >
<label for="yourname"></label>
<input type="text"  id="yourname" name="yourname">

<label for="gender"></label>
<select id="gender" name="gender">
<option value="male">Male</option>
<option value="female">Female</option>
</select>

<label for="address"></label>
<input type="text"  id="address" name="address">

<input  type="submit" name="submit" value="submit"></pre>
';

 }

add_shortcode('form', 'createform');

now create an new post and put [form] in your post , you must be able to see our created form there.

for next step to move we need to be able to submit this form. now again open up your functions.php file and put this code.

add_action('init', 'submit');

function submit(){
if(isset($_POST['submit']))
{
$myname=$_POST['myname'];
$gender=$_POST['gender'];
$address=$_POST['address'];
$sql=INSERT INTO form (id, myname, gender, address) VALUES
('', '$myname', '$gender','$address', );

&quot;;

require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);

}
}

and you are done!

Wp-redirect To Use In WordPress Post and Pages

You have many plugin which can do redirection for you and are great at doing your job. but sometime you many need to redirect user after some specific event or task. like if user click on some button, it should go to some specific page in your WordPress website or outside to some website. at this point plugins are not helping you to do this. you can only use plugin for the purpose of redirecting of some post/page to some other page/post.

So you need to know about wp_redirect() function.

wp_redirect() is a Pluggable Function

you can find wp_redirect()  in wp-includes/pluggable.php.

so if you are coding a plugin or theme you may need this function to redirect purpose.

i also use this when i was working on a form and i was using WordPress short code to display form and then i apply some conditions to  form, if user select what and he will go to some page and like this.

code bellow:

add_action('init', 'get_rate');

function get_rate(){
if(isset($_POST['get_rate']))
{
$loan_purpose=$_POST['loan_purpose'];
if($loan_ammount&lt;10000)
{
wp_redirect(home_url('/decline'));
}

else

{

wp_redirect(home_url('/approve'));

}

so this should work accordingly to conditions. here you see get_rate, its actually that submit button name. i am using function to take data from FORM. i have place this code to functions.php file of WordPress.

then i was in need to redirect user to outside of my WordPress website. i get that working by following code.

wp_redirect("http://www.google.com",301); exit();

at this stage i miss http:// in the start of www.google.com, so page was not redirecting properly and showing address at the end of page url. so you also need to be careful about this. place http:// before the address of some page which you want to redirect.

here is one thing you can also use PHP headers to redirect page but mostly chance is you will get ‘header already sent’ error on your page.

if you will see wp redirect refrence:

wp_redirect() does not exit automatically and should almost always be followed by exit.

<?php
wp_redirect( $location, $status );
exit;
?>

use this like to 301 redirect

<?php wp_redirect( 'http://www.example.com', 301 ); exit; ?>

Header may not contain more than a single header, new line detected…

You may get this error when you are dealing with lot of variables and trying to passing those variable to header and by a mistake you have new line in your header string.

i was getting this following error when redirecting after form submit..

“Warning: Header may not contain more than a single header, new line detected. in /home/gedforad/public_html/index.php on line 60”

So i was thinking there may be the problem with redirect it with lot of variables but it was not!

there may be you have some code like this

[php]

$post_url="Location: http://webservices.somewebsite.com/default.ashx?"."&amp;state=".$state."&amp;country=".$country;

header($post_url);

[/php]

Or whatever code you have with lot of variable.its most common when press enter to go to new line.

here i am using location within string , otherwise actual code should be(this is also a way to avoid errors):

[php]

$post_url="http://webservices.somewebsite.com/default.ashx?"."&amp;state=".$state."&amp;country=".$country."&amp;email=".$email."&amp;ReturnToURL=".$ReturnToURL;

header("Location: $post_url");

[/php]

you will get an error if you will have some new line in your $post_url variable string.

just test your code again and remove new line in your code, you can post lot of variable through browser.

Codeigniter Issues- Moving Website From Localhost To Live Server

Its always a issue when you move your website from localhost to live server (Regardless of your type of website). but when you are dealing with some  more complicated Framework or Cms, its need to be careful when moving website.

Here we are going to discuss about issue which may occur when you move your codeigniter website.

Codeigniter have “auto connect” feature that will load and instantiate the database class with every page load. To enable “auto connecting”, we only need to add the word database to the library array.

application/config/autoload.php

in this file you can edit auto-load setting of your codeigniter application.

1) First In your case you would create two copies of config.php. One for localhost and one for live server, In your development copy you would have the base URL set to localhost and in the production copy you would have it set to youwebsite.com. this will help to manage both environment when you are transferring your website.

2) 2nd issue you may face regarding your mod rewrite, mod rewrite is an amazing feature of Apache server by which you can make SEO friendly URL for website Simply, mod_rewrite is used for rewriting a URL at the server level, giving the user output for that final page. Codeginitor URL’s always include /index.php with each controller class. so you need to do some modifications to your .htaccess and your mod rewrite should be ON to apply those changes. its mostly happen you are trying your .htaccess file but problem is with mod rewrite engine.

http://codeigniter.com/wiki/mod_rewrite

here are the steps involves in removing index.php from your urls.

3) Codeigniter Sessions, this is issue not at your end and not very common to every server.but i face it when i was transferring my website to fatcow server. Fatcow server was not storing sessions, so that time i edit php.ini file to make enable to store sessions. you also need to give path in php.ini file where you session will be stored.

4) Be careful about use of Case-sensitive issue of codeigniter while redirecting between pages.

example.com/news/article/
my_article is not same as example.com/news/Article/
My_article

it may work fine on localhost but when you will use it on live server it will become problem and will show 404 errors.