Support

Account

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

Solving

Dynamically create vcard from ACF data and download with button click?

  • Hello,

    Ditto the subject pretty much. I can find a lot of info on how to generate a vcard file with PHP, but nothing really says how to make this happen by clicking a button. I don’t want to store a copy locally, or generate a new file on post save or any nonsense like that. Click the button, it builds the file and downloads with a filename that mirrors the post slug.

  • So I did end up just creating the vCard upon saving the custom post type by cobbling together a few different methods. I started with this function:

    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 ( "attorneys" != $post_type ) return;
    
        $vpost = get_post($post->ID);
        $filename = $vpost->post_name.".vcf";
        header('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:Client Company Name\n";
        $data.="EMAIL;TYPE=work:" .get_field('att_email',$vpost->ID)."\n";  // get acf field value
        $data.="TEL;WORK;VOICE:" .get_field('att_phone',$vpost->ID)."\n";  // get acf field value
        $data.="ADR;WORK;PREF:123 Fake Street;Fake City;MN;55106\n";  // get acf field value
        $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' );

    Then for use in my template I just grab the file that is generated on save by looking for the file based on its name, which is generated from the page slug:

    <?php
    global $post;
    $vcfname = $post->post_name;
    ?>
    
    <a href="<?php echo get_template_directory_uri(); ?>/vcard/<?php echo $vcfname; ?>.vcf"><i class="far fa-address-card"></i> Download Vcard</a>
    • treon

    • March 21, 2019 at 8:32 pm

    Hi,

    Im trying to do the same. You got this working??

    I was try this code put first part in my template functions.php and secound part in post with shortcode snippet.

    I get this error
    The page can’t be found.
    It looks like nothing was found at this location.

  • @pierrebalian Thanks so much for this, worked a treat and was easily tweaked for it’s intended purpose on a website i’m working on.

  • @pierrebalian this is awesome and works wonderfully except for one thing: when you attempt to add a new post of the specified custom post type the browser attempts to download a .vcf file. No file name, as I assume it’s trying to download a file with “post_name.vcf” but there’s not post_name to reference so it’s just “.vcf”.

    Did you or @pixelsandthings figure out a workaround for this glitch?

    Thanks!

  • Hi @shawnrosspeters

    Just unset the two header lines in the main function. That is the cause of the problem.

    //header('Content-type: text/x-vcard; charset=utf-8');
    //header("Content-Disposition: attachment; filename=".$filename);
  • @pierrebalian that worked! Thanks!

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.