Generate vCard from Custom fields
- This topic has 2 replies, 2 voices, and was last updated 3 years, 4 months ago by
Ghislain Malardier.
-
AuthorPosts
-
August 7, 2021 at 8:03 AM #29984
Ghislain Malardier
ParticipantHi,
I'm looking for a way to automatically generate a vCard from specific custom fields when a post is saved.Then to have the url of that vCard saved in a custom field as well.
Does someone already done something similar?
I don't have extended coding knowledge, so looking for guidance!
Thanks.
August 7, 2021 at 7:20 PM #30008Long Nguyen
ModeratorHi,
You can use the meta box action
rwmb_{$field_id}_after_save_field
then update another field value with the function update_post_meta().Get more details on the documentation https://docs.metabox.io/actions/#rwmb_after_save_field
Refer to these topics https://support.metabox.io/topic/use-rwmb_frontend_after_save_post/
https://support.metabox.io/topic/using-custom-meta-fields-to-create-cpt-title/December 16, 2021 at 12:44 PM #32675Ghislain Malardier
ParticipantHi Long,
Sorry I never reply to this post!I'm actually trying agin to create the vCard with the little knowledge I got, but still not working. Right now I'm just trying to save the info in the vCard, but no file is created when saving posts. What am I missing?
Here is the code:
function otc_create_vcard( $post_id ) { /* * Get basic metabox values */ $post_type = get_post_type( $post_id ); $featured_image = wp_get_attachment_url(get_post_thumbnail_id($vpost->ID)); $otc_meta_name = rwmb_meta( 'ccard_main_contact', '', $post_id ); $otc_meta_business_name = rwmb_meta('ccard_business_name', '', $post_id ); $otc_meta_job_title = rwmb_meta('ccard_job_title', '', $post_id ); $otc_meta_business_email = rwmb_meta('ccard_business_email', '', $post_id ); $otc_meta_cell_phone = rwmb_meta( 'ccard_cellular_phone_number', '', $post_id ); $otc_meta_office_phone = rwmb_meta( 'ccard_business_phone_number', '', $post_id ); $otc_meta_website = rwmb_meta( 'ccard_website', '', $post_id ); $otc_meta_address_street = rwmb_meta('ccard_business_address_street', '', $post_id ); $otc_meta_address_city = rwmb_meta( 'ccard_business_address_city', '', $post_id ); $otc_meta_address_state = rwmb_meta( 'ccard_business_address_state', '', $post_id ); $otc_meta_address_zipcode = rwmb_meta( 'ccard_business_address_zip-code', '', $post_id ); $otc_meta_address_country = rwmb_meta( 'ccard_business_address_country', '', $post_id); $otc_page_url = get_permalink(); // only update the user custom post type on save //if ( "card" != $post_type ) return; $vpost = get_post($post->ID); $filename = strtolower(str_replace(" ","-",$vpost->post_title)).".vcf"; ('Content-type: text/x-vcard; charset=utf-8'); header("Content-Disposition: attachment; filename=".$filename); $data=null; $data.="BEGIN:VCARD\n"; $data.="VERSION:3.0\n"; $data.="FN:".$vpost->post_title."\n"; // get post title $data.="ORG: ".$otc_meta_business_name."\n"; $data.="TITLE: ".$otc_meta_job_title."\n"; $data.="EMAIL;TYPE=work: ".$otc_meta_business_email."\n"; $data.="TEL;type=CELL;type=VOICE;: ".$otc_meta_cell_phone."\n"; $data.="TEL;type=Office;type=VOICE;: ".$otc_meta_office_phone."\n"; $data.="ADR;Location;PREF: ".$otc_meta_address_street.";".$otc_meta_address_city.";".$otc_meta_address_state.";".$otc_meta_address_zipcode.";".$otc_meta_address_country."/n"; $data.="PHOTO;ENCODING=b;TYPE=JPEG:".$featured_image."/n"; $data.="END:VCARD"; $filePath = get_template_directory()."/vcard/".$filename; // you can specify path here where you want to store file. $file = fopen($filePath,"w"); fwrite($file,$data); fclose($file); } add_action( 'save_post', 'otc_create_vcard' );
-
AuthorPosts
- You must be logged in to reply to this topic.