因此,我需要添加一个类来链接这些图像,以达到灯箱的目的。但仅限于上传目录中的图像,而不是外部图像。我通过@TommiForsström找到了这个解决方案here:
function add_colorbox_class_to_image_links($html, $attachment_id, $attachment) {
$linkptrn = "/<a[^>]*>/";
$found = preg_match($linkptrn, $html, $a_elem);
// If no link, do nothing
if($found <= 0) return $html;
$a_elem = $a_elem[0];
// Check to see if the link is to an uploaded image
$is_attachment_link = strstr($a_elem, "wp-content/uploads/");
// If link is to external resource, do nothing
if($is_attachment_link === FALSE) return $html;
if(strstr($a_elem, "class=\\"") !== FALSE){ // If link already has class defined inject it to attribute
$a_elem_new = str_replace("class=\\"", "class=\\"colorbox ", $a_elem);
$html = str_replace($a_elem, $a_elem_new, $html);
}else{ // If no class defined, just add class attribute
$html = str_replace("<a ", "<a class=\\"colorbox\\" ", $html);
}
return $html;
}
add_filter(\'image_send_to_editor\', \'add_colorbox_class_to_image_links\', 10, 3);
看起来这正是我需要的,只是不起作用。我已经尝试了一个干净的WP三十三安装以及它。有什么想法吗?谢谢