Tet banner

Custom Fields
Re-Envisioned
Support General Generate vCard from Custom fields

  • Creator
    Topic
  • #29984
    Ghislain Malardier
    Participant

    Hi,
    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.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Author
    Replies
  • #30008
    Long Nguyen
    Moderator

    Hi,

    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://metabox.io/support/topic/use-rwmb_frontend_after_save_post/
    https://metabox.io/support/topic/using-custom-meta-fields-to-create-cpt-title/

    #32675
    Ghislain Malardier
    Participant

    Hi 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' );
    
Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.