Archive for the 'wordpress' Category

 

cforms ii custom submit script

Aug 15, 2008 in php, wordpress

Cforms ii is one of the most powerful plugins available for wordpress. It enables quick form creation with several nice data tracking features. There is also the option to reroute the form submission to a custom script. This is a very nice feature that makes it easy to control what happens after form submission. Here is a script I wrote that saves the data collected back to the wordpress database and emails the admin and form submitter.

Place this script somewhere in a folder under your Wordpress installation, and on the cforms admin page under Redirection, Messages, Text and Button Label  set the Enable alternative form action! checkbox and then provide the URL to the script. Once a form is submitted you will still be able to track the submission via the admin tool.

<?php
//Make sure the user got here by way of form submission
// replace sendbutton2 with the name of your forms send button.

if (isset($_POST['sendbutton2'])) {
?>
<h3>Thanks for your submission.</h3>
<div>Some other detailed html message if you like…</div>

<?php

require(”wp-includes/class-phpmailer.php”);

////////////////////////////////
// Notification Email Recipients
$mailto = array();

//These addresses are for notification when the form is submitted.
$mailto[] = array(”address” => “someaddress1@somewhere.net”, “name” => “some person1″);
$mailto[] = array(”address” => “someaddress2@somewhere.net”, “name” => “some person2″);
$mailto[] = array(”address” => “someaddress3@somewhere.net”, “name” => “some person3″);

$mailfromaddress     = ‘from@address.net’;
$mailfromname         = ‘from name’;

$mailnotificationsubject = ‘Notification Subject’;
$mailconfirmationsubject = ‘Confirmation Subject’;
//////////////////////////////////////////////////////////////////

//Set the variables
//These come directly from cforms. You can use firebug to so see the field names, or just use the
//given form number. Example, the second field in form 2 will have ID cf2_field_2
// Of course you should customize this to your specific form.

$first = $_POST['cf2_field_2'];
$last = $_POST['cf2_field_3'];
$email = $_POST['cf2_field_4'];
$address = $_POST['cf2_field_5'];
$city = $_POST['cf2_field_6'];
$state = $_POST['cf2_field_7'];
$zip = $_POST['cf2_field_9'];
//etc etc etc

$link = mysql_connect(’DB-name usually localhost’, ‘DB-user’, ‘DB-password’);
if (!$link) {
die(’Could not connect: ‘ . mysql_error());
}
//Your wordpress schema name
mysql_select_db(”DB-schema-name”, $link);

mysql_query(”INSERT INTO wp_cformssubmissions (form_id, sub_date, email, ip) VALUES (’$formID’, NOW(), ‘$email’, ‘$_SERVER[REMOTE_ADDR]‘)”);

$id = mysql_insert_id();

//Insert the information block.
mysql_query(”INSERT INTO wp_cformsdata (sub_id, field_name, field_val) VALUES (’$id’, ‘First Name’, ‘$first’)”);
mysql_query(”INSERT INTO wp_cformsdata (sub_id, field_name, field_val) VALUES (’$id’, ‘Last Name’, ‘$last’)”);
mysql_query(”INSERT INTO wp_cformsdata (sub_id, field_name, field_val) VALUES (’$id’, ‘Email’, ‘$email’)”);

mysql_query(”INSERT INTO wp_cformsdata (sub_id, field_name, field_val) VALUES (’$id’, ‘Address’, ‘$address’)”);
mysql_query(”INSERT INTO wp_cformsdata (sub_id, field_name, field_val) VALUES (’$id’, ‘City’, ‘$city’)”);
mysql_query(”INSERT INTO wp_cformsdata (sub_id, field_name, field_val) VALUES (’$id’, ‘State’, ‘$state’)”);
mysql_query(”INSERT INTO wp_cformsdata (sub_id, field_name, field_val) VALUES (’$id’, ‘Zip Code’, ‘$zip’)”);

mysql_close($link);

//Mail to all persons who should be notified.
$mail = new PHPMailer();

foreach($mailto as $to) {
$mail->AddAddress($to["address"], $to["name"]);
}

$mail->IsSMTP();  // telling the class to use SMTP
$mail->Host = “smtp.someserver.net”; // Your SMTP server  here

$mail->From = $mailfromaddress;
$mail->FromName = $mailfromname;

$mail->Subject = $mailnotificationsubject;
$mail->Body = <<<EOT
Hello Admin,<br/><br/>A new form submission has been completed …. <br/><br/>
<b>Details:</b><br/>
$first $last <br/>
$email <br/>
Login to http://yourwordpressinstance.org to review further registration information.
EOT;

$mail->AltBody = “<<<EOT
Hello Admin,
A new form submission has been completed ….
Details
$first $last
$email
$orderNum
Login to http://yourwordpressinstance.org to review further registration information.
EOT;”;
$mail->Send();


//Mail to form submitter

$mail = new PHPMailer();
$mail->AddAddress($email, $first.’ ‘.$last);
$mail->IsSMTP();  // telling the class to use SMTP
$mail->Host = “smtp.someserver.net”; // Your SMTP server  here

$mail->From = $mailfromaddress;
$mail->FromName = $mailfromname;
$mail->Subject = $mailconfirmationsubject;
$mail->Body = <<<EOT
Hello $first,<br/><br/>Message to submitter<br/><br/>
<b>Details:</b><br/>
$first $last <br/>
$email <br/>
EOT;

$mail->AltBody = <<<EOT
Hello $first,
Message to submitter
Details:
$first $last
$email
EOT;

$mail->Send();

}
//Did not get here via form post,  output an error.

