【问题标题】:Wordpress: how to hook get_the_post_thumbnail_url() functionWordpress:如何挂钩 get_the_post_thumbnail_url() 函数
【发布时间】:2018-09-14 12:35:05
【问题描述】:

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

【问题讨论】:

  • 您需要挂钩还是可以创建自己的函数版本,例如:my_get_the_post_thumbnail_url() 将其参数传递给原始函数,在那里进行修改并返回修改?跨度>

标签: php wordpress wordpress-hook


【解决方案1】:
wp-includes/post-thumbnail-template.php中的

get_the_post_thumbnail_url没有钩子或动作,定义如下:

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为其创建过滤器 p>

这将是您与该功能进行交互的唯一方式,尽管在功能树上稍远一点。

使用说明如下:

/**
     * 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 );

【讨论】:

    【解决方案2】:

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

    解决方案 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)

    为了为您的帖子缩略图提供完全可定制的 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;
    }
    

    【讨论】:

      猜你喜欢
      • 2013-04-04
      • 1970-01-01
      • 2022-01-23
      • 1970-01-01
      • 1970-01-01
      • 2020-03-28
      • 2011-11-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多