图像过期时如何更改 Wordpress 帖子中的图像源
How to change the image source inside Wordpress Posts when an image is expired
如题所述。如果图像的到期日期已过期,我会尝试将图像源更改为默认图像。
我使用了 ACF 并为所有媒体文件添加了一个新的日期字段。
然后我创建了以下函数:
function check_expiration_date($post) {
$url = get_the_post_thumbnail_url($post->ID);
$thumbnail_id = get_post_thumbnail_id( $post->ID );
$today = date('d/m/Y');
$expire = get_field('expiration_date', $thumbnail_id);
if( $expire < $today && $expire!="" )
{
$url = "default.jpg";
}
return $url;
}
现在这个函数适用于我在模板文件中特别添加的图像
但这不适用于 post 页上通过编辑器添加的图像。
我试图在尝试获取元信息之前先使用各种挂钩更改 html 标记,但其中 none 对结果有任何影响。举个例子:
function change_image_markup($html, $id, $caption, $title, $align, $url) {
$html5 = "<section>";
$html5 .= "<figure id='post-$id media-$id' class='align-$align'>";
$html5 .= "<img src='$url' alt='$title' />";
if ($caption) {
$html5 .= "<figcaption>$caption</figcaption>";
}
$html5 .= "</figure>";
$html5 .= "<section>";
return $html5;
}
add_filter( 'image_send_to_editor', 'change_image_markup', 10, 9 );
是否有特定的钩子可以改变图像的 html 标记?
我正在使用 Wordpress 5.5.1
任何提示或技巧表示赞赏。
由于这个问题已经一个月了,我没有收到任何答案,所以我想我会回答我自己的问题。虽然要求有所不同。我没有将图像更改为默认图像,而是完全删除了图像。两者都可以实现。
所以没有真正的钩子可以让我出来,因为这些钩子只在实际插入图像时调用操作。
在我的例子中,每次加载 post 时,都必须检查图像是否过期。
我没有使用钩子,而是创建了自己的函数并在 single.php
中调用该函数
在single.php中通常有类似echo post->post_content的东西;
相反,我调用我的函数并将 post 内容作为值。
add_post_based_on_expiration_date($post->post_content);
我的函数看起来像这样:
function add_post_based_on_expiration_date($post_content){
$search_pattern = '/<figure(.*)\/figure>/'; //<figure(.*)/figure> /wp-image-(\d+)/
preg_match_all( $search_pattern, $post_content, $embedded_images );
$embedded_images_count = count( $embedded_images[0] );
$new_content = $post_content;
if ( $embedded_images_count > 0 ) {
for ( $i=0; $i < $embedded_images_count ; $i++ ) {
$dom = new DOMDocument;
@$dom->loadHTML($embedded_images[0][$i]);
$image = $dom->getElementsByTagName('img');
$imageId = str_replace("wp-image-" , "" , $image[0]->getAttribute('class'));
if(check_expiration_date_get_by_id($imageId)){
$new_content = str_replace($embedded_images[0][$i],"",$new_content);
}
};
};
echo $new_content;
}
它看起来有点乱,但它确实起作用了。我所做的是使用正则表达式查找图像标记
因此,如果图像已过期,图像标记将被完全删除。
除了混乱的代码之外,它的缺点是处理时间。因为它总是在打开 post 站点时调用。
如题所述。如果图像的到期日期已过期,我会尝试将图像源更改为默认图像。
我使用了 ACF 并为所有媒体文件添加了一个新的日期字段。
然后我创建了以下函数:
function check_expiration_date($post) {
$url = get_the_post_thumbnail_url($post->ID);
$thumbnail_id = get_post_thumbnail_id( $post->ID );
$today = date('d/m/Y');
$expire = get_field('expiration_date', $thumbnail_id);
if( $expire < $today && $expire!="" )
{
$url = "default.jpg";
}
return $url;
}
现在这个函数适用于我在模板文件中特别添加的图像 但这不适用于 post 页上通过编辑器添加的图像。
我试图在尝试获取元信息之前先使用各种挂钩更改 html 标记,但其中 none 对结果有任何影响。举个例子:
function change_image_markup($html, $id, $caption, $title, $align, $url) {
$html5 = "<section>";
$html5 .= "<figure id='post-$id media-$id' class='align-$align'>";
$html5 .= "<img src='$url' alt='$title' />";
if ($caption) {
$html5 .= "<figcaption>$caption</figcaption>";
}
$html5 .= "</figure>";
$html5 .= "<section>";
return $html5;
}
add_filter( 'image_send_to_editor', 'change_image_markup', 10, 9 );
是否有特定的钩子可以改变图像的 html 标记?
我正在使用 Wordpress 5.5.1 任何提示或技巧表示赞赏。
由于这个问题已经一个月了,我没有收到任何答案,所以我想我会回答我自己的问题。虽然要求有所不同。我没有将图像更改为默认图像,而是完全删除了图像。两者都可以实现。
所以没有真正的钩子可以让我出来,因为这些钩子只在实际插入图像时调用操作。 在我的例子中,每次加载 post 时,都必须检查图像是否过期。
我没有使用钩子,而是创建了自己的函数并在 single.php
中调用该函数在single.php中通常有类似echo post->post_content的东西; 相反,我调用我的函数并将 post 内容作为值。
add_post_based_on_expiration_date($post->post_content);
我的函数看起来像这样:
function add_post_based_on_expiration_date($post_content){
$search_pattern = '/<figure(.*)\/figure>/'; //<figure(.*)/figure> /wp-image-(\d+)/
preg_match_all( $search_pattern, $post_content, $embedded_images );
$embedded_images_count = count( $embedded_images[0] );
$new_content = $post_content;
if ( $embedded_images_count > 0 ) {
for ( $i=0; $i < $embedded_images_count ; $i++ ) {
$dom = new DOMDocument;
@$dom->loadHTML($embedded_images[0][$i]);
$image = $dom->getElementsByTagName('img');
$imageId = str_replace("wp-image-" , "" , $image[0]->getAttribute('class'));
if(check_expiration_date_get_by_id($imageId)){
$new_content = str_replace($embedded_images[0][$i],"",$new_content);
}
};
};
echo $new_content;
}
它看起来有点乱,但它确实起作用了。我所做的是使用正则表达式查找图像标记 因此,如果图像已过期,图像标记将被完全删除。
除了混乱的代码之外,它的缺点是处理时间。因为它总是在打开 post 站点时调用。