插件idea#1
当我们将图像插入编辑器时,插件会自动将标题和alt属性修改为:
[caption id="attachment_729" align="alignnone" width="300"]
<a href="http://example.tld/wp-content/uploads/2015/02/wordpress.jpg">
<img src="http://example.tld/wp-content/uploads/2015/02/wordpress-300x284.jpg"
alt="###ALT###"
width="300"
height="284"
class="size-medium wp-image-729" />
</a>
###CAPTION###
[/caption]
然后在前端,根据
attachment_729
字符串并替换
###ALT###
和
###CAPTION###
虚拟值。
因此,当我们想要修改所有图像副本的标题时,只需在Media Manager中修改一次即可。
下面是我们的动态图像标题插件,它应该支持此功能(PHP 5.4+):
<?php
/**
* Plugin Name: Dynamic Image Caption #1
* Description: We only want to modify the image captions in the Media Manager
* Plugin URI: http://wordpress.stackexchange.com/a/179689/26350
* Plugin Author: Birgir Erlendsson (birgire)
* Version: 0.0.1
*/
namespace birgire\\wpse;
add_action( \'init\', function()
{
$o = new DynamicImageCaption;
$o->activate();
});
class DynamicImageCaption
{
private $caption_dummy = \'###CAPTION###\';
private $alt_dummy = \'###ALT###\';
public function activate()
{
add_shortcode(
\'caption\',
[ $this, \'caption_shortcode\' ]
);
add_filter(
\'image_add_caption_text\',
[ $this, \'add_caption_text\' ],
10,
2
);
add_filter(
\'image_send_to_editor\',
[ $this, \'send_to_editor\' ],
10,
8
);
}
public function caption_shortcode( $attr = [], $content = \'\' )
{
if( isset( $attr[\'id\'] )
&& (
false !== strpos( $content, $this->caption_dummy )
||
false !== strpos( $content, $this->alt_dummy )
)
)
{
$caption = $this->get_caption(
$attr[\'id\'],
str_replace( \'attachment_\', \'\', $attr[\'id\'] )
);
$content = str_replace(
[ $this->caption_dummy, $this->alt_dummy ],
[ $caption, esc_attr( $caption ) ],
$content
);
}
return img_caption_shortcode( $attr, $content );
}
private function get_caption( $caption, $id )
{
$caption = \'\';
$attachments = get_posts(
[
\'p\' => $id,
\'post_type\' => \'attachment\',
\'post_status\' => \'inherit\'
]
);
if( isset( $attachments[0] )
&& $attachments[0] instanceof \\WP_Post
)
$caption = $attachments[0]->post_excerpt;
return $caption;
}
public function send_to_editor( $html, $id, $caption, $title, $align, $url, $size, $alt )
{
if( $alt )
{
$html = str_replace(
\'alt="\' . esc_attr( $alt ),
\'alt="\' . $this->alt_dummy,
$html
);
}
return $html;
}
} // end class
将其复制到文件中:
/wp-content/plugins/dynamic-image-caption/dynamic-image-caption.php
并激活插件。
请注意,为了方便起见,这里我还将标题用作alt属性。您应该对此进行进一步测试,并根据需要进行修改。
Ps:我的第一个想法是在[caption]
短代码,如:
[dynamic_caption id="729"]
以及
[dynamic_alt id="729"]
这是可行的,但我想找到另一种方法,只获取相应的附件一次,而不使用这些短代码。
插件idea#2
这里是另一个修改现有图像标题和alt属性的想法。这个插件没有像以前的插件那样修改post编辑器中的代码,只修改了它的输出。
<?php
/**
* Plugin Name: Dynamic Image Caption And Alt #2
* Description: We only want to modify the image caption and alt in the Media Manager.
* Plugin URI: http://wordpress.stackexchange.com/a/179689/26350
* Plugin Author: Birgir Erlendsson (birgire)
* Version: 0.0.1
*/
namespace birgire\\wpse;
add_action( \'init\', function()
{
$o = new DynamicImageCaptionAlt;
$o->activate();
});
class DynamicImageCaptionAlt
{
public function activate()
{
add_shortcode(
\'caption\',
[ $this, \'caption_shortcode\' ]
);
}
public function caption_shortcode( $attr = [], $content = \'\' )
{
if( isset( $attr[\'id\'] ) )
{
$id = str_replace( \'attachment_\', \'\', $attr[\'id\'] );
// Fetch the caption and the alt attribute:
$caption = $this->get_caption( $id );
$alt = esc_attr( $this->get_alt( $id ) );
// Use caption as alt, if alt is empty:
if( ! $alt )
$alt = esc_attr( $caption );
// Modify the caption:
$content .= \'###END###\';
$content = str_replace(
strip_tags( $content ),
$caption,
$content
);
// Modify the alt attribute:
$content = preg_replace(
\'#alt="[^"]*"#\',
sprintf( \'alt="%s"\', $alt ),
$content
);
}
return img_caption_shortcode( $attr, $content );
}
private function get_caption( $id )
{
global $wpdb;
$sql = $wpdb->prepare(
"SELECT post_excerpt
FROM {$wpdb->posts}
WHERE ID = %d AND post_type = \'attachment\' ",
$id
);
return $wpdb->get_var( $sql );
}
private function get_alt( $id )
{
return get_post_meta( $id, \'_wp_attachment_image_alt\', true );
}
} // end class
请注意,这将像预期的那样向站点添加额外的数据库查询。