如何在 php 中显示修改了 url 的 post 缩略图?
How to display post thumbnail with modified url in php?
我正在使用 WordPress 内容保护插件,该插件具有在浏览器上禁用 javascript 时可以为图像添加水印的功能。该功能正在处理 post 内的图像,如下所示...
首先,当它检测到浏览器不支持 javascript 时,它会执行重定向到 no-javascript php 脚本。然后获取当前post的内容,在内容中匹配any/all img src URLs,修改img src URLs(这样图片解析通过应用图像水印的 php 脚本),然后 returns 具有修改后的 URLs 的 post 内容。一切顺利。
但是,我希望保护的大多数图像都设置为特色图像(又名 post 缩略图),因此不是内容的一部分。我没有得到插件作者(付费插件)的任何支持。所以我想修改插件。
我知道 wp_get_attachment_image_src() 和 the_post_thumbnail_url() 是获取特色图片 URL 的方法。但是我找不到任何方法来 return 为当前 post 修改特色图片 URL。
所以我想要的是一个小脚本,对于当前 post/page,允许我访问特色图像 URL 和 return 修改后的 URL由浏览器显示(与获取、修改和 post 的 the_content 相同的概念)。我不想更改图像附件的详细信息。可能吗?
拜托,不要 comments/discussion content/image 保护的优点(或者更确切地说是无用的)。那是完全不同的讨论。
提前感谢您提供的任何支持!!
无法理解您的场景,但乍一看似乎您应该查看过滤器 wp_get_attachment_image_attributes
以修改返回的 post-缩略图 url.
如果我们分解你说的话:
But I can't find any means of returning a modified featured image URL
for the current post.
从场景中获取 $attachment id/ post-thumbnail;在循环中/或当 is_feed() 或 is_single() 或任何需要保护输出的地方时 。 (没有你的上下文不能提供更多帮助)将图像传递到过滤器中:
function change_or_filter_the_url($attr, $attachment, $size){
if(is_admin()) return $attr;
if($size != 'post-thumbnail') return $attr;
$attr['src'] = ... do stuff with the url here ...
return $attr;
}
add_filter('wp_get_attachment_image_attributes', 'change_or_filter_the_url', 10, 3);
但我想完成您的回答不仅如此。看看这个关于 Is it possible set a featured image with external image URL 的话题。它以一种方式实现相同的目标。您可能会 从所有代码 中找到正确的方向 来解决一个可以理解的问题 。
我正在使用 WordPress 内容保护插件,该插件具有在浏览器上禁用 javascript 时可以为图像添加水印的功能。该功能正在处理 post 内的图像,如下所示... 首先,当它检测到浏览器不支持 javascript 时,它会执行重定向到 no-javascript php 脚本。然后获取当前post的内容,在内容中匹配any/all img src URLs,修改img src URLs(这样图片解析通过应用图像水印的 php 脚本),然后 returns 具有修改后的 URLs 的 post 内容。一切顺利。
但是,我希望保护的大多数图像都设置为特色图像(又名 post 缩略图),因此不是内容的一部分。我没有得到插件作者(付费插件)的任何支持。所以我想修改插件。
我知道 wp_get_attachment_image_src() 和 the_post_thumbnail_url() 是获取特色图片 URL 的方法。但是我找不到任何方法来 return 为当前 post 修改特色图片 URL。
所以我想要的是一个小脚本,对于当前 post/page,允许我访问特色图像 URL 和 return 修改后的 URL由浏览器显示(与获取、修改和 post 的 the_content 相同的概念)。我不想更改图像附件的详细信息。可能吗?
拜托,不要 comments/discussion content/image 保护的优点(或者更确切地说是无用的)。那是完全不同的讨论。
提前感谢您提供的任何支持!!
无法理解您的场景,但乍一看似乎您应该查看过滤器 wp_get_attachment_image_attributes
以修改返回的 post-缩略图 url.
如果我们分解你说的话:
But I can't find any means of returning a modified featured image URL for the current post.
从场景中获取 $attachment id/ post-thumbnail;在循环中/或当 is_feed() 或 is_single() 或任何需要保护输出的地方时 。 (没有你的上下文不能提供更多帮助)将图像传递到过滤器中:
function change_or_filter_the_url($attr, $attachment, $size){
if(is_admin()) return $attr;
if($size != 'post-thumbnail') return $attr;
$attr['src'] = ... do stuff with the url here ...
return $attr;
}
add_filter('wp_get_attachment_image_attributes', 'change_or_filter_the_url', 10, 3);
但我想完成您的回答不仅如此。看看这个关于 Is it possible set a featured image with external image URL 的话题。它以一种方式实现相同的目标。您可能会 从所有代码 中找到正确的方向 来解决一个可以理解的问题 。