Support

Account

Home Forums General Issues Dynamically create vcard from ACF data and download with button click? Reply To: Dynamically create vcard from ACF data and download with button click?

  • I’ve gotten this to work perfectly with the information above, however I also want to have the image as a vcard item. so.. the image has to be set to BASE64 format, after some googling I also got that to work.. so finally I updated my profile and yess! the image is in the vcard after updating the page. But.. one big problem; I’m not able to create new pages, I get the error: Path cannot be empty in function $image = file_get_contents($path);

    This is what I got:

    function imageEncode($path)
    {
    $path  = __DIR__."/".$path;
    $image = file_get_contents($path);
    $finfo = new finfo(FILEINFO_MIME_TYPE);
    $type  = $finfo->buffer($image);
    return "data:".$type.";charset=utf-8;base64,".base64_encode($image);
    }
    
    function imageEncodePath($path)
    {
    $image = file_get_contents($path);
    $finfo = new finfo(FILEINFO_MIME_TYPE);
    $type  = $finfo->buffer($image);
    return "data:".$type.";charset=utf-8;base64,".base64_encode($image);
    }
    
    function imageEncodeURL($path)
    {
    $image = file_get_contents($path);
    $finfo = new finfo(FILEINFO_MIME_TYPE);
    $type  = $finfo->buffer($image);
    return "PHOTO;TYPE=".$type.";ENCODING=BASE64:".base64_encode($image);
    }
    
    function hj_create_vCard( $post_id ) {
    
    	/*
         * In production code, $slug should be set only once in the plugin,
         * preferably as a class property, rather than in each function that needs it.
         */
        $post_type = get_post_type($post_id);
    
    	
        // only update the attorneys custom post type on save
        if ( "page" != $post_type ) return;
    
        $vpost = get_post($post_id);
        $filename = $vpost->post_name.".vcf";
       	$today = date('Y-m-d'); 
        $data=null;
    	$data.="BEGIN:VCARD\n";
    	$data.="VERSION:3.0\n";
    	$data.="REV:".$today."T00:00:00Z\n";
    	$data.="N:" .get_field('achternaam',$vpost->ID).";" .get_field('voornaam',$vpost->ID).";;;\n";
    	$data.="ORG:" .get_field('bedrijf',$vpost->ID)."\n";
    	$data.="TITLE:" .get_field('functie',$vpost->ID)."\n";
    	$data.="EMAIL;INTERNET;WORK:" .get_field('email_adres',$vpost->ID)."\n";
    	$data.="TEL;WORK:" .get_field('telefoonnummer',$vpost->ID)."\n";
    	$data.="ADR;WORK:" .get_field('adres',$vpost->ID)."\n";
    	$data.="URL;WORK:" .get_field('website',$vpost->ID)."\n";
    	$data.="URL;OTHER:" .get_field('linkedin',$vpost->ID)."\n";
    	$data.="URL;OTHER:" .get_field('facebook',$vpost->ID)."\n";
    	$data.="URL;OTHER:" .get_field('twitter',$vpost->ID)."\n";
    	$data.="URL;OTHER:" .get_field('instagram',$vpost->ID)."\n";
    	$data.="URL;OTHER:" .get_field('snapchat',$vpost->ID)."\n";
    	$data.="" .imageEncodeURL(get_field('foto',$vpost->ID))."\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', 'hj_create_vCard' );

    I suppose its quiet an easy fix for someone with a little bit php knowledge, seems I can only copy paste ;(

    Hope someone is willing to help me out!