1

I have this function:

function attachment_selectbox_edit($form_fields, $post) {

$select_options = array(
    "no" => "No",
    "yes" => "Yes"
);

// get the current value of our custom field
$current_value = get_post_meta($post->ID, "_my_select", true);

// build the html for our select box
$mySelectBoxHtml = "<select name='attachments[{$post->ID}][my_select]' id='attachments[{$post->ID}][my_select]'>";
foreach($select_options as $value => $text){

    // if this value is the current_value we'll mark it selected
    $selected = ($current_value == $value) ? ' selected ' : '';

    // escape value for single quotes so they won't break the html
    $value = addcslashes( $value, "'");

    $mySelectBoxHtml .= "<option value='{$value}' {$selected}>{$text}</option>";
}
$mySelectBoxHtml .= "</select>";

// add our custom select box to the form_fields
$form_fields["my_select"]["label"] = __("Move the file?");
$form_fields["my_select"]["input"] = "html";
$form_fields["my_select"]["html"] = $mySelectBoxHtml;

return $form_fields;
}
add_filter("attachment_fields_to_edit", "attachment_selectbox_edit", null, 2);

I want to move the file from "uploads" directory to "uploads/new-directory" when $current_value == 'yes'.

Can you help me?

5
  • is /new-directory exists?
    – Jevuska
    Apr 10, 2016 at 2:58
  • Yes, it exists. Any ideas to move the file? Apr 11, 2016 at 14:03
  • I don't think will work. You'll got issue if there are too many crop images in old folder that need to move progmatically in WP.
    – Jevuska
    Apr 12, 2016 at 7:30
  • on what page are you intending to use this form?
    – majick
    Apr 12, 2016 at 12:55
  • I want to use it when editing an attachment of the media library. It is posible to edit title, caption, description... but I would like the option of moving the file from one folder to another. Apr 12, 2016 at 17:15

1 Answer 1

2

Can't seem to track down the right filter to process this in just yet (where the edit form is being saved), especially since attachment_fields_to_edit can be used in multiple places. But maybe you already know? This will give you a good start anyway...

add_filter('??attachment_fields_save_filter??','move_attachment_directory',10,2);

function move_attachment_directory($form_fields,$post) {

$attach_id = $post->ID;
if (!isset($_POST['attachments'][$attach_id]['my_select'])) {return;}
if ($_POST['attachments'][$attach_id]['my_select'] == 'no') {return;}

// set directories
$uploaddir = wp_upload_dir();
$newdirectory = 'new-directory'; // or whatever it is

// get attachment metadata
$attachdata = wp_get_attachment_metadata($attach_id);

// move original image
$filepath = trailingslashit($uploaddir).$attachdata['file'];
$newfilepath = trailingslashit($uploaddir).trailingslashit($newdirectory).$attachdata['file'];
rename($filepath,$newfilepath);
$attachdata['file'] = trailingslashit($newdirectory).$attachdata['file'];

// move each attachment size too
foreach ($attachdata['sizes'] as $size => $data) {
    $file = $data['file'];
    $filepath = trailingslashit($uploaddir).$data['file'];
    $newfilepath = trailingslashit($uploaddir).trailingslashit($newdirectory).$data['file'];
    rename($filepath,$newfilepath);
    $data['file'] = trailingslashit($newdirectory).$data['file'];
    $attachdata['sizes'][$size] = $data;
}

// update attachment metadata
wp_update_attachment_metadata($attach_id,$attachdata);

}

Note: I don't think this will work as is if you have media sorted by month/year on.

In any case this is untested code and may have path bugs etc., so may need some work in a test environment, but it shows it can be done and hopefully it gets you to the next step...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.