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
echo 'You need to upload some images first';

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);

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

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);
if($check 0):
$username = $username.$n;
$username_flag = 'set';

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 .= "Username: ".$username."\n";
$body .= "Password: ".$password."\n\n";
mail($to, $subject, $body, $from);

jueves, 12 de noviembre de 2009

African Travel Experts


Press Stick

Dynamic mp3 player

For the Elastic Artists website, a dynamic MP3 player was required that could be controlled and loaded from the backend.

I decided to go with a Flash based player and found a great tutorial for creating a flash MP3 player using XML. In order for the player layout to match the design called for by the client, a few tweaks were needed:

Original code:

_root._x = 5;
_root._y = 40 + (i*15);
_root.but_txt.text = songname;
if (i >= 3){
_root._x = 160
_root._y = -5 + (i*15);

My code:

_root._x = 0;
_root._y = 55+(i*30);
_root.but_txt.text.html = true;
_root.but_txt.htmlText = "Artist: "+songband+""+newline+"Track: "+songname;

The original produces two columns of 3 tracks side by side and just displays the song name. The amended code produces one list of tracks with font-color black (other font info was applied through flash) in the format:
Artist: artistname
Track: trackname
The amount of tracks returned will always be nine. This is controlled by the backend of the website.

Other than all the various original design elements, the only other change called for was for a playlist generated from a database table. While the tutorial makes use of a user-generated XML file, I used a php file that generates XML:

header("Content-type: text/xml");
$xml_output = '$xml_output .= '';
//connect and run query to return values for track title, artist and link to mp3 file
$title = $row;
$url = $row;
$artistName = $row;
$xml_output .= '';
$xml_output .= '
echo $xml_output;

The client can then control the contents of the playlist via the site backend.

miércoles, 11 de noviembre de 2009

Wordpress plugins worth your time

Wordpress plugins can make your blog better and your blogging easier. These are my new faves.

Bad Behavior
Bad Behavior complements other link spam solutions by acting as a gatekeeper, preventing spammers from ever delivering their junk, and in many cases, from ever reading your site in the first place. This keeps your site’s load down, makes your site logs cleaner, and can help prevent denial of service conditions caused by spammers.

Broken Link Checker
Checks your posts for broken links and missing images and notifies you on the dashboard if any are found.

Comment Info Tip
When you mouseover a commenter’s name you will see a tip appear displaying some information about that given commenter.

Contact Form 7
Contact Form 7 can manage multiple contact forms, plus you can customize the form and the mail contents flexibly with simple markup. The form supports Ajax-powered submitting, CAPTCHA, Akismet spam filtering and so on.
Embed Iframe
Embed Iframe is a Wordpress plugin that will let you embed iframe – an HTML tag that allows a webpage to be displayed inline with the current page, in a Wordpress post. Although an iframe can lead to a complicated website, it can be very effective when used appropriately.

Hotlink Protection
Do you have problems that other people hotlink your images in guestbooks, bulletin boards and other sites? Then this plugin for Wordpress helps you without breaking online feedreaders. Normally, if someone hotlinks your image the plugin will serve your custom image or this image:

MoFuse is a service that allows bloggers to easily create a mobile-friendly version of their blog for free.

This plugin allows you to automatically detect and redirect your mobile visitors to the mobile-friendly version of your blog.

Lifestream is a plugin built on top of the WordPress platform. It allows you to effortlessly integrate your social network activity across the web with your blog.

Out of the box, Lifestream is just streams in RSS/Atom feeds and prettying them up, but deep down it's a very flexible platform allowing developers to integrate any kind of activity they desire.

Though the name may be a little "edgy" for some, SexyBookmarks has proven time and time again to be an extremely useful and successful tool in getting your readers to actually submit your articles to numerous social bookmarking sites.

Ultimate Category Excluder
Allows you to quickly and easily exclude categories from your front page, archives, and feeds. Just select which categories you want to be excluded, and UCE does all the work for you!

WP Greet Box
This plugin lets you show a different greeting message to your new visitors depending on their referrer url. For example, when a Digg user clicks through from Digg, they will see a message reminding them to digg your post if they like it. Another example, when a visitor clicks through from Twitter, they will see a message suggesting them to twit the post and follow you on Twitter. You can also set a default greeting message for new visitors (not matching any referrer URLs) suggesting them to subscribe to your RSS feed.