我有以下过滤器,但不知道如何在将媒体附加到post时将自定义属性添加到图像字段。
实例
<img data-ext-link-title="" data-ext-link-url="">
功能。php
function pp_external_link_edit( $form_fields, $post ) {
$form_fields[\'pp-external-link-title\'] = array(
\'label\' => \'External Link Title\',
\'input\' => \'text\',
\'value\' => get_post_meta( $post->ID, \'pp_external_link_title\', true ),
\'helps\' => \'Link for button at bottom of pretty photo modal\',
);
$form_fields[\'pp-external-link-url\'] = array(
\'label\' => \'External Link URL\',
\'input\' => \'text\',
\'value\' => get_post_meta( $post->ID, \'pp_external_link_url\', true ),
\'helps\' => \'Link for button at bottom of pretty photo modal\',
);
return $form_fields;
}
add_filter( \'attachment_fields_to_edit\', \'pp_external_link_edit\', 10, 2 );
function pp_external_link_save( $post, $attachment ) {
if( isset( $attachment[\'pp-external-link-title\'] ) )
update_post_meta( $post[\'ID\'], \'pp_external_link_title\', $attachment[\'pp-external-link-title\']);
if( isset( $attachment[\'pp-external-link-url\'] ) )
update_post_meta( $post[\'ID\'], \'pp_external_link_url\', $attachment[\'pp-external-link-url\']);
return $post;
}
add_filter( \'attachment_fields_to_save\', \'pp_external_link_save\', 10, 2 );
最合适的回答,由SO网友:birgire 整理而成
我想知道您是否要修改插入图像的HTML,使用image_send_to_editor
或get_image_tag
过滤器?
如果是这样,那么这里有一个例子:
/**
* Add the data-ext-link-title and data-ext-link-url attributes to inserted images.
*/
add_filter( \'image_send_to_editor\',
function( $html, $id, $caption, $title, $align, $url, $size, $alt )
{
if( $id > 0 )
{
$ext_title = get_post_meta( $id, \'pp_external_link_title\', true );
$ext_url = get_post_meta( $id, \'pp_external_link_url\', true );
$data = sprintf( \' data-ext-link-title="%s" \', esc_attr( $ext_title ) );
$data .= sprintf( \' data-ext-link-url="%s" \', esc_url( $ext_url ) );
$html = str_replace( "<img src", "<img{$data}src", $html );
}
return $html;
}
, 10, 8 );
这里我想你想要空的
data-ext-link-title
或
data-ext-link-url
属性,如果相应的元值为空或缺少。
希望您可以根据自己的需要进行调整。
SO网友:Privateer
前几天我也在做类似的事情,无论我怎么做都无法让attachment\\u fields\\u to\\u save工作。
我最终使用了edit\\u附件过滤器,并使用了如下代码:
public function action_edit_attachment( $attachment_id ) {
# Check to make sure we were sent by the side of the media editor, not by the real edit form
# - if the real edit form was used, action would be editpost instead
if ( isset($_REQUEST[\'action\']) ) {
$our_fields = array(
\'pp-external-link-title\' => \'text\',
\'pp-external-link-url\' => \'text\'
);
foreach ( $our_fields as $field_name => $input_type ) {
if ( isset( $_REQUEST[\'attachments\'][$attachment_id]["{$field_name}"] ) ) {
switch ( "{$input_type}" ) {
case \'text\':
$value = stripcslashes($_REQUEST[\'attachments\'][$attachment_id]["{$field_name}"]);
update_post_meta( $attachment_id, "{$field_name}", $value );
break;
default:
# Not implemented
break;
}
}
}
}
return $attachment_id;
}
add_filter( \'edit_attachment\', \'action_edit_attachment\' );
如果只想从媒体屏幕保存,可以更改\\u请求检查
if ( isset($_REQUEST[\'action\']) && \'save-attachment-compat\' == "{$_REQUEST[\'action\']}" ) {
或从完整附件帖子编辑器
if ( isset($_REQUEST[\'action\']) && \'editpost\' == "{$_REQUEST[\'action\']}" ) {
不管怎么说,我对旧方法没有什么好运气。。。看到至少有几个人和我一样有困难。