我编写了一个函数来向图像添加和插入额外的元数据。看起来是这样的:
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);
写文章时,点击“插入媒体”,选择图像,并确保表单字段id集为我提供了正确的图像嵌入。单击“保存草稿/发布”时出现问题:
作为super admin (所讨论的博客是多站点设置的一部分),将作品按预期保存为administrator (对于该站点),它会删除我新创建的属性,有人能解释一下这里发生了什么吗?到目前为止,我已经花了2个小时试图回溯wp包含的迷宫。
我假设在保存帖子时有一些清理/过滤步骤,出于某种原因选择删除我的属性,但如果是这样的话,为什么只会发生在站点管理员(而不是超级管理员)身上?