Wordpress:如何挂钩 get_the_post_thumbnail_url() 函数

Wordpress: how to hook get_the_post_thumbnail_url() function

我是 Wordpress 新手。我想要做的是挂钩 get_the_post_thumbnail_url() 和 return 稍微不同的 URL。我知道我可以使用 WP add_action() 进行挂钩。如果我编写我的函数和 return 所需的字符串,我如何确保 get_the_post_thumbnail_url() 将 return 我的自定义代码?

get_the_post_thumbnail_url in wp-includes/post-thumbnail-template.php 没有钩子或动作,它被定义如下:

function get_the_post_thumbnail_url( $post = null, $size = 'post-thumbnail' ) {
  $post_thumbnail_id = get_post_thumbnail_id( $post );
  if ( ! $post_thumbnail_id ) {
    return false;
  }
  return wp_get_attachment_image_url( $post_thumbnail_id, $size );
}

如果您遵循 wp_get_attachment_image_url 函数,您会发现它使用了 wp_get_attachment_image_src 函数来应用过滤器所以你可以使用 wp_get_attachment_image_src

为它创建一个过滤器

这将是您与该功能交互的唯一方式,尽管在功能树的更上层。

使用说明如下:

/**
     * Filters the image src result.
     *
     * @since 4.3.0
     *
     * @param array|false  $image         Either array with src, width & height, icon src, or false.
     * @param int          $attachment_id Image attachment ID.
     * @param string|array $size          Size of image. Image size or array of width and height values
     *                                    (in that order). Default 'thumbnail'.
     * @param bool         $icon          Whether the image should be treated as an icon. Default false.
     */
    apply_filters( 'wp_get_attachment_image_src', $image, $attachment_id, $size, $icon );

我可以看到两种不同的方法来实现这一点。

解决方案 1(来自媒体库的文件)

有一个过滤器可用于覆盖(或提供默认值)get_the_post_thumbnail_url() 函数。

在此过程中,函数将调用 get_post_meta(),最终将调用过滤器 get_{$meta_type}_metadata

你可以做的是用类似的东西挂钩这个过滤器:

add_filter( 'get_post_metadata', function ( $metadata, int $object_id, string $meta_key, bool $single, string $meta_type ) {
    // @todo fetch a predefined file ID (this must be an existing file from the media library).

    return $metadata;
}, 10, );

此方法的缺点是您无法提供一些随机 URL,但您需要使用媒体库中的现有文件。

解决方案 2(任何自定义 URL)

为了为您的 post 缩略图提供完全可定制的 URL,您需要挂接 3 个不同的函数。

add_filter( 'has_post_thumbnail', 'so52332168_hasPostThumbnail', 10, 3 );
add_filter( 'get_post_metadata', 'so52332168_getPostThumbnailFileId', 10, 4 );
add_filter( 'wp_get_attachment_image_src', 'so52332168_getPostThumbnailSrc', 10, 4 );

/**
 * Use any way you want to determine if your post should have a custom thumbnail URL.
 *
 * @param $has_thumbnail
 * @param $post_id
 * @param $thumbnail_id
 *
 * @return bool
 */
function so52332168_hasPostThumbnail( $has_thumbnail, $post_id, $thumbnail_id ) {
    if ( ! $has_thumbnail ) {
        // @todo check if the $post_id match any rule to get a custom thumbnail.
        // if so, return true here

        return true;
    }
    
    return $has_thumbnail;
}

/**
 * Force override the thumbnail URL.
 *
 * @param $value
 * @param $object_id
 * @param $meta_key
 * @param $single
 *
 * @return int
 */
function so52332168_getPostThumbnailFileId( $value, $object_id, $meta_key, $single ) {
    if ( '_thumbnail_id' === $meta_key ) {
        // @todo check if the current $object_id should get a custom thumbnail (probably the same check as above)
            global $customPostThumbnailUrl;
            $customPostThumbnailUrl = 'https://placekitten.com/300/300';

            // return the fake thumbnail id -> this should absolutely never match an actual file id
            return PHP_INT_MAX;
            }
        }
    }
    
    return $value;
}

/**
 * Eventually return the URL that we "calculate" for the post.
 *
 * @param $image
 * @param $attachment_id
 * @param $size
 * @param $icon
 *
 * @return array
 */
function so52332168_getPostThumbnailSrc( $image, $attachment_id, $size, $icon ) {
    global $customPostThumbnailUrl;

    // check that the attachment id is the one we returned previously
    if ( (PHP_INT_MAX === $attachment_id) && $customPostThumbnailUrl ) {
        // return our custom image URL
        return [ $customPostThumbnailUrl ];
    }
    
    return $image;
}