Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save divsbhalala/cca87a912e52d376ff279819a76f7b00 to your computer and use it in GitHub Desktop.
Save divsbhalala/cca87a912e52d376ff279819a76f7b00 to your computer and use it in GitHub Desktop.

Revisions

  1. @saltnpixels saltnpixels revised this gist Dec 5, 2016. 1 changed file with 34 additions and 30 deletions.
    64 changes: 34 additions & 30 deletions save_upload_field_to_custom_field.php
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,12 @@

    add_action( 'gform_after_create_post', 'gf_add_to_media_library', 10, 3 );

    /**
    * Save file upload fields under custom post field to the library
    *
    * @param int $post_id The post identifier
    * @param array $entry The entry
    * @param array $form The form
    * @param $post_id The post identifier
    * @param $entry The entry
    * @param $form The form
    */
    function gf_add_to_media_library ( $post_id, $entry, $form ) {
    foreach($form['fields'] as $field){
    @@ -20,38 +21,41 @@ function gf_add_to_media_library ( $post_id, $entry, $form ) {
    $entry_id = $field['id'];
    $files = rgar ( $entry, $entry_id );
    $custom_field = $field['postCustomFieldName']; //custom field key

    //if file field is not empty
    if ( $files !== ''){

    //if file field is not empty or not []
    if ( $files !== '' && $files !== "[]"){

    $patterns = ['[', ']', '"']; //get rid of crap
    $file_entry = str_replace($patterns, '', $files);
    $files = explode ( ',', $file_entry );

    foreach ($files as $file) {
    //each file is a url
    //get the filename from end of url in match[1]
    $filename = pathinfo($file, PATHINFO_FILENAME);
    //add to media library
    //WordPress API for image uploads.
    include_once( ABSPATH . 'wp-admin/includes/image.php' );
    include_once( ABSPATH . 'wp-admin/includes/file.php' );
    include_once( ABSPATH . 'wp-admin/includes/media.php' );

    $new_url = stripslashes($file);
    $result = media_sideload_image( $new_url, $post_id, $filename, 'src');
    //saving the image to field or thumbnail
    if( strpos($field['cssClass'], 'thumb') == false ){
    $attachment_ids[] = (int) get_attachment_id_from_src($result);
    }
    else{
    set_post_thumbnail($post_id, (int) get_attachment_id_from_src($result) );
    }
    foreach ($files as $file) {
    //each file is a url
    //get the filename from end of url in match[1]
    $filename = pathinfo($file, PATHINFO_FILENAME);
    //add to media library
    //WordPress API for image uploads.
    include_once( ABSPATH . 'wp-admin/includes/image.php' );
    include_once( ABSPATH . 'wp-admin/includes/file.php' );
    include_once( ABSPATH . 'wp-admin/includes/media.php' );

    } //end foreach file

    update_post_meta ($post_id, $custom_field, $attachment_ids);
    } //end if files not empty
    } //end if custom field of uploadfile
    $new_url = stripslashes($file);
    $result = media_sideload_image( $new_url, $post_id, $filename, 'src');
    //saving the image to field or thumbnail

    if( strpos($field['cssClass'], 'thumb') === false ){
    $attachment_ids[] = (int) get_attachment_id_from_src($result);
    }
    else{
    set_post_thumbnail($post_id, (int) get_attachment_id_from_src($result) );
    }

    } //end foreach file
    if ( isset( $attachment_ids ) ){
    update_post_meta ($post_id, $custom_field, $attachment_ids);
    }
    } //end if files not empty
    } //end if custom field of uploadfile
    }
    } //end for each form field

  2. @saltnpixels saltnpixels revised this gist Dec 5, 2016. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions save_upload_field_to_custom_field.php
    Original file line number Diff line number Diff line change
    @@ -3,9 +3,9 @@
    /**
    * Save file upload fields under custom post field to the library
    *
    * @param <type> $psost_id The post identifier
    * @param <type> $entry The entry
    * @param <type> $form The form
    * @param int $post_id The post identifier
    * @param array $entry The entry
    * @param array $form The form
    */
    function gf_add_to_media_library ( $post_id, $entry, $form ) {
    foreach($form['fields'] as $field){
  3. @saltnpixels saltnpixels revised this gist Dec 5, 2016. 1 changed file with 13 additions and 9 deletions.
    22 changes: 13 additions & 9 deletions save_upload_field_to_custom_field.php
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,3 @@

    add_action( 'gform_after_create_post', 'gf_add_to_media_library', 10, 3 );

    /**
    @@ -40,16 +39,21 @@ function gf_add_to_media_library ( $post_id, $entry, $form ) {

    $new_url = stripslashes($file);
    $result = media_sideload_image( $new_url, $post_id, $filename, 'src');
    //saving the image to the post.

    $attachment_ids[] = (int) get_attachment_id_from_src($result);
    } //end foreach
    //saving the image to field or thumbnail
    if( strpos($field['cssClass'], 'thumb') == false ){
    $attachment_ids[] = (int) get_attachment_id_from_src($result);
    }
    else{
    set_post_thumbnail($post_id, (int) get_attachment_id_from_src($result) );
    }

    } //end foreach file

    update_post_meta ($post_id, $custom_field, $attachment_ids);
    } //end if file is not empty
    }
    }
    }
    } //end if files not empty
    } //end if custom field of uploadfile
    }
    } //end for each form field


    function get_attachment_id_from_src($image_src) {
  4. @saltnpixels saltnpixels created this gist Dec 5, 2016.
    61 changes: 61 additions & 0 deletions save_upload_field_to_custom_field.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,61 @@

    add_action( 'gform_after_create_post', 'gf_add_to_media_library', 10, 3 );

    /**
    * Save file upload fields under custom post field to the library
    *
    * @param <type> $psost_id The post identifier
    * @param <type> $entry The entry
    * @param <type> $form The form
    */
    function gf_add_to_media_library ( $post_id, $entry, $form ) {
    foreach($form['fields'] as $field){

    //get media upload dir
    $uploads = wp_upload_dir();
    $uploads_dir = $uploads['path'];
    $uploads_url = $uploads['url'];

    //if its a custom field with input type file upload.
    if( $field['type'] == 'post_custom_field' && $field['inputType'] == 'fileupload'){
    $entry_id = $field['id'];
    $files = rgar ( $entry, $entry_id );
    $custom_field = $field['postCustomFieldName']; //custom field key

    //if file field is not empty
    if ( $files !== ''){
    $patterns = ['[', ']', '"']; //get rid of crap
    $file_entry = str_replace($patterns, '', $files);
    $files = explode ( ',', $file_entry );

    foreach ($files as $file) {
    //each file is a url
    //get the filename from end of url in match[1]
    $filename = pathinfo($file, PATHINFO_FILENAME);
    //add to media library
    //WordPress API for image uploads.
    include_once( ABSPATH . 'wp-admin/includes/image.php' );
    include_once( ABSPATH . 'wp-admin/includes/file.php' );
    include_once( ABSPATH . 'wp-admin/includes/media.php' );

    $new_url = stripslashes($file);
    $result = media_sideload_image( $new_url, $post_id, $filename, 'src');
    //saving the image to the post.

    $attachment_ids[] = (int) get_attachment_id_from_src($result);
    } //end foreach

    update_post_meta ($post_id, $custom_field, $attachment_ids);
    } //end if file is not empty
    }
    }
    }


    function get_attachment_id_from_src($image_src) {

    global $wpdb;
    $query = "SELECT ID FROM {$wpdb->posts} WHERE guid='$image_src'";
    $id = $wpdb->get_var($query);
    return $id;
    }