Archive

Archive for the ‘Intermediate’ Category

Attaching Post-Submit PHP to Contact Form 7 Forms

July 11th, 2011 No comments

One of the non-obvious features available in the Contact Form 7 plugin for WordPress are the hookswpcf7_before_send_mail” which gets fired right before the email is sent and “wpcf7_mail_sent” right after the email is sent.

Both of these actions are also sent as the first argument the WPCF7_contact_form object. print_r() on it and see what you get: some good stuff, particularly, $wpcf7_contact_form->posted_data is an associative array of the submitted data.

So include this code somewhere in a helper plugin or something and VOILA!

add_action('wpcf7_before_send_mail', 'do_post_submit_pre_mail_php');

function do_post_submit_pre_mail_php($form_obj) {
    print_r($form_obj->posted_data);
    return;
}

I haven’t checked into the other hooks, but these and the other customization available with Contact Form 7 make WordPress an even better tool for basic yet fully-featured Content Management System

Categories: Intermediate, PHP, Tutorials Tags:

Text Image Creator

May 16th, 2008 No comments

This is a simple image creator to make images out of text. It uses functions in the GD library for PHP. The image is updated automatically when the specifications form inputs are changed (except for the text) with some simple Javascript. The image created is in .png format and if you click the download link, the file is saved as text_image.png.

Requests for additional features/image attributes can be submitted through the comments section.

And since this is a nice example of using the GD Library, here’s the code:

class text_image_creator { function text_image_creator($text, $font="AmericanaItalicBT.ttf", $font_size = 20, $text_color = "#000000", $background_color = "#FFFFFF", $download = false) { $font = getcwd()."/fonts/".$font; $textbox = imagettfbbox($font_size, 0, $font, $text); /*RETURN VALUES FOR IMAGETTFBBOX 0 lower left corner, X position 1 lower left corner, Y position 2 lower right corner, X position 3 lower right corner, Y position 4 upper right corner, X position 5 upper right corner, Y position 6 upper left corner, X position (0,0) 7 upper left corner, Y position */ // BUILD IMAGE WITH A PADDING OF 1X THE FONT SIZE $height = $textbox[1] + 2*$font_size; //compensate for large tff files $width = $textbox[2] + $font_size; $image = @imagecreate($width, $height) or die('Cannot Initialize new GD image stream'); $bc_array = get_rgb_values($background_color); $background_color = imagecolorallocatealpha($image, $bc_array['r'], $bc_array['g'], $bc_array['b'], 0); $tc_array = get_rgb_values($text_color); $text_color = imagecolorallocate($image, $tc_array['r'], $tc_array['g'], $tc_array['b']); // COMPUTE POSITION COORDINATES $pos_x = ($width - $textbox[4])/2; $pos_y = ($height - $textbox[3])/2 + ($font_size/2); imagettftext($image, $font_size, 0, $pos_x, $pos_y, $text_color, $font , $text); header('Content-Type: image/png'); if($download) header('Content-Disposition: attachment; filename=text_image.png'); imagepng($image); imagedestroy($image); } } function get_rgb_values($color) { $color = str_replace("#", "", $color); $a['r'] = hexdec(substr($color, 0, 2)); $a['g'] = hexdec(substr($color, 2, 2)); $a['b'] = hexdec(substr($color, 4, 2)); return $a; } if(isset($_GET['text']) AND $_GET['text'] != '') $text = urldecode($_GET['text']); else $text = "Sample"; if(isset($_GET['font']) AND $_GET['font'] != '') $font = urldecode($_GET['font']); else $font="AmericanaItalicBT.ttf"; if(isset($_GET['font_size']) AND $_GET['font_size'] != '') $font_size = urldecode($_GET['font_size']); else $font_size = 20; if(isset($_GET['text_color']) AND $_GET['text_color'] != '') $text_color = urldecode($_GET['text_color']); else $text_color = "#000000"; if(isset($_GET['background_color']) AND $_GET['background_color'] != '') $background_color = urldecode($_GET['background_color']); else $background_color = "#FFFFFF"; new text_image_creator($text, $font, $font_size, $text_color, $background_color, $_GET['download']);