WordPress 3.5 attachment_fields_to_edit and media_send_to_editor

In previous version i set a new check field into the media window with a simple code

 function ab_prettyphoto_attachment_fields_to_edit( $form_fields, $post ) {

    $my_form_fields = array(
     'ab_prettyphoto' => array(
        'label'     => __('Automatic Zoom on click', 'ab_prettyphoto'),
        'input'     => 'html',
        'html'      => "
        <input type="checkbox" name="ab_prettyphoto-{$post->ID}" id='ab_prettyphoto-{$post->ID}' value="1" />
        <label for="ab_prettyphoto-{$post->ID}">" . __('Enable zoom', 'ab_prettyphoto') . "</label>" )
);
if( $post->post_mime_type == 'image/jpeg' OR  $post->post_mime_type == 'image/gif' OR $post->post_mime_type == 'image/png' OR $post->post_mime_type == 'image/tiff')
  return array_merge( $form_fields, $my_form_fields );
else
  return $form_fields;
}
add_filter( 'attachment_fields_to_edit', 'ab_prettyphoto_attachment_fields_to_edit', 100, 2 );

And i send to the editor the value of the check with

function ab_prettyphoto_send_to_editor( $html, $send_id, $attachment ) {
 if( isset($_POST["ab_prettyphoto-$send_id"]) )
 $title=($_POST['attachments'][$send_id]['post_title']);
  return str_replace('<a', '<a rel="prettyPhoto-img" title="'.$title.'"', $html);
}
 else
  return $html;
 }

 add_filter( 'media_send_to_editor', 'ab_prettyphoto_send_to_editor', 66, 3 );

In the new WP35 the attachment_fields_to_edit work, but the media_send_to_editor not: into the $_POST array the key “ab_prettyphoto-$send_id” not exists and i have see with Firebug a wp-ajax.php call when i click on the checkbox.
The values of $_SPOST array into the ab_prettyphoto_send_to_editor function (launched by media_send_editor) are (the value as for example)

     Array
     (
      [nonce] => 125ece740a
      [attachment] => Array
      (
       [id] => 4
       [post_content] => 
       [post_excerpt] => 
       [url] => http://localhost:8888/wpn/wp-content/uploads/2012/12/fotohome.jpg
       [align] => left
       [image-size] => thumbnail
       [image_alt] => fotohome
      )

      [html] => <a href=\"http://localhost:8888/wpn/wp-content/uploads/2012/1/fotohome.jpg\"><img src width=\"150\" height=\"150\" alt=\"fotohome\" class=\"wp-image-4 alignleft size-thumbnail\" /></a>
      [post_id] => 1
      [action] => send-attachment-to-editor
  )

The $_POST[ab_pretthyphoto….] is not send

I have found some docs into the web about the problem but i have not f

2 s
2

So, there are some things which work differently (now).

I. What you’ve noticed, when you click the checkbox WordPress sends an Ajax request to ‘wp_ajax_save_attachment_compat()’ with some form data, if your checkbox is checked, this information will be part of the form data.

‘wp_ajax_save_attachment_compat()’ checks for $_REQUEST[‘attachments’] and $_REQUEST[‘attachments’][$id] and will fail if it does not exist.

This means you’ll have to change your input field to:

<input type="checkbox" name="attachments[$post->ID][ab_prettyphoto]" id='ab_prettyphoto-{$post->ID}' value="1" />

II. Now this will pass all checks in ‘wp_ajax_save_attachment_compat’ and arrives at a useful filter:
(this is line 1930 in ajax-actions.php)

$post = apply_filters( 'attachment_fields_to_save', $post, $attachment_data );

You can use this filter to add your field data as post meta to the attachment, later on, you will check if this post meta exist and do your action if true:

add_filter('attachment_fields_to_save', 'wpse76219_add_meta',10,2);
function wpse76219_add_meta($post, $attachment_data){

    // use this filter to add post meta if key exists or delete it if not
    if ( !empty($attachment_data['ab_prettyphoto']) && $attachment_data['ab_prettyphoto'] == '1' )
         update_post_meta($post['ID'], 'ab_prettyphoto', true);
    else
         delete_post_meta($post['ID'], 'ab_prettyphoto');

    // return $post in any case, things will break otherwise
    return $post;
}

III. Last step, use ‘media_send_to_editor’ to check if your meta data exists, if true, do string manipulation, else return original:

add_filter('media_send_to_editor', 'wpse76219_send_to_editor', 10,3);
function wpse76219_send_to_editor( $html, $id, $att)
{
    $is_set = get_post_meta($id,'ab_prettyphoto', true);
    if ($is_set and $is_set == '1')
        return str_replace('<a', '<a rel="prettyPhoto-img"', $html);
    else
        return $html;
}

Leave a Comment