Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I wrote a function to add and insert additional metadata to images. It looks like this:

function add_image_orientation_fields_to_edit($form_fields, $post) {
    $current_value = strtolower(get_post_meta($post->ID, 'orientation', true));
    $options = array('Portrait','Landscape');

    $html = "<select name='attachments[{$post->ID}][orientation]' id='attachments[{$post->ID}][orientation]'>";
    foreach($options as $value):
        $selected = ($current_value == strtolower($value)) ? ' selected="selected"' : '';
        $html .= sprintf('<option value="%s"%s>%s</option>', strtolower($value), $selected, $value);
    endforeach;
    $html .= "</select>";

    $form_fields["orientation"]["label"] = __("Orientation");
    $form_fields["orientation"]["input"] = "html";
    $form_fields["orientation"]["html"] = $html;

    return $form_fields;
}

function add_image_orientation_fields_save($post, $attachment) {
    if(isset($attachment['orientation']))
        update_post_meta($post['ID'], 'orientation', $attachment['orientation']);
    return $post;
}

function image_orientation_to_editor($html, $id, $caption, $title, $align, $url, $size, $alt) {
    $img_url = wp_get_attachment_url($id);
    $orientation = get_post_meta($id, 'orientation', true);
    if(isset($orientation)) $orientation = sprintf('data-orientation="%s"', $orientation);
    $html = sprintf('<img src="%s" alt="%s" %s/>', $img_url, $alt, $orientation);
    return $html;
}

add_filter("attachment_fields_to_edit", "add_image_orientation_fields_to_edit", null, 2);
add_filter("attachment_fields_to_save", "add_image_orientation_fields_save", null, 2);
add_filter("image_send_to_editor", "image_orientation_to_editor", null, 8);

When writing a post, hitting "Insert media", choosing an image and making sure the form field id set gives me a proper image embed. The problem arises when i hit save draft/publish:

  • As super admin (the blog in question is part of a multisite setup), saving works as intended
  • As administrator (for that site), it strips my newly created attribute

Can anyone explain what's happening here? I've so far spent 2 hours trying to backtrack the maze that is wp-includes.

I assume there's some sanitize/filter steps while saving the post that for a reason chooses to remove my attribue, but if so - why does it just happen for the site admin (and not super admin)?

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

I managed to figure it out myself (numbers of hours spent: way too high).

Initially I thought using the unfiltered_html capability for administrators would solve the issue. Even if it would (which it didn't), it would be somewhat of a shotgun approach to a revolver issue.

Upon further searching, I learned that wordpress has a global list of allowed tags/attributes. Adding my attribute to the image tag solved it:

add_action( 'init', 'add_data_orientation_to_posttags' );
function add_data_orientation_to_posttags() {
    global $allowedposttags;

    $tags = array( 'img' );
    $new_attributes = array( 'data-orientation' => array() );

    foreach ( $tags as $tag ) {
        if ( isset( $allowedposttags[ $tag ] ) && is_array( $allowedposttags[ $tag ] ) )
            $allowedposttags[ $tag ] = array_merge( $allowedposttags[ $tag ], $new_attributes );
    }
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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