Ok, for some reason it’s now working. Instead of hooking into ‘init’ I’ve used ‘the_post’ and the form shortcode does it’s job. I no longer have an issue with publishing a new post or the data not saving. Also, no “Cannot modify header information – headers already sent” error.
The function
function el_process_form(){
if( $_SERVER['REQUEST_METHOD'] == 'POST' && !empty( $_POST['post_type'] ) && $_POST['post_type'] == "everlead"){
$name = stripcslashes($_POST["name"]);
$email = stripcslashes($_POST["email"]);
$phone = stripcslashes($_POST["phone"]);
$website = stripcslashes($_POST["website"]);
// Do some minor form validation to make sure there is content
if (trim($name) == "" ) {
echo 'Please go back and enter your name. <br />';
exit();
}
if (trim($email) == "" ) {
echo 'Please go back and enter a valid email address.<br/>';
exit();
}
if (trim($phone) == "" ) {
echo 'Please go back and enter a contact number.<br/>';
exit();
}
if (trim($website) == "" ) {
echo 'Please go back and enter your website url.<br/>';
exit();
}
global $wpdb;
//Set time
$formtime = date('m-d-Y h:i:a');
$ipaddress = $_SERVER['REMOTE_ADDR'];
//Table data
$data = array(
'name' => $name,
'email' => $email,
'phone' => $phone,
'website' => $website,
'date' => $formtime,
'ipaddress' => $ipaddress
);
//set lead data
$table = $wpdb->prefix . 'el_leads';
$wpdb->insert( $table, $data, array('%s', '%s', '%s', '%s', '%s') );
}
}
I use ‘the_post’ and all is fine
add_action( 'the_post', 'el_process_form' );
Now my shortcode [el_form buttonsize="block"]
works and displays the form on any page or post. Most importantly, it saves the data to the database.
I’ll have to dig deeper to find out why ‘the_post’ works over ‘init’ when using a form shortcode.