taxonomy === 'wpmf-category') { $currentFolder = $folder->term_id; } } } if (!empty($current_folder) && substr($post->post_mime_type, 0, 5) === 'image') { $option_bgfolder = get_option('wpmf_field_bgfolder'); $name = 'attachments[' . $post->ID . '][wpmf_field_bgfolder]'; $id = 'attachments-' . $post->ID . '-wpmf_field_bgfolder'; $class = 'wpmf_field_bgfolder'; if (!empty($option_bgfolder) && !empty($current_folder) && !empty($option_bgfolder[$currentFolder]) && (int) $option_bgfolder[$currentFolder][0] === $post->ID ) { $html = ''; } else { $html = ''; } $form_fields['wpmf_field_bgfolder'] = array( 'label' => __('Folder cover', 'wpmf'), 'input' => 'html', 'html' => $html ); } } return $form_fields; } /** * Save background for folder * * @param array $post An array of post data. * @param array $attachment An array of attachment metadata. * * @return mixed $post */ public function attachmentFieldsToSave($post, $attachment) { if (empty($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'nonce')) { return $post; } // Retrieve previous image covers $cover_images = get_option('wpmf_field_bgfolder'); // Define array if not yet any cover image defined if (empty($cover_images)) { $cover_images = array(); } // Retrieve the current folder the post is in if (isset($_POST['wpmf_folder'])) { $current_folder_id = (int) $_POST['wpmf_folder']; if (isset($attachment['wpmf_field_bgfolder']) && $attachment['wpmf_field_bgfolder'] === 'on') { // This image should be the cover image // Retrieve the thumbnail image $image_thumb = wp_get_attachment_image_src($post['ID'], 'thumbnail'); // Affect post ID and image thumbnail to the folder $cover_images[$current_folder_id] = array((int) $post['ID'], $image_thumb[0]); $post['wpmf_cover_updated'] = true; } elseif (isset($cover_images[$current_folder_id]) && (int) $cover_images[$current_folder_id][0] === $post['ID']) { // Delete the cover for this folder unset($cover_images [$current_folder_id]); } update_option('wpmf_field_bgfolder', $cover_images); } return $post; } }