martes, 17 de noviembre de 2009

Displaying user data in your Wordpress theme

The last time I needed to display user data was for the Press Stick website. For their site, they wanted the user to be greeted with a personalised message on logging in.

The message for the Press Stick site was:
"hi from . welcome back to the press-stick sourcing portal. we look forward to showing you the new and exciting products available to you."

For the purpose's of this site, we tweaked the standard Wordpress user data so that the user's magazine name would be stored in the 'Nickname' field. To return the message above, we simply insert the following:

user_firstname.' '.$current_user user_lastname;
$magazine = $current_user nickname;

echo '';

List all blogs in a Wordpress MU installation

The African Travel Experts site has two dropdown lists of all the blogs on their WPMU installation sorted into two different categories. Here's how.

The way the site is set up is to have the African Travel Experts site as the main 'umbrella site' with sub-sites for each African country, which act as parent sites to sub-sites relating to the country. For example:

Master site: African Travel Experts
Country sub-site: South Africa
Child-site: Cape Town

Seeing as the standard WPMU installation doesn't provide for classifying blogs/sites in this way, a plugin and a bit of lateral thinking was required. I opted for the Blog Types plugin, which allows you to define your own categories for a blog. Once installed, I defined my categories in the config as follows:


// Blog types
// name and nicename are required

$blog_types = 'South Africa';
$blog_types = 'south_africa';
$blog_types = '';
$blog_types = 'no';

$blog_types = 'Botswana';
$blog_types = 'botswana';
$blog_types = '';
$blog_types = 'no';

//.... etc until all countries are defined

$blog_subtypes = 'Cape Town';
$blog_subtypes = 'cape_town';
$blog_subtypes = 'south_africa';
$blog_subtypes = '';

$blog_subtypes = 'Eastern Cape';
$blog_subtypes = 'eastern_cape';
$blog_subtypes = 'south_africa';
$blog_subtypes = '';

//.... etc until all areas are defined

// Allow users to select one or multiple blog types
// Note: If you allow users to select multiple blog types, they cannot select a subtype
$blog_types_selection = 'single'; //Options: 'single' or 'multiple'

// Allow users to select one or multiple blog subtypes
$blog_subtypes_selection = 'single'; //Options: 'single' or 'multiple'

// Branding singular
$blog_types_branding_singular = __('Country');
$blog_subtypes_branding_singular = __('Area');

// Branding plural
$blog_types_branding_plural = __('Countries');
$blog_subtypes_branding_plural = __('Areas');

// Display admin panel blog types page
$blog_types_display_admin_page = 'yes'; //Options: 'yes' or 'no'

// Display signup form blog types selection
$blog_types_display_signup_form = 'yes'; //Options: 'yes' or 'no'

// Enable subtypes
$blog_types_enable_subtypes = 'yes'; //Options: 'yes' or 'no'

Watermark images

Quivertree publications are an image library website and like most image library websites, they wanted their images watermarked

There are a lot of scripts floating about for adding watermarks to image as they are served up to the end user, but a lot of these made it too easy for the end user to ascertain the location of the original, 'un-watermarked' image, and as there was already a backend process involved in loading the images and their meta-data, it made sense to write code that generated the final watermarked image in advance. I used this fantastic tutorial on Watermarking Images on the Fly in PHP as a starting point and came up with this:


$dir = dirList ('temp_images');//function that returns a list of all images in the temp_images directory - these images were already uploaded to this location by the site admin
if($dir)://if there are images in the temp_images directory, then away we go!
foreach($dir as $key => $value): //for each image $key = image number, $value = the image name (e.g myImage.jpg)
$imgsrc = 'temp_images/'.$value; //source image without watermark
$imgout = 'temp_watermark/'.$value; //where to save the image with the watermark
watermark_img($imgsrc,$imgout);//this function is below
endforeach;
else:
echo 'You need to upload some images first';
endif;


Here is the watermark_img function:


function watermark_img($imgsrc,$saveas) {
$watermark = imagecreatefrompng('images/watermark.png'); //location of my watermark
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
$image = imagecreatetruecolor($watermark_width, $watermark_height);
$image = imagecreatefromjpeg($imgsrc);
$size = getimagesize($imgsrc);
$mainimg_w_pos = $size / 2; //get half of the main image
$watermark_w_pos = $watermark_width / 2; //get half of the watermark
$dest_x = $mainimg_w_pos - $watermark_w_pos; //watermark sits at half of width minus half its own width
$mainimg_h_pos = $size / 2; //get half of the main image
$watermark_h_pos = $watermark_height / 2; //get half of the watermark
$dest_y = $mainimg_h_pos - $watermark_h_pos; //watermark sits at half of width minus half its own width
_ckdir($saveas);//function to test that save as location is available
imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 100);
imagejpeg($image,$saveas,100);
}

lunes, 16 de noviembre de 2009

Auto-generating Community Builder profiles

For the AIRCO website, user profiles had to be auto-generated on admin approval, and a mail generated to the user.

The site was already running on a Joomla installation making use of the Community Builder suite, but some extra tweaking was needed to hook the site up to the member application backend.

The Process:
Potential member completes application form
Admin approves form
Profile is auto-generated and details are mailed to the member as follows:

1. Convert the member's name to a valid username (the member can always edit this if it isn't what they want)

$username = ereg_replace("", "", $name ); //convert to alphanumeric
$username = trim($username); // remove any unnecessary whitespace
$username = substr($username, 0, 24); //ensure the length of the username doesn't exceed 24 characters


2. Ensure that the email address used is unique (this is tested on initial registration, but we re-test to be safe)

$query = "SELECT * FROM jos_users WHERE email = '$email'";
$result = mysql_query($query) or die(mysql_error());
$check = mysql_num_rows($result);
if($check 0):
//flag as duplicate and exit
endif;


3. Check for duplicate username, and if one exists add a number on the end, retest and increment number until we get a unique value:

$n = 0;
$username_flag = '';
while($username_flag != 'set'):
$query = "SELECT * FROM jos_users WHERE username = '$username'";
$result = mysql_query($query) or die(mysql_error());
$check = mysql_num_rows($result);
$n++;
if($check 0):
$username = $username.$n;
else:
$username_flag = 'set';
endif;
endwhile;


4. Everything ok? Then we add the data:
a. Add
jos_users - name, username, email, password (remember to encrypt this with md5), usertype, gid, registerDate, params
jos_core_acl_aro - section_value = users, value = id of the record you inserted into jos_users, name = name of the member (as entered into jos_users)
jos_core_acl_groups_aro_map - group_id = id of the group your member belongs to, in my case 19 - Author, aro_id = id of the record you inserted into jos_core_acl_aro
jos_comprofiler id & user_id = id of the record you inserted into jos_users, then any other values captured for custom fields.

5. Finally email the member:

$to = $email;//captured from application form
$subject = "Your profile";
$body = "Dear ".$name."\n";
$body .= "You are now a full Member.\n\n";
$body .= "MEMBER'S AREA LOG-IN DETAILS\n";
$body .= "Username: ".$username."\n";
$body .= "Password: ".$password."\n\n";
$from = "FROM: ".$SENDERS_EMAIL_HERE;
mail($to, $subject, $body, $from);