Advertisement
helgatheviki

Ninja Forms Image Attachments

Apr 29th, 2013
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.37 KB | None | 0 0
  1. // remove ninja forms handling of featured image
  2. add_action ( 'init', 'kia_tweak_ninja_forms' );
  3. function kia_tweak_ninja_forms(){
  4.     remove_action( 'init', 'ninja_forms_register_set_featured_image' );
  5. }
  6.  
  7. // attach ALL the uploaded images
  8. add_action( 'ninja_forms_create_post', 'ninja_forms_prepare_attach_uploads' );
  9. function ninja_forms_prepare_attach_uploads( $post_id ){
  10.     global $ninja_forms_processing;
  11.     $upload_id = '';
  12.     foreach( $ninja_forms_processing->get_all_fields() as $field_id => $user_value ){
  13.         $field_row = ninja_forms_get_field_by_id( $field_id );
  14.         $field_type = $field_row['type'];
  15.         $field_data = $field_row['data'];
  16.         if( $field_type == '_upload' ){
  17.  
  18.             $set_featured_image = isset( $field_data['featured_image'] ) AND $field_data['featured_image'] == 1  ? TRUE : FALSE;
  19.  
  20.             $upload_id = $field_id;
  21.  
  22.         }
  23.     }
  24.     if( $upload_id != '' ){
  25.  
  26.         $user_value = $ninja_forms_processing->get_field_value( $upload_id );
  27.  
  28.         $counter = 1;
  29.         if ( $user_value ) foreach ( $user_value as $user_value ){
  30.             $file_name = $user_value['file_path'].$user_value['file_name'];
  31.  
  32.             $feature_this = ( 1 == $counter && $set_featured_image ) ? TRUE: FALSE;
  33.  
  34.             ninja_forms_attach_uploads( $post_id, $file_name, $feature_this );
  35.  
  36.             $counter++;
  37.         }
  38.  
  39.         // hard-coded, if more than 1 image attached add category id=66, 'submitted-gallery'
  40.         if ( $counter > 1 && in_category( 65, $post_id ) ) {
  41.                 wp_set_post_categories( $post_ID, array( 65, 66 ) );
  42.                 // also change the post format
  43.                 set_post_format( $post_id, 'gallery' );
  44.             }
  45.  
  46.     }
  47. }
  48.  
  49. function ninja_forms_attach_uploads( $post_id, $filename, $feature_this ) {
  50.     $wp_filetype = wp_check_filetype( basename( $filename ), null );
  51.     $attachment = array(
  52.         'post_mime_type' => $wp_filetype['type'],
  53.         'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
  54.         'post_content' => '',
  55.         'post_status' => 'inherit'
  56.     );
  57.     $attach_id = wp_insert_attachment( $attachment, $filename, $post_id );
  58.     // you must first include the image.php file
  59.     // for the function wp_generate_attachment_metadata() to work
  60.     require_once(ABSPATH . "wp-admin" . '/includes/image.php');
  61.     $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
  62.     if ( wp_update_attachment_metadata( $attach_id,  $attach_data ) && $feature_this ) {
  63.         // set as featured image
  64.         update_post_meta($post_id, '_thumbnail_id', $attach_id);
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement