Monday, 27 May 2013

Saving a post, content filtering and user levels

Saving a post, content filtering and user levels

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 a super admin (the blog in question is part of a multisite setup), saving works as intended
As a site admin, 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)?

No comments:

Post a Comment