• Hello, I have the following function and action that creates a VCARD file on post save for a specific custom post type. It works great – except that it is firing when I click the “Add New” button for the specified post type – which prevents the creation of new posts:

    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);
    
        // If this isn't an attorney cpt post, don't update it.
    	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 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:CLIENT ADDRESS\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' );

    Looking around stack I thought this is what I am supposed to do, but it doesn’t work. When I click the “Add New” button it still just downloads a screwed up vcard in the backend and prevents the creation of a new post in that post type.

    function hj_create_vCard( $post_id ) {
    if (isset($post->post_status) && 'publish' == $post->post_status) {
        return;
      }
    /*
         * 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);
    
        // If this isn't an attorney cpt post, don't update it.
    	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 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:CLIENT ADDRESS\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' );

    Any help anyone could provide would be much appreciated. I am very, very stuck!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Why are you setting the headers at all?

    solved this? I want to make the download from frontend and same function.

    • This reply was modified 5 years, 11 months ago by xeonz.
    Thread Starter pierrehooker

    (@pierrehooker)

    Hi @xeonz yes not setting headers was the fix.

    @pierrehooker Ok nice! Thx! I got it to work now but only when I save the post/ update post in backend. I need to run this from begging when creating the post with WPUF in frontend.

    Any idea how to do activate script on first save or save from frontend of WP?

    Update:

    I got this to work with changing

    $vpost = get_post($post->ID);

    to

    $vpost = get_post($post_id);

    because I think WPUF not compitable with $post->ID

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘save_post action but not on post creation?’ is closed to new replies.