I am using a custom post type to enter employees for a company, and want people to be able to download vcards for each employee, generated from the custom meta fields for each employee, but I'm really stuck here, how would I do this?
The code for the custom post type is
add_action('init', 'ansatt_register');
function ansatt_register() {
$labels = array(
'name' => _x('Ansatt', 'post type general name'),
'singular_name' => _x('Ansatt', 'post type singular name'),
'add_new' => _x('Legg til', 'ansatt'),
'add_new_item' => __('Legg til ny ansatt'),
'edit_item' => __('Rediger ansatt.'),
'new_item' => __('Ny ansatt.'),
'view_item' => __('Vis ansatt'),
'search_items' => __('Søk i ansatte'),
'not_found' => __('Ingenting funnet'),
'not_found_in_trash' => __('Ingenting funnet i søppel'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'menu_icon' => get_stylesheet_directory_uri() . '/article16.png',
'rewrite' => true,
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title','editor','thumbnail')
);
register_post_type( 'ansatte' , $args );
}
and the meta fields
function ansatte_meta() {
global $post;
$custom = get_post_custom($post->ID);
$firstname = $custom["firstname"][0];
$lastname = $custom["lastname"][0];
$born = $custom["born"][0];
$about = $custom["about"][0];
$jobtitle = $custom["jobtitle"][0];
$mail = $custom["mail"][0];
$phone = $custom["phone"][0];
$cvlink = $custom["cvlink"][0]; ?>
<p><label>First name:</label><br />
<input type="text" name="firstname" value="<?php echo $firstname; ?>"></input></p>
<p><label>Lastname:</label><br />
<input type="text" cols="50" rows="1" name="lastname" value="<?php echo $lastname; ?>"></input></p>
<p><label>Born year:</label><br />
<input type="text" cols="50" rows="1" name="born" value="<?php echo $born; ?>"></input></p>
<p><label>Short text about employee:</label><br />
<textarea cols="50" rows="5" name="about"><?php echo $about; ?></textarea></p>
<p><label>Job title:</label><br />
<input type="text" name="jobtitle" value="<?php echo $jobtitle; ?>"></input></p>
<p><label>Phone no:</label><br />
<input type="text" cols="50" rows="1" name="phone" value="<?php echo $phone; ?>"></input></p>
<p><label>E-mail adress:</label><br />
<input type="text" cols="50" rows="1" name="mail" value="<?php echo $mail; ?>"></input></p>
<p><label>Link to CV:</label><br />
<input type="text" cols="50" rows="1" name="cvlink" value="<?php echo $cvlink; ?>"></input></p>
<?php
}
register_taxonomy("firma", array("ansatte"), array("hierarchical" => true, "label" => "Firma", "singular_label" => "Firma", "rewrite" => true));
John Cotton answers:
Basically, you need to use set the content type of the http return and then write out what ever content you want.
Probably the best way is the extent the template that you're using to display each employee. I guess for that you've got a single-ansatte.php file?
If so, then make your link for the vcard something like http://www.your-site.com/ansatte/employee-name?vcard=1 (ie whatever the current url is with ?vcard=1 on the end).
Then BEFORE your get_header() call put something along the following lines:
if( isset($_GET['vcard']) ) {
global $post;
$custom = get_post_custom($post->ID);
header('Content-type: text/x-vcard; charset=utf-8');
header('Content-Disposition: attachment; filename="vcard.vcf"');
echo "BEGIN:VCARD\n";
echo "VERSION:2.1\n";
echo 'FN:'.$custom["firstname"][0] .' '.$custom["lastname"][0]."\n";
echo 'TITLE:'.$custom["jobtitle"][0]."\n";
echo 'TEL;WORK;VOICE:'.$custom["phone"][0]."\n";
echo 'EMAIL;PREF;INTERNET:'.$custom["mail"][0]."\n";
/// etc for each field
echo "END:VCARD\n";
exit();
}
Clearly, you have more to show so you'd need to check out the complete spec to write each of the lines you want.
UPDATE: Added a new line after each echo as pointed out by Torstein (just in case anyone is copying this code).
Torstein Opperud comments:
Yeah, I'm using a "single-ansatte.php"-file to show each individual employee, but when I added your code I got a
<blockquote>Parse error: syntax error, unexpected '{' in /home/industri/public_html/ny/wp-content/themes/industrifinans/single-ansatte.php on line 10</blockquote>
Now it looks like this:
<?php
/**
* Mal for ansattes CV.
*
* @package WordPress
*/
// er det vcard som skal lastes ned?
if( isset( $_GET['vcard'] ) {
global $post;
$custom = get_post_custom($post->ID);
header('Content-type: text/x-vcard; charset=utf-8');
header('Content-Disposition: attachment; filename="vcard.vcf"');
echo 'BEGIN:VCARD';
echo 'VERSION:2.1';
echo 'FN:' $custom["firstname"][0] .' '.$custom["lastname"][0];
echo 'TITLE:' .$custom["jobtitle"][0];
echo 'TEL;WORK;VOICE:.$custom["phone"][0];
echo 'EMAIL;PREF;INTERNET:'.$custom["mail"][0]';
/// etc for each field
echo 'END:VCARD';
exit();
}
// vcard slutt
get_header(); ?>
John Cotton comments:
This line was missing a full stop:
echo 'FN:'.$custom["firstname"][0] .' '.$custom["lastname"][0];
John Cotton comments:
And this line is missing a single quote !!
echo 'TEL;WORK;VOICE:'.$custom["phone"][0];
Torstein Opperud comments:
thanks :) but I'm still getting the same error, which seems to be about the first { in the if
John Cotton comments:
if( isset( $_GET['vcard'] ) ) {
Sorry, I was typing off the top of my head - I make mistakes doing that!!
Torstein Opperud comments:
Alright, there was another small error, but I solved that one myself :)
I just had to add a linebreak at the end of each line, like this:
echo 'FN:'.$custom["firstname"][0] .' '.$custom["lastname"][0]. "\n";
Thanks John :)
Jurre Hanema answers:
I think this could be realized best as a custom plug-in.
The way I see it, the plugin-should add a new page or URL to the site, say something like /get_vcard. This page should then be called with the ID of the employee you want to generate a vCard for, like /get_vcard?employee=123.
The plugin would then generate the vCard file using the custom metadata associated with the employee in question, set the content-type header to the right value and echo the vCard to the browser.
I think I should be able to come up with a reasonable solution to do this which works in the way described above. Please let me know if you want me to go ahead and write some code.
Torstein Opperud comments:
I'd really like it not to be a plugin, I'd rather just put it straight into the template...