else
echo “PAGE ERROR: You have tried to access this page from invalid context…”;
?>

wordpress get_parent function

Aug 14, 2008 in php, wordpress

 I was looking for a way to get the parent ID of the current subpage recently and could not find an available function on the codex pages. I needed this function in order to create a navigation that is always based on the current page. The site I was working on has no more than 3 levels of navigation, so I needed a way for the deepest sub-pages to generate the same side navigation as the 2nd level pages. I wrote this short simple function.

Hopefully someone may find it usefull.

//Gets the parent ID of a post or page
function get_parent($parent) {
global $wp_query;
$return = $wp_query->post->post_parent;

return $return;
}

Flexible Navigation – Wordpress Plugin *beta

Jun 13, 2008 in flex, php, wordpress

Beta release 0.09

Download it here

This Plugin adds a Flex Accordion Navigation to your wordpress instance. This plugin is perfect for those sites that have out of control links in the sidebar. With Flexible-Navigation you can simple use one function call in the sidbar to generate an accordion navigation.

add AddFlexNav(); anywhere you would like to add the navigation. (most commonly the sidebar.php file of your theme.)

Flexible navigation takes your links, posts and catagories and creates an accordion navigation. You may customize this from the admin menu or by editing the php file itself.

more to come!

Populate user form using register-plus and cforms II

May 22, 2008 in internet, php, wordpress

One very key feature of the new cforms II wordpress plugin (8.4.2) is its expanded use of custom variables for its form fields. I have found that it is rather easy to add more variables to be used. Simply add them to lib_aux.php in the cforms II directory. Using register-plus, it is possible to save even more information about the user by adding more fields upon registration as a new user. This information is stored in the usermeta table. Using standard Wordpress variables, making the extra information captured with register-plus available to cforms II as custom variables is a snap!

if ( $wp_db_version >= 3440 ) //&& function_exists( ‘wp_get_current_user’ )
$CurrUser = wp_get_current_user();
$CurrUserId = $CurrUser->ID;
$CurrUserMeta = get_userdata($CurrUserId);

$m = str_replace( ‘{Form Name}’, get_option(’cforms’.$no.’_fname’), $m );
$m = str_replace( ‘{Page}’, $page, $m );
$m = str_replace( ‘{Date}’, $date, $m );
$m = str_replace( ‘{Author}’, $find->display_name, $m );
$m = str_replace( ‘{Time}’, $time, $m );
$m = str_replace( ‘{IP}’, cf_getip(), $m );
$m = str_replace( ‘{BLOGNAME}’, get_option(’blogname’), $m );

$m = str_replace( ‘{CurUserID}’, $CurrUser->ID, $m );
$m = str_replace( ‘{CurUserName}’, $CurrUser->display_name, $m );
$m = str_replace( ‘{CurUserEmail}’,$CurrUser->user_email, $m );


//Some more custom variable for use in the form
$m = str_replace( ‘{CurUserFirstName}’, $CurrUserMeta->first_name, $m );
$m = str_replace( ‘{CurUserLastName}’, $CurrUserMeta->last_name, $m );
$m = str_replace( ‘{CurUserDepartment}’,$CurrUserMeta->department, $m );