Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // remove ninja forms handling of featured image
- add_action ( 'init', 'kia_tweak_ninja_forms' );
- function kia_tweak_ninja_forms(){
- remove_action( 'init', 'ninja_forms_register_set_featured_image' );
- }
- // attach ALL the uploaded images
- add_action( 'ninja_forms_create_post', 'ninja_forms_prepare_attach_uploads' );
- function ninja_forms_prepare_attach_uploads( $post_id ){
- global $ninja_forms_processing;
- $upload_id = '';
- foreach( $ninja_forms_processing->get_all_fields() as $field_id => $user_value ){
- $field_row = ninja_forms_get_field_by_id( $field_id );
- $field_type = $field_row['type'];
- $field_data = $field_row['data'];
- if( $field_type == '_upload' ){
- $set_featured_image = isset( $field_data['featured_image'] ) AND $field_data['featured_image'] == 1 ? TRUE : FALSE;
- $upload_id = $field_id;
- }
- }
- if( $upload_id != '' ){
- $user_value = $ninja_forms_processing->get_field_value( $upload_id );
- $counter = 1;
- if ( $user_value ) foreach ( $user_value as $user_value ){
- $file_name = $user_value['file_path'].$user_value['file_name'];
- $feature_this = ( 1 == $counter && $set_featured_image ) ? TRUE: FALSE;
- ninja_forms_attach_uploads( $post_id, $file_name, $feature_this );
- $counter++;
- }
- // hard-coded, if more than 1 image attached add category id=66, 'submitted-gallery'
- if ( $counter > 1 && in_category( 65, $post_id ) ) {
- wp_set_post_categories( $post_ID, array( 65, 66 ) );
- // also change the post format
- set_post_format( $post_id, 'gallery' );
- }
- }
- }
- function ninja_forms_attach_uploads( $post_id, $filename, $feature_this ) {
- $wp_filetype = wp_check_filetype( basename( $filename ), null );
- $attachment = array(
- 'post_mime_type' => $wp_filetype['type'],
- 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
- 'post_content' => '',
- 'post_status' => 'inherit'
- );
- $attach_id = wp_insert_attachment( $attachment, $filename, $post_id );
- // you must first include the image.php file
- // for the function wp_generate_attachment_metadata() to work
- require_once(ABSPATH . "wp-admin" . '/includes/image.php');
- $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
- if ( wp_update_attachment_metadata( $attach_id, $attach_data ) && $feature_this ) {
- // set as featured image
- update_post_meta($post_id, '_thumbnail_id', $attach_id);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement