Php 强制WordPress Gallery使用媒体文件链接而不是附件链接
是否有过滤器或挂钩强制WordPress图库使用媒体文件链接而不是附件链接。 我无法信任我的客户端在创建库时手动切换设置。Php 强制WordPress Gallery使用媒体文件链接而不是附件链接,php,wordpress,hook,add-filter,Php,Wordpress,Hook,Add Filter,是否有过滤器或挂钩强制WordPress图库使用媒体文件链接而不是附件链接。 我无法信任我的客户端在创建库时手动切换设置。 提出了为什么媒体文件不是默认类型的另一个问题。您基本上可以验证默认的库短代码 remove_shortcode('gallery', 'gallery_shortcode'); // removes the original shortcode add_shortcode('gallery', 'my_awesome_gallery_shortcode'); //
提出了为什么媒体文件不是默认类型的另一个问题。您基本上可以验证默认的库短代码
remove_shortcode('gallery', 'gallery_shortcode'); // removes the original shortcode
add_shortcode('gallery', 'my_awesome_gallery_shortcode'); // add your own shortcode
function my_awesome_gallery_shortcode($attr) {
$output = 'Codex is your friend' ;
return $output;
}
在自定义函数中,您可以设置任何需要的内容。。看这张照片
另一种方法是实际过滤属性(如果您转到上面的链接,您将看到一个过滤器挂钩)
Obmerk Kronen的答案非常简洁,适用于大多数情况。然而,最近我在我客户的一个网站上遇到了一个问题,这个问题无法通过这种方式解决。我真的不知道,为什么。我想这是因为另一个图库插件篡改了相同的短代码 所以,我找到了一个不同的解决方案,不是那么简单,而是有效的。在这里,也许有人会觉得这很有帮助:
add_filter('the_content','galleries_attachments_to_media_links');
function galleries_attachments_to_media_links( $content ) {
/**
* Overrides gallery shortcode settings and forces media file loading, if gallery set to attachment link
*/
if(strpos($content,'[gallery') !== FALSE) {
$content = str_replace('[gallery','[gallery link="file"',$content);
return $content;
}
else return $content;
}
看起来,即使多媒体资料已设置为媒体文件,这也不是问题,WordPress会正确执行短代码。您可以使用自己的多媒体资料短代码验证您的多媒体资料谢谢!我没有找到正确的答案。我试图通过wp_get_attachment_link filter更改链接。再次感谢!
add_filter('the_content','galleries_attachments_to_media_links');
function galleries_attachments_to_media_links( $content ) {
/**
* Overrides gallery shortcode settings and forces media file loading, if gallery set to attachment link
*/
if(strpos($content,'[gallery') !== FALSE) {
$content = str_replace('[gallery','[gallery link="file"',$content);
return $content;
}
else return $content;
}