/** * THESE FUNCTIONS ALLOW FOR ATTACHMENTS THAT BELONG TO PAGES TO BE REASSIGNED BETWEEN PAGES ON THE MEDIA EDIT SCREEN * */ /** * * @param array $form_fields * @param object $post * @return array */ function my_image_attachment_fields_to_edit($form_fields, $post) { // only activate for images that already attached to pages, ignore images attached to posts if (get_post_type($post->post_parent) == 'page') { // get the list of pages for our select box $all_pages = get_pages(); $select_code = get_pages_as_select_field($post, $all_pages); // $form_fields is a special array of fields to include in the attachment form // $post is the attachment record in the database // $post->post_type == 'attachment' // (attachments are treated as posts in WordPress) // add our custom field to the $form_fields array // input type="text" name/id="attachments[$attachment->ID][custom1]" $form_fields["post_parent"] = array( "label" => __("Attatched to page"), "input" => "html", "html" => $select_code ); } return $form_fields; } /** * * @param object $post * @param object $all_pages * @return string */ function get_pages_as_select_field($post, $all_pages) { $content = ""; return $content; } // attach our function to the correct hook add_filter("attachment_fields_to_edit", "my_image_attachment_fields_to_edit", null, 2); /** * @param array $post * @param array $attachment * @return array */ function my_image_attachment_fields_to_save($post, $attachment) { if( isset($attachment['post_parent']) ){ if( trim($attachment['post_parent']) == '' ){ // adding our custom error $post['errors']['post_parent']['errors'][] = __('No value found for post_parent.'); }else{ $post['post_parent'] = $attachment['post_parent']; } } return $post; } add_filter("attachment_fields_to_save", "my_image_attachment_fields_to_save", null, 2